Skip to content

Commit

Permalink
WIP adding asserts for warnings/deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed May 11, 2020
1 parent d26b955 commit 7dd33d6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/WebpackConfig.js
Expand Up @@ -526,8 +526,6 @@ class WebpackConfig {
}

createSharedEntry(name, file) {
logger.deprecation('Encore.createSharedEntry() is deprecated and will be removed in a future version, please use Encore.splitEntryChunks() or Encore.addCacheGroup() instead.');

if (this.shouldSplitEntryChunks) {
throw new Error('Using splitEntryChunks() and createSharedEntry() together is not supported. Use one of these strategies only to optimize your build.');
}
Expand All @@ -537,6 +535,8 @@ class WebpackConfig {
throw new Error('createSharedEntry() cannot be called multiple times: you can only create *one* shared entry.');
}

logger.deprecation('Encore.createSharedEntry() is deprecated and will be removed in a future version, please use Encore.splitEntryChunks() or Encore.addCacheGroup() instead.');

if (Array.isArray(file)) {
throw new Error('Argument 2 to createSharedEntry() must be a single string file: not an array of files. Try creating one file that requires/imports all the modules that should be included.');
}
Expand Down
12 changes: 8 additions & 4 deletions test/WebpackConfig.js
Expand Up @@ -141,7 +141,7 @@ describe('WebpackConfig object', () => {
const config = createConfig();

config.setPublicPath('foo');
loggerAssert.assertWarning('TODO');
loggerAssert.assertWarning('The value passed to setPublicPath() should *usually* start with "/" or be a full URL');
});
});

Expand Down Expand Up @@ -205,7 +205,7 @@ describe('WebpackConfig object', () => {
const config = createConfig();

config.setManifestKeyPrefix('/foo/');
loggerAssert.assertWarning('TODO');
loggerAssert.assertWarning('The value passed to setManifestKeyPrefix "/foo/" starts with "/". This is allowed, but since the key prefix does not normally start with a "/"');
});
});

Expand Down Expand Up @@ -380,6 +380,7 @@ describe('WebpackConfig object', () => {
it('Calling twice throws an error', () => {
const config = createConfig();
config.createSharedEntry('vendor', 'jquery');
loggerAssert.assertDeprecation('Encore.createSharedEntry() is deprecated');

expect(() => {
config.createSharedEntry('vendor2', './main');
Expand Down Expand Up @@ -606,7 +607,7 @@ describe('WebpackConfig object', () => {

it('Calling with "includeNodeModules" option', () => {
const config = createConfig();
config.configureBabel(() => {}, { include_node_modules: ['foo', 'bar'] });
config.configureBabel(() => {}, { includeNodeModules: ['foo', 'bar'] });

expect(config.babelOptions.exclude).to.be.a('Function');

Expand Down Expand Up @@ -662,7 +663,6 @@ describe('WebpackConfig object', () => {
const config = createConfig();
config.runtimeConfig.babelRcFileExists = true;
config.configureBabel(null, { includeNodeModules: ['foo'] });
loggerAssert.assertWarning('TODO');
});

it('Calling with a non-whitelisted option when .babelrc is present displays a warning', () => {
Expand Down Expand Up @@ -1322,6 +1322,7 @@ describe('WebpackConfig object', () => {

config.configureLoaderRule('eslint', callback);
expect(config.loaderConfigurationCallbacks['eslint']).to.equal(callback);
loggerAssert.assertWarning('Be careful when using Encore.configureLoaderRule');
});

it('Call method with a not supported loader', () => {
Expand All @@ -1330,6 +1331,7 @@ describe('WebpackConfig object', () => {
expect(() => {
config.configureLoaderRule('reason');
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "css", "images", "fonts", "sass", "less", "stylus", "vue", "eslint", "typescript", "handlebars" and the aliases "js", "ts", "scss".');
loggerAssert.assertWarning('Be careful when using Encore.configureLoaderRule');
});

it('Call method with not a valid callback', () => {
Expand All @@ -1338,10 +1340,12 @@ describe('WebpackConfig object', () => {
expect(() => {
config.configureLoaderRule('eslint');
}).to.throw('Argument 2 to configureLoaderRule() must be a callback function.');
loggerAssert.assertWarning('Be careful when using Encore.configureLoaderRule');

expect(() => {
config.configureLoaderRule('eslint', {});
}).to.throw('Argument 2 to configureLoaderRule() must be a callback function.');
loggerAssert.assertWarning('Be careful when using Encore.configureLoaderRule');
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/_unsilencedLogsCheck.js
Expand Up @@ -17,7 +17,7 @@ beforeEach(function() {

afterEach(function() {
if (logger.getDeprecations().length > 0) {
this.test.error(new Error(`There were ${logger.getWarnings().length} unexpected deprecation log messages: \n${logger.getDeprecations().join('\n')}`));
this.test.error(new Error(`There were ${logger.getDeprecations().length} unexpected deprecation log messages: \n${logger.getDeprecations().join('\n')}`));
}

if (logger.getWarnings().length > 0) {
Expand Down
18 changes: 14 additions & 4 deletions test/config-generator.js
Expand Up @@ -22,7 +22,7 @@ const loggerAssert = require('./helpers/logger-assert');

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

function createConfig(runtimeConfig = null) {
function createConfig(runtimeConfig = null, disableSingleRuntimeChunk = true) {
runtimeConfig = runtimeConfig ? runtimeConfig : new RuntimeConfig();

if (null === runtimeConfig.context) {
Expand All @@ -37,7 +37,12 @@ function createConfig(runtimeConfig = null) {
runtimeConfig.babelRcFileExists = false;
}

return new WebpackConfig(runtimeConfig);
const config = new WebpackConfig(runtimeConfig);
if (disableSingleRuntimeChunk) {
config.disableSingleRuntimeChunk();
}

return config;
}

function findPlugin(pluginConstructor, plugins) {
Expand Down Expand Up @@ -167,6 +172,7 @@ describe('The config-generator function', () => {
// pretend we're installed to a subdirectory
config.setPublicPath('/subdirectory/build');
config.setManifestKeyPrefix('/build');
loggerAssert.assertWarning('The value passed to setManifestKeyPrefix "/build" starts with "/"');

const actualConfig = configGenerator(config);

Expand Down Expand Up @@ -1085,7 +1091,7 @@ describe('The config-generator function', () => {
});

it('Not set + createSharedEntry()', () => {
const config = createConfig();
const config = createConfig(null, false);
config.outputPath = '/tmp/public/build';
config.setPublicPath('/build/');
config.createSharedEntry('foo', 'bar.js');
Expand All @@ -1096,7 +1102,7 @@ describe('The config-generator function', () => {
});

it('Not set without createSharedEntry()', () => {
const config = createConfig();
const config = createConfig(null, false);
config.outputPath = '/tmp/public/build';
config.setPublicPath('/build/');

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

afterEach(function() {
loggerAssert.assertWarning('Be careful when using Encore.configureLoaderRule');
});

it('configure rule for "javascript"', () => {
config.configureLoaderRule('javascript', (loaderRule) => {
loaderRule.test = /\.m?js$/;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/logger-assert.js
Expand Up @@ -21,7 +21,7 @@ function assertDeprecation(expectedMessage) {

function assertLogMessage(messages, description, expectedMessage) {
if (messages.length === 0) {
throw new Error(`Found zero log ${description}s. And so, expected ${description} ${expectedMessage} was not logged.`);
throw new Error(`Found zero log ${description}s. And so, expected "${description} ${expectedMessage}" was not logged.`);
}

let isFound = false;
Expand Down

0 comments on commit 7dd33d6

Please sign in to comment.