Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: loader's tests for errors and warnings #1736

Merged
merged 4 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ packages/**/*.map

# temporary test files
test-assets/
./lib/
6 changes: 3 additions & 3 deletions packages/webpack-cli/lib/utils/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Compiler {
logger.error(err.stack || err);
process.exit(1); // eslint-disable-line
}
if (!outputOptions.watch && (stats.hasErrors() || stats.hasWarnings())) {
process.exitCode = 1;
}
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
if (outputOptions.json) {
process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + '\n');
} else if (stats.hash !== lastHash) {
Expand All @@ -93,9 +96,6 @@ class Compiler {
}
return this.generateOutput(outputOptions, stats, statsErrors, processingMessageBuffer);
}
if (!outputOptions.watch && stats.hasErrors()) {
process.exitCode = 2;
}
}
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

async invokeCompilerInstance(lastHash, options, outputOptions, processingMessageBuffer) {
Expand Down
15 changes: 15 additions & 0 deletions test/loader/error-test/loader-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

// eslint-disable-next-line node/no-unpublished-require
const { run } = require('../../utils/test-utils');

describe('loader error regression test for #1581', () => {
it(`should not ignore loader's error produce a failing build`, () => {
// Ignoring assertion on stderr because ts-loader is producing depreciation warnings
// with [email protected] -> https://github.com/TypeStrong/ts-loader/issues/1169
const { stdout, exitCode } = run(__dirname, [], false);
expect(exitCode).not.toEqual(0);
expect(stdout).toContain('[1 error]');
expect(stdout).toContain(`Cannot assign to 'foobar' because it is a constant`);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
});
});
6 changes: 6 additions & 0 deletions test/loader/error-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"ts-loader": "^7.0.5",
"typescript": "^3.9.3"
}
}
4 changes: 4 additions & 0 deletions test/loader/error-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const foobar = 'foobar';
// eslint-disable-next-line no-const-assign
foobar = 'barbaz'; // Error!
console.log(foobar);
25 changes: 25 additions & 0 deletions test/loader/error-test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');

module.exports = {
mode: 'development',

entry: {
bundle: './src/index.ts',
},

output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},

module: {
rules: [
{
test: /.(ts|tsx)?$/,
loader: 'ts-loader',
include: [path.resolve(__dirname, 'src')],
exclude: [/node_modules/],
},
],
},
};
13 changes: 13 additions & 0 deletions test/loader/warning-test/loader-warning.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const { run } = require('../../utils/test-utils');

describe('loader warning test', () => {
it(`should not ignore loader's warning and exit with a non zero exit code`, () => {
const { stdout, exitCode } = run(__dirname, [], false);

expect(stdout).toContain('[1 warning]');
expect(stdout).toContain('This is a warning');
expect(exitCode).not.toEqual(0);
});
});
5 changes: 5 additions & 0 deletions test/loader/warning-test/my-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function loader(source) {
const { emitWarning } = this;
emitWarning('This is a warning');
return source;
};
2 changes: 2 additions & 0 deletions test/loader/warning-test/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('../my-loader');
console.log('loader warning test');
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 30 additions & 0 deletions test/loader/warning-test/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const path = require('path');

module.exports = {
mode: 'development',

entry: {
bundle: './src/main.js',
},

output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},

module: {
rules: [
{
test: /.(js|jsx)?$/,
loader: 'my-loader',
include: [path.resolve(__dirname, 'src')],
exclude: [/node_modules/],
},
],
},
resolveLoader: {
alias: {
'my-loader': require.resolve('./my-loader'),
},
},
};