Skip to content

Commit 314216b

Browse files
committed
implementing webpack-log, cleaning up sinon sandboxes in tests
1 parent 0f9e063 commit 314216b

File tree

8 files changed

+43
-79
lines changed

8 files changed

+43
-79
lines changed

lib/context.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const logger = require('./log');
3+
const weblog = require('webpack-log');
44

55
module.exports = function ctx(compiler, options) {
66
const context = {
@@ -16,7 +16,11 @@ module.exports = function ctx(compiler, options) {
1616
if (options.logger) {
1717
context.log = options.logger;
1818
} else {
19-
context.log = logger(options);
19+
context.log = weblog({
20+
level: options.logLevel || 'info',
21+
name: 'wdm',
22+
timestamp: options.logTime
23+
});
2024
}
2125

2226
const { log } = context;

lib/log.js

-39
This file was deleted.

package-lock.json

+14-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,13 @@
3434
"webpack": "^2.2.0 || ^3.0.0 || ^4.0.0-alpha"
3535
},
3636
"dependencies": {
37-
"chalk": "^2.3.0",
38-
"log-symbols": "^2.1.0",
39-
"loglevel": "^1.6.0",
40-
"loglevel-plugin-prefix": "^0.5.3",
4137
"loud-rejection": "^1.6.0",
4238
"memory-fs": "~0.4.1",
4339
"mime": "^2.0.3",
4440
"path-is-absolute": "^1.0.0",
4541
"range-parser": "^1.0.3",
46-
"time-stamp": "^2.0.0",
4742
"url-join": "^2.0.2",
48-
"uuid": "^3.1.0"
43+
"webpack-log": "^1.0.1"
4944
},
5045
"devDependencies": {
5146
"assert": "^1.4.1",

test/tests/compiler-callbacks.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
const assert = require('assert');
44
const sinon = require('sinon'); // eslint-disable-line import/no-extraneous-dependencies
5+
const weblog = require('webpack-log');
56
const middleware = require('../../');
6-
const log = require('../../lib/log');
77

88
describe('CompilerCallbacks', () => {
99
let plugins = {};
10-
let sandbox;
11-
10+
const sandbox = sinon.sandbox.create();
1211
const compiler = {
1312
watch() {},
1413
plugin(name, callback) {
@@ -17,7 +16,6 @@ describe('CompilerCallbacks', () => {
1716
};
1817

1918
beforeEach(() => {
20-
sandbox = sinon.sandbox.create();
2119
plugins = {};
2220
});
2321

@@ -28,7 +26,7 @@ describe('CompilerCallbacks', () => {
2826
it('watch error should be reported to console', () => {
2927
const err = new Error('Oh noes!');
3028
const stub = sandbox.stub(compiler, 'watch');
31-
const logger = log({ logLevel: 'silent' });
29+
const logger = weblog({ level: 'silent' });
3230
const error = sandbox.spy(logger, 'error');
3331

3432
stub.callsFake((opts, callback) => {

test/tests/lazy.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const doneStats = {
1515

1616
describe('Lazy mode', () => {
1717
let plugins = [];
18+
const sandbox = sinon.sandbox.create();
1819
const res = {};
1920
const compiler = {
2021
plugin(name, callback) {
@@ -26,8 +27,12 @@ describe('Lazy mode', () => {
2627

2728
beforeEach(() => {
2829
plugins = {};
29-
compiler.run = sinon.stub();
30-
next = sinon.stub();
30+
compiler.run = sandbox.stub();
31+
next = sandbox.stub();
32+
});
33+
34+
afterEach(() => {
35+
sandbox.restore();
3136
});
3237

3338
describe('builds', () => {
@@ -61,7 +66,7 @@ describe('Lazy mode', () => {
6166
it('should pass through compiler error', (done) => {
6267
const err = new Error('MyCompilerError');
6368
const { log } = instance.context;
64-
const spy = sinon.spy(log, 'error');
69+
const spy = sandbox.spy(log, 'error');
6570

6671
compiler.run.callsArgWith(0, err);
6772

test/tests/log.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const assert = require('assert');
44
const sinon = require('sinon'); // eslint-disable-line import/no-extraneous-dependencies
5+
const weblog = require('webpack-log');
56
const middleware = require('../../');
67

78
// @note logLevel testing is handled by the loglevel module
@@ -48,14 +49,18 @@ describe('Logging', () => {
4849
});
4950

5051
it('should log with a timestamp', () => {
52+
// we need to kill this logger as it's cached, and new options won't be
53+
// applied. isn't something we're going to do in the module, but needed for
54+
// testing
55+
weblog.delLogger('wdm');
56+
5157
const sandbox = sinon.sandbox.create();
52-
const infoSpy = sandbox.spy(console, 'info');
58+
const info = sandbox.spy(console, 'info');
5359
const instance = middleware(compiler, { logTime: true });
5460
const { log } = instance.context;
5561

5662
spy(instance, sandbox);
5763

58-
5964
log.info('bar');
6065
log.warn('bar');
6166
log.error('bar');
@@ -64,7 +69,8 @@ describe('Logging', () => {
6469
assert.strictEqual(log.warn.callCount, 1);
6570
assert.strictEqual(log.error.callCount, 1);
6671
assert(/bar/.test(log.info.firstCall.args[0]));
67-
assert(/[\d{2}:\d{2}:\d{2}]/.test(infoSpy.firstCall.args[0]));
72+
73+
assert(/[\d{2}:\d{2}:\d{2}]/.test(info.firstCall.args[0]));
6874

6975
sandbox.restore();
7076
});

test/tests/reporter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const rawStats = fs.readFileSync(statsPath, 'utf8');
1414

1515
describe('Reporter', () => {
1616
let plugins = {};
17-
let sandbox;
17+
const sandbox = sinon.sandbox.create();
1818
const defaults = { logLevel: 'silent' };
1919
const compiler = {
2020
watch() {
@@ -46,7 +46,7 @@ describe('Reporter', () => {
4646
}
4747

4848
beforeEach(() => {
49-
sandbox = sinon.sandbox.create();
49+
// sandbox = sinon.sandbox.create();
5050
plugins = {};
5151
});
5252

0 commit comments

Comments
 (0)