Skip to content

Commit

Permalink
tests: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Feb 1, 2021
1 parent 7ce95cf commit 40650c0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -38,7 +38,7 @@
"pretest": "yarn build && yarn lint && yarn prepsuite",
"test": "jest --reporters=default",
"test:smoketests": "nyc node smoketests",
"test:coverage": "nyc --no-hook-require jest --forceExit",
"test:coverage": "nyc jest --forceExit",
"test:cli": "jest test --reporters=default --forceExit",
"test:packages": "jest packages/ --reporters=default --forceExit",
"test:ci": "yarn test:cli && yarn test:packages",
Expand Down
6 changes: 5 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -1160,7 +1160,11 @@ class WebpackCLI {
Module.prototype._compile = previousModuleCompile;
}

if (error.code === 'ERR_REQUIRE_ESM' && pathToFileURL && dynamicImportLoader) {
if (
(error.code === 'ERR_REQUIRE_ESM' || process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) &&
pathToFileURL &&
dynamicImportLoader
) {
const urlForConfig = pathToFileURL(configPath);

options = await dynamicImportLoader(urlForConfig);
Expand Down
6 changes: 3 additions & 3 deletions test/config-format/mjs/mjs.test.js
Expand Up @@ -2,14 +2,14 @@ const { run } = require('../../utils/test-utils');

describe('webpack cli', () => {
it('should support mjs config format', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']);
const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], {
env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true },
});

if (/Unexpected token/.test(stderr)) {
expect(exitCode).toBe(2);
expect(stdout).toBeFalsy();
} else {
console.log(stderr);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
Expand Down
6 changes: 4 additions & 2 deletions test/config-lookup/custom-name/custom-name.test.js
Expand Up @@ -5,15 +5,17 @@ const { run } = require('../../utils/test-utils');

describe('custom config file', () => {
it('should work with cjs format', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')], false);
const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});

it('should work with esm format', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], false);
const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], {
env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true },
});

if (/Unexpected token/.test(stderr)) {
expect(exitCode).toBe(2);
Expand Down
2 changes: 1 addition & 1 deletion test/config/defaults/mjs-config/default-mjs-config.test.js
Expand Up @@ -4,7 +4,7 @@ const { run, isWebpack5 } = require('../../../utils/test-utils');

describe('Default Config:', () => {
it('Should be able to pick mjs config by default', () => {
const { exitCode, stderr, stdout } = run(__dirname, []);
const { exitCode, stderr, stdout } = run(__dirname, [], { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true } });

if (/Unexpected token/.test(stderr)) {
expect(exitCode).toEqual(2);
Expand Down
8 changes: 6 additions & 2 deletions test/config/error-mjs/config-error.test.js
Expand Up @@ -4,7 +4,9 @@ const { run } = require('../../utils/test-utils');

describe('config error', () => {
it('should throw error with invalid configuration', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')]);
const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')], {
env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true },
});

expect(exitCode).toBe(2);

Expand All @@ -17,7 +19,9 @@ describe('config error', () => {
});

it('should throw syntax error and exit with non-zero exit code', () => {
const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')]);
const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')], {
env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true },
});

expect(exitCode).toBe(2);
expect(stderr).toContain('SyntaxError: Unexpected token');
Expand Down

0 comments on commit 40650c0

Please sign in to comment.