Skip to content

Commit

Permalink
Refactoring existing logger asserts to new system
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed May 10, 2020
1 parent 45c03bf commit e68355a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 68 deletions.
7 changes: 7 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ module.exports = {
return messages.warning;
},

/**
* @returns {Array}
*/
getMessages() {
return messages;
},

quiet(setQuiet = true) {
config.quiet = setQuiet;
},
Expand Down
38 changes: 6 additions & 32 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const RuntimeConfig = require('../lib/config/RuntimeConfig');
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const logger = require('../lib/logger');
const loggerAssert = require('./helpers/logger-assert');

function createConfig() {
const runtimeConfig = new RuntimeConfig();
Expand Down Expand Up @@ -139,11 +139,9 @@ describe('WebpackConfig object', () => {

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

config.setPublicPath('foo');
expect(logger.getMessages().warning).to.have.lengthOf(1);
loggerAssert.assertWarning('TODO');
});
});

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

logger.reset();
logger.quiet();
config.setManifestKeyPrefix('/foo/');
expect(logger.getMessages().warning).to.have.lengthOf(1);
loggerAssert.assertWarning('TODO');
});
});

Expand Down Expand Up @@ -593,15 +589,6 @@ describe('WebpackConfig object', () => {
});

describe('configureBabel', () => {
beforeEach(() => {
logger.reset();
logger.quiet();
});

afterEach(() => {
logger.quiet(false);
});

it('Calling method sets it', () => {
const config = createConfig();
const testCallback = () => {};
Expand Down Expand Up @@ -668,26 +655,22 @@ describe('WebpackConfig object', () => {
config.runtimeConfig.babelRcFileExists = true;
config.configureBabel(() => {});

const warnings = logger.getMessages().warning;
expect(warnings).to.have.lengthOf(1);
expect(warnings[0]).to.contain('your app already provides an external Babel configuration');
loggerAssert.assertWarning('your app already provides an external Babel configuration');
});

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

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

const warnings = logger.getMessages().warning;
expect(warnings).to.have.lengthOf(1);
expect(warnings[0]).to.contain('your app already provides an external Babel configuration');
loggerAssert.assertWarning('your app already provides an external Babel configuration');
});

it('Pass invalid config', () => {
Expand Down Expand Up @@ -716,15 +699,6 @@ describe('WebpackConfig object', () => {
});

describe('configureBabelPresetEnv', () => {
beforeEach(() => {
logger.reset();
logger.quiet();
});

afterEach(() => {
logger.quiet(false);
});

it('Calling method sets it', () => {
const config = createConfig();
const testCallback = () => {};
Expand Down
23 changes: 4 additions & 19 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ManifestPlugin = require('webpack-manifest-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const logger = require('../lib/logger');
const loggerAssert = require('./helpers/logger-assert');

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

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

it('enableEslintLoader("extends-name")', () => {
before(() => {
logger.reset();
});

const config = createConfig();
config.addEntry('main', './main');
config.publicPath = '/';
Expand All @@ -384,7 +380,7 @@ describe('The config-generator function', () => {

const actualConfig = configGenerator(config);

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.');
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.');
expect(JSON.stringify(actualConfig.module.rules)).to.contain('eslint-loader');
expect(JSON.stringify(actualConfig.module.rules)).to.contain('extends-name');
});
Expand Down Expand Up @@ -1068,15 +1064,6 @@ describe('The config-generator function', () => {
});

describe('Test shouldUseSingleRuntimeChunk', () => {
before(() => {
logger.reset();
logger.quiet();
});

after(() => {
logger.quiet(false);
});

it('Set to true', () => {
const config = createConfig();
config.outputPath = '/tmp/public/build';
Expand All @@ -1085,7 +1072,6 @@ describe('The config-generator function', () => {

const actualConfig = configGenerator(config);
expect(actualConfig.optimization.runtimeChunk).to.equal('single');
expect(logger.getMessages().deprecation).to.be.empty;
});

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

const actualConfig = configGenerator(config);
expect(actualConfig.optimization.runtimeChunk).to.be.undefined;
expect(logger.getMessages().deprecation).to.be.empty;
});

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

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

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

const actualConfig = configGenerator(config);
expect(actualConfig.optimization.runtimeChunk).to.be.undefined;
expect(JSON.stringify(logger.getMessages().deprecation)).to.contain('the recommended setting is Encore.enableSingleRuntimeChunk()');
loggerAssert.assertDeprecation('the recommended setting is Encore.enableSingleRuntimeChunk()');
});
});

Expand Down
3 changes: 3 additions & 0 deletions test/config/path-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const pathUtil = require('../../lib/config/path-util');
const process = require('process');
const loggerAssert = require('../helpers/logger-assert');

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

const actualContentBase = pathUtil.getContentBase(config);
expect(actualContentBase).to.equal(isWindows ? 'C:\\tmp\\public' : '/tmp/public');

loggerAssert.assertWarning('The value passed to setManifestKeyPrefix "/build/" starts with "/". This is allowed, but');
});

it('contentBase is calculated correctly with no public path', function() {
Expand Down
22 changes: 5 additions & 17 deletions test/config/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const validator = require('../../lib/config/validator');
const logger = require('../../lib/logger');
const loggerAssert = require('../helpers/logger-assert');

function createConfig() {
const runtimeConfig = new RuntimeConfig();
Expand Down Expand Up @@ -75,12 +75,9 @@ describe('The validator function', () => {
config.addEntry('main', './main');
config.runtimeConfig.useDevServer = true;

logger.reset();
logger.quiet();
validator(config);

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

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

logger.reset();
logger.quiet();
validator(config);

expect(logger.getMessages().warning).to.have.lengthOf(1);
expect(logger.getMessages().warning[0]).to.include('Passing "vendors" to createSharedEntry() is not recommended');
loggerAssert.assertWarning('Passing "vendors" to createSharedEntry() is not recommended');
});

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

logger.reset();
logger.quiet();
validator(config);

expect(logger.getMessages().warning).to.have.lengthOf(1);
expect(logger.getMessages().warning[0]).to.include('Passing "defaultVendors" to addCacheGroup() is not recommended');
loggerAssert.assertWarning('Passing "defaultVendors" to addCacheGroup() is not recommended');
});

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

logger.reset();
logger.quiet();
validator(config);

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

0 comments on commit e68355a

Please sign in to comment.