Skip to content

Commit

Permalink
tests: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 21, 2021
1 parent 3548643 commit 8c63e4d
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/serve/src/index.ts
Expand Up @@ -73,7 +73,7 @@ class ServeCommand {
}

if (entries.length > 0) {
webpackOptions.entry = entries;
webpackOptions.entry = [...entries, ...(webpackOptions.entry || [])];
}

webpackOptions.argv = { ...options, env: { WEBPACK_SERVE: true, ...options.env } };
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -343,7 +343,7 @@ class WebpackCLI {
this.getBuiltInOptions(),
async (entries, options) => {
if (entries.length > 0) {
options.entry = entries;
options.entry = [...entries, ...(options.entry || [])];
}

if (isWatchCommandUsed) {
Expand Down
16 changes: 16 additions & 0 deletions test/build/basic/basic.test.js
Expand Up @@ -43,6 +43,22 @@ describe('bundle command', () => {
expect(stdout).toBeTruthy();
});

it('should work with multiple entries syntax without command with options #3 (default command)', async () => {
const { exitCode, stderr, stdout } = run(__dirname, ['./src/index.js', './src/other.js', '--entry', './src/again.js'], false);

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

it('should work with and override entries from the configuration', async () => {
const { exitCode, stderr, stdout } = run(__dirname, ['./src/index.js', './src/other.js', '--config', './entry.config.js'], false);

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

it('should work with the "build" alias', async () => {
const { exitCode, stderr, stdout } = run(__dirname, ['build'], false);

Expand Down
4 changes: 4 additions & 0 deletions test/build/basic/entry.config.js
@@ -0,0 +1,4 @@
module.exports = {
mode: 'development',
entry: './src/entry.js',
};
1 change: 1 addition & 0 deletions test/build/basic/src/again.js
@@ -0,0 +1 @@
console.log('again');
1 change: 1 addition & 0 deletions test/build/basic/src/entry.js
@@ -0,0 +1 @@
console.log('CONFIG');
36 changes: 18 additions & 18 deletions test/serve/basic/serve-basic.test.js
Expand Up @@ -28,15 +28,15 @@ describe('basic serve usage', () => {
}

it('should work', async () => {
const { stderr, stdout } = await runServe(__dirname, ['']);
const { stderr, stdout } = await runServe(__dirname, []);

expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should work with the "--config" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'webpack.config.js', '--port', port]);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'serve.config.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
Expand All @@ -45,7 +45,6 @@ describe('basic serve usage', () => {

it('should work with the "--config" and "--env" options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'serve',
'--config',
'function-with-env.config.js',
'--env',
Expand All @@ -63,7 +62,6 @@ describe('basic serve usage', () => {

it('should work with the "--config" and "--env" options and expose dev server options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'serve',
'--config',
'function-with-argv.config.js',
'--env',
Expand All @@ -82,7 +80,7 @@ describe('basic serve usage', () => {
});

it('should work in multi compiler mode', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi.config.js', '--port', port]);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi.config.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
Expand All @@ -94,7 +92,7 @@ describe('basic serve usage', () => {

// TODO need fix in future, edge case
it.skip('should work in multi compiler mode with multiple dev servers', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi-dev-server.config.js']);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server.config.js']);

expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
Expand Down Expand Up @@ -228,7 +226,7 @@ describe('basic serve usage', () => {
});

it('should work with the default "publicPath" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve']);
const { stderr, stdout } = await runServe(__dirname, []);

expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
Expand All @@ -237,7 +235,7 @@ describe('basic serve usage', () => {
});

it('should work with the "--output-public-path" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--output-public-path', '/my-public-path/']);
const { stderr, stdout } = await runServe(__dirname, ['--output-public-path', '/my-public-path/']);

if (isWebpack5) {
expect(stderr).toBeFalsy();
Expand All @@ -251,7 +249,7 @@ describe('basic serve usage', () => {
});

it('should respect the "publicPath" option from configuration', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'output-public-path.config.js']);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'output-public-path.config.js']);

expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
Expand All @@ -260,7 +258,7 @@ describe('basic serve usage', () => {
});

it('should respect the "publicPath" option from configuration using multi compiler mode', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi-output-public-path.config.js', '--port', port]);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-output-public-path.config.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('one');
Expand All @@ -272,7 +270,7 @@ describe('basic serve usage', () => {
});

it('should respect the "publicPath" option from configuration (from the "devServer" options)', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'dev-server-output-public-path.config.js']);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'dev-server-output-public-path.config.js']);

expect(stderr).toBeFalsy();
expect(stdout).toContain('main.js');
Expand All @@ -289,13 +287,7 @@ describe('basic serve usage', () => {
});

it('should respect the "publicPath" option from configuration using multi compiler mode (from the "devServer" options)', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'serve',
'--config',
'multi-dev-server-output-public-path.config.js',
'--port',
port,
]);
const { stderr, stdout } = await runServe(__dirname, ['--config', 'multi-dev-server-output-public-path.config.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stderr).toBeFalsy();
Expand All @@ -307,6 +299,14 @@ describe('basic serve usage', () => {
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should work with entries syntax', async () => {
const { stderr, stdout } = await runServe(__dirname, ['./src/entry.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should log and error on unknown flag', async () => {
const { exitCode, stdout, stderr } = await runServe(testPath, ['--port', port, '--unknown-flag']);

Expand Down
7 changes: 7 additions & 0 deletions test/serve/basic/serve.config.js
@@ -0,0 +1,7 @@
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');

module.exports = {
mode: 'development',
devtool: false,
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
};
1 change: 1 addition & 0 deletions test/serve/basic/src/entry.js
@@ -0,0 +1 @@
console.log('Entry');
35 changes: 35 additions & 0 deletions test/watch/basic/basic.test.js
Expand Up @@ -83,6 +83,41 @@ describe('basic', () => {
});
});

it('should recompile upon file change using the `watch` command and entries syntax', (done) => {
const proc = runAndGetWatchProc(__dirname, ['watch', './src/entry.js', '--mode', 'development'], false, '', true);

let modified = false;

const wordsInStatsv5Entries = ['asset', 'entry.js', 'compiled successfully'];

proc.stdout.on('data', (chunk) => {
const data = stripAnsi(chunk.toString());

if (data.includes('entry.js')) {
if (isWebpack5) {
for (const word of wordsInStatsv5Entries) {
expect(data).toContain(word);
}
} else {
for (const word of wordsInStatsv4) {
expect(data).toContain(word);
}
}

if (!modified) {
process.nextTick(() => {
writeFileSync(resolve(__dirname, './src/entry.js'), `console.log('watch flag test');`);
});

modified = true;
} else {
proc.kill();
done();
}
}
});
});

it('should recompile upon file change using the `command` option and the `--watch` option and log warning', (done) => {
const proc = runAndGetWatchProc(__dirname, ['watch', '--watch', '--mode', 'development'], false, '', true);

Expand Down
1 change: 1 addition & 0 deletions test/watch/basic/src/entry.js
@@ -0,0 +1 @@
console.log('watch flag test');

0 comments on commit 8c63e4d

Please sign in to comment.