Skip to content

Commit e68355a

Browse files
committed
Refactoring existing logger asserts to new system
1 parent 45c03bf commit e68355a

File tree

5 files changed

+25
-68
lines changed

5 files changed

+25
-68
lines changed

Diff for: lib/logger.js

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ module.exports = {
8383
return messages.warning;
8484
},
8585

86+
/**
87+
* @returns {Array}
88+
*/
89+
getMessages() {
90+
return messages;
91+
},
92+
8693
quiet(setQuiet = true) {
8794
config.quiet = setQuiet;
8895
},

Diff for: test/WebpackConfig.js

+6-32
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const RuntimeConfig = require('../lib/config/RuntimeConfig');
1515
const path = require('path');
1616
const fs = require('fs');
1717
const webpack = require('webpack');
18-
const logger = require('../lib/logger');
18+
const loggerAssert = require('./helpers/logger-assert');
1919

2020
function createConfig() {
2121
const runtimeConfig = new RuntimeConfig();
@@ -139,11 +139,9 @@ describe('WebpackConfig object', () => {
139139

140140
it('You can omit the opening slash, but get a warning', () => {
141141
const config = createConfig();
142-
logger.reset();
143-
logger.quiet();
144142

145143
config.setPublicPath('foo');
146-
expect(logger.getMessages().warning).to.have.lengthOf(1);
144+
loggerAssert.assertWarning('TODO');
147145
});
148146
});
149147

@@ -206,10 +204,8 @@ describe('WebpackConfig object', () => {
206204
it('You can use an opening slash, but get a warning', () => {
207205
const config = createConfig();
208206

209-
logger.reset();
210-
logger.quiet();
211207
config.setManifestKeyPrefix('/foo/');
212-
expect(logger.getMessages().warning).to.have.lengthOf(1);
208+
loggerAssert.assertWarning('TODO');
213209
});
214210
});
215211

@@ -593,15 +589,6 @@ describe('WebpackConfig object', () => {
593589
});
594590

595591
describe('configureBabel', () => {
596-
beforeEach(() => {
597-
logger.reset();
598-
logger.quiet();
599-
});
600-
601-
afterEach(() => {
602-
logger.quiet(false);
603-
});
604-
605592
it('Calling method sets it', () => {
606593
const config = createConfig();
607594
const testCallback = () => {};
@@ -668,26 +655,22 @@ describe('WebpackConfig object', () => {
668655
config.runtimeConfig.babelRcFileExists = true;
669656
config.configureBabel(() => {});
670657

671-
const warnings = logger.getMessages().warning;
672-
expect(warnings).to.have.lengthOf(1);
673-
expect(warnings[0]).to.contain('your app already provides an external Babel configuration');
658+
loggerAssert.assertWarning('your app already provides an external Babel configuration');
674659
});
675660

676661
it('Calling with a whitelisted option when .babelrc is present works fine', () => {
677662
const config = createConfig();
678663
config.runtimeConfig.babelRcFileExists = true;
679664
config.configureBabel(null, { includeNodeModules: ['foo'] });
680-
expect(logger.getMessages().warning).to.be.empty;
665+
loggerAssert.assertWarning('TODO');
681666
});
682667

683668
it('Calling with a non-whitelisted option when .babelrc is present displays a warning', () => {
684669
const config = createConfig();
685670
config.runtimeConfig.babelRcFileExists = true;
686671
config.configureBabel(null, { useBuiltIns: 'foo' });
687672

688-
const warnings = logger.getMessages().warning;
689-
expect(warnings).to.have.lengthOf(1);
690-
expect(warnings[0]).to.contain('your app already provides an external Babel configuration');
673+
loggerAssert.assertWarning('your app already provides an external Babel configuration');
691674
});
692675

693676
it('Pass invalid config', () => {
@@ -716,15 +699,6 @@ describe('WebpackConfig object', () => {
716699
});
717700

718701
describe('configureBabelPresetEnv', () => {
719-
beforeEach(() => {
720-
logger.reset();
721-
logger.quiet();
722-
});
723-
724-
afterEach(() => {
725-
logger.quiet(false);
726-
});
727-
728702
it('Calling method sets it', () => {
729703
const config = createConfig();
730704
const testCallback = () => {};

Diff for: test/config-generator.js

+4-19
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ManifestPlugin = require('webpack-manifest-plugin');
1818
const CleanWebpackPlugin = require('clean-webpack-plugin');
1919
const webpack = require('webpack');
2020
const path = require('path');
21-
const logger = require('../lib/logger');
21+
const loggerAssert = require('./helpers/logger-assert');
2222

2323
const isWindows = (process.platform === 'win32');
2424

@@ -372,10 +372,6 @@ describe('The config-generator function', () => {
372372
});
373373

374374
it('enableEslintLoader("extends-name")', () => {
375-
before(() => {
376-
logger.reset();
377-
});
378-
379375
const config = createConfig();
380376
config.addEntry('main', './main');
381377
config.publicPath = '/';
@@ -384,7 +380,7 @@ describe('The config-generator function', () => {
384380

385381
const actualConfig = configGenerator(config);
386382

387-
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('enableEslintLoader: Extending from a configuration is deprecated, please use a configuration file instead. See https://eslint.org/docs/user-guide/configuring for more information.');
383+
loggerAssert.assertDeprecation('enableEslintLoader: Extending from a configuration is deprecated, please use a configuration file instead. See https://eslint.org/docs/user-guide/configuring for more information.');
388384
expect(JSON.stringify(actualConfig.module.rules)).to.contain('eslint-loader');
389385
expect(JSON.stringify(actualConfig.module.rules)).to.contain('extends-name');
390386
});
@@ -1068,15 +1064,6 @@ describe('The config-generator function', () => {
10681064
});
10691065

10701066
describe('Test shouldUseSingleRuntimeChunk', () => {
1071-
before(() => {
1072-
logger.reset();
1073-
logger.quiet();
1074-
});
1075-
1076-
after(() => {
1077-
logger.quiet(false);
1078-
});
1079-
10801067
it('Set to true', () => {
10811068
const config = createConfig();
10821069
config.outputPath = '/tmp/public/build';
@@ -1085,7 +1072,6 @@ describe('The config-generator function', () => {
10851072

10861073
const actualConfig = configGenerator(config);
10871074
expect(actualConfig.optimization.runtimeChunk).to.equal('single');
1088-
expect(logger.getMessages().deprecation).to.be.empty;
10891075
});
10901076

10911077
it('Set to false', () => {
@@ -1096,7 +1082,6 @@ describe('The config-generator function', () => {
10961082

10971083
const actualConfig = configGenerator(config);
10981084
expect(actualConfig.optimization.runtimeChunk).to.be.undefined;
1099-
expect(logger.getMessages().deprecation).to.be.empty;
11001085
});
11011086

11021087
it('Not set + createSharedEntry()', () => {
@@ -1107,7 +1092,7 @@ describe('The config-generator function', () => {
11071092

11081093
const actualConfig = configGenerator(config);
11091094
expect(actualConfig.optimization.runtimeChunk.name).to.equal('manifest');
1110-
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('the recommended setting is Encore.enableSingleRuntimeChunk()');
1095+
loggerAssert.assertDeprecation('the recommended setting is Encore.enableSingleRuntimeChunk()');
11111096
});
11121097

11131098
it('Not set without createSharedEntry()', () => {
@@ -1117,7 +1102,7 @@ describe('The config-generator function', () => {
11171102

11181103
const actualConfig = configGenerator(config);
11191104
expect(actualConfig.optimization.runtimeChunk).to.be.undefined;
1120-
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('the recommended setting is Encore.enableSingleRuntimeChunk()');
1105+
loggerAssert.assertDeprecation('the recommended setting is Encore.enableSingleRuntimeChunk()');
11211106
});
11221107
});
11231108

Diff for: test/config/path-util.js

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const WebpackConfig = require('../../lib/WebpackConfig');
1414
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
1515
const pathUtil = require('../../lib/config/path-util');
1616
const process = require('process');
17+
const loggerAssert = require('../helpers/logger-assert');
1718

1819
function createConfig() {
1920
const runtimeConfig = new RuntimeConfig();
@@ -54,6 +55,8 @@ describe('path-util getContentBase()', () => {
5455

5556
const actualContentBase = pathUtil.getContentBase(config);
5657
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');
58+
59+
loggerAssert.assertWarning('The value passed to setManifestKeyPrefix "/build/" starts with "/". This is allowed, but');
5760
});
5861

5962
it('contentBase is calculated correctly with no public path', function() {

Diff for: test/config/validator.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const expect = require('chai').expect;
1313
const WebpackConfig = require('../../lib/WebpackConfig');
1414
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
1515
const validator = require('../../lib/config/validator');
16-
const logger = require('../../lib/logger');
16+
const loggerAssert = require('../helpers/logger-assert');
1717

1818
function createConfig() {
1919
const runtimeConfig = new RuntimeConfig();
@@ -75,12 +75,9 @@ describe('The validator function', () => {
7575
config.addEntry('main', './main');
7676
config.runtimeConfig.useDevServer = true;
7777

78-
logger.reset();
79-
logger.quiet();
8078
validator(config);
8179

82-
expect(logger.getMessages().warning).to.have.lengthOf(1);
83-
expect(logger.getMessages().warning[0]).to.include('Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues');
80+
loggerAssert.assertWarning('Passing an absolute URL to setPublicPath() *and* using the dev-server can cause issues');
8481
});
8582

8683
it('warning with createSharedEntry() and core cache group name', () => {
@@ -89,12 +86,9 @@ describe('The validator function', () => {
8986
config.setPublicPath('/build');
9087
config.createSharedEntry('vendors', './main');
9188

92-
logger.reset();
93-
logger.quiet();
9489
validator(config);
9590

96-
expect(logger.getMessages().warning).to.have.lengthOf(1);
97-
expect(logger.getMessages().warning[0]).to.include('Passing "vendors" to createSharedEntry() is not recommended');
91+
loggerAssert.assertWarning('Passing "vendors" to createSharedEntry() is not recommended');
9892
});
9993

10094
it('warning with addCacheGroup() and core cache group name', () => {
@@ -106,12 +100,9 @@ describe('The validator function', () => {
106100
test: /[\\/]main/,
107101
});
108102

109-
logger.reset();
110-
logger.quiet();
111103
validator(config);
112104

113-
expect(logger.getMessages().warning).to.have.lengthOf(1);
114-
expect(logger.getMessages().warning[0]).to.include('Passing "defaultVendors" to addCacheGroup() is not recommended');
105+
loggerAssert.assertWarning('Passing "defaultVendors" to addCacheGroup() is not recommended');
115106
});
116107

117108
it('warning with addCacheGroup() and a similar createSharedEntry() name', () => {
@@ -124,11 +115,8 @@ describe('The validator function', () => {
124115
test: /[\\/]main/,
125116
});
126117

127-
logger.reset();
128-
logger.quiet();
129118
validator(config);
130119

131-
expect(logger.getMessages().warning).to.have.lengthOf(1);
132-
expect(logger.getMessages().warning[0]).to.include('Using the same name when calling createSharedEntry() and addCacheGroup() is not recommended.');
120+
loggerAssert.assertWarning('Using the same name when calling createSharedEntry() and addCacheGroup() is not recommended.');
133121
});
134122
});

0 commit comments

Comments
 (0)