From 7dd33d6035a373c8a5d6400e157a79cb98e2ae13 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 11 May 2020 06:25:57 -0400 Subject: [PATCH] WIP adding asserts for warnings/deprecations --- lib/WebpackConfig.js | 4 ++-- test/WebpackConfig.js | 12 ++++++++---- test/_unsilencedLogsCheck.js | 2 +- test/config-generator.js | 18 ++++++++++++++---- test/helpers/logger-assert.js | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/WebpackConfig.js b/lib/WebpackConfig.js index 131db1e1..34b84873 100644 --- a/lib/WebpackConfig.js +++ b/lib/WebpackConfig.js @@ -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.'); } @@ -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.'); } diff --git a/test/WebpackConfig.js b/test/WebpackConfig.js index d641731e..3c78e4cd 100644 --- a/test/WebpackConfig.js +++ b/test/WebpackConfig.js @@ -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'); }); }); @@ -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 "/"'); }); }); @@ -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'); @@ -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'); @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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'); }); }); diff --git a/test/_unsilencedLogsCheck.js b/test/_unsilencedLogsCheck.js index ccb71d50..3af3a247 100644 --- a/test/_unsilencedLogsCheck.js +++ b/test/_unsilencedLogsCheck.js @@ -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) { diff --git a/test/config-generator.js b/test/config-generator.js index ac005177..85337ce7 100644 --- a/test/config-generator.js +++ b/test/config-generator.js @@ -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) { @@ -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) { @@ -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); @@ -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'); @@ -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/'); @@ -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$/; diff --git a/test/helpers/logger-assert.js b/test/helpers/logger-assert.js index c2949a4a..ce456f90 100644 --- a/test/helpers/logger-assert.js +++ b/test/helpers/logger-assert.js @@ -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;