6 | }
7 | }
--------------------------------------------------------------------------------
/test/fixtures/babel-syntax-babel-6/webpack.config.js:
--------------------------------------------------------------------------------
1 | const FriendlyErrorsWebpackPlugin = require('../../../index');
2 |
3 | module.exports = {
4 | mode: 'development',
5 | entry: __dirname + "/index.js",
6 | output: {
7 | path: __dirname + "/dist",
8 | filename: "bundle.js"
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin()
12 | ],
13 | module: {
14 | rules: [
15 | {
16 | test: /\.jsx?$/,
17 | exclude: /node_modules/,
18 | loader: require.resolve('babel-loader-7'),
19 | }
20 | ]
21 | },
22 | };
--------------------------------------------------------------------------------
/test/fixtures/babel-syntax-babel-7/.babelrc:
--------------------------------------------------------------------------------
1 | {}
--------------------------------------------------------------------------------
/test/fixtures/babel-syntax-babel-7/index.js:
--------------------------------------------------------------------------------
1 |
2 | class MyComponent extends React.Component {
3 |
4 | render() {
5 | return
6 | }
7 | }
--------------------------------------------------------------------------------
/test/fixtures/babel-syntax-babel-7/webpack.config.js:
--------------------------------------------------------------------------------
1 | const FriendlyErrorsWebpackPlugin = require('../../../index');
2 |
3 | module.exports = {
4 | mode: 'development',
5 | entry: __dirname + "/index.js",
6 | output: {
7 | path: __dirname + "/dist",
8 | filename: "bundle.js"
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin()
12 | ],
13 | module: {
14 | rules: [
15 | {
16 | test: /\.jsx?$/,
17 | exclude: /node_modules/,
18 | loader: require.resolve('babel-loader'),
19 | }
20 | ]
21 | },
22 | };
23 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-warnings/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "parser": "babel-eslint",
4 | "env": {
5 | "browser": true,
6 | "commonjs": true,
7 | "es6": true,
8 | "jest": true,
9 | "node": true
10 | },
11 | "parserOptions": {
12 | "ecmaVersion": 6,
13 | "sourceType": "module"
14 | },
15 | "rules": {
16 | // http://eslint.org/docs/rules/
17 | "no-unused-expressions": "warn",
18 | "no-unused-labels": "warn",
19 | "no-unused-vars": ["warn", { "vars": "local", "args": "none" }]
20 | }
21 | }
--------------------------------------------------------------------------------
/test/fixtures/eslint-warnings/index.js:
--------------------------------------------------------------------------------
1 | require('./module');
2 |
3 | const unused = 'I am unused';
4 | const unused2 = 'I am unused too';
5 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-warnings/module.js:
--------------------------------------------------------------------------------
1 | const unused = 'I am unused';
2 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-warnings/webpack.config.js:
--------------------------------------------------------------------------------
1 | const FriendlyErrorsWebpackPlugin = require('../../../index');
2 |
3 | module.exports = {
4 | mode: 'development',
5 | entry: __dirname + "/index.js",
6 | output: {
7 | path: __dirname + "/dist",
8 | filename: "bundle.js"
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin(),
12 |
13 | ],
14 | module: {
15 | rules: [
16 | {
17 | enforce: 'pre',
18 | test: /\.js$/,
19 | loader: 'eslint-loader',
20 | include: __dirname,
21 | },
22 | {
23 | test: /\.jsx?$/,
24 | exclude: /node_modules/,
25 | loader: require.resolve('babel-loader-7'),
26 | }
27 | ]
28 | },
29 | };
30 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-webpack-plugin-warnings/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "parser": "babel-eslint",
4 | "env": {
5 | "browser": true,
6 | "commonjs": true,
7 | "es6": true,
8 | "jest": true,
9 | "node": true
10 | },
11 | "parserOptions": {
12 | "ecmaVersion": 6,
13 | "sourceType": "module"
14 | },
15 | "rules": {
16 | // http://eslint.org/docs/rules/
17 | "no-unused-expressions": "warn",
18 | "no-unused-labels": "warn",
19 | "no-unused-vars": ["warn", { "vars": "local", "args": "none" }]
20 | }
21 | }
--------------------------------------------------------------------------------
/test/fixtures/eslint-webpack-plugin-warnings/index.js:
--------------------------------------------------------------------------------
1 | require('./module');
2 |
3 | const unused = 'I am unused';
4 | const unused2 = 'I am unused too';
5 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-webpack-plugin-warnings/module.js:
--------------------------------------------------------------------------------
1 | const unused = 'I am unused';
2 |
--------------------------------------------------------------------------------
/test/fixtures/eslint-webpack-plugin-warnings/webpack.config.js:
--------------------------------------------------------------------------------
1 | const FriendlyErrorsWebpackPlugin = require('../../../index');
2 | const ESLintPlugin = require('eslint-webpack-plugin');
3 |
4 | module.exports = {
5 | mode: 'development',
6 | entry: __dirname + "/index.js",
7 | output: {
8 | path: __dirname + "/dist",
9 | filename: "bundle.js"
10 | },
11 | plugins: [
12 | new FriendlyErrorsWebpackPlugin(),
13 | new ESLintPlugin({
14 | eslintPath: require.resolve('eslint-7')
15 | })
16 | ],
17 | module: {
18 | rules: [
19 | {
20 | test: /\.jsx?$/,
21 | exclude: /node_modules/,
22 | loader: 'babel-loader',
23 | }
24 | ]
25 | },
26 | };
27 |
--------------------------------------------------------------------------------
/test/fixtures/mini-css-extract-babel-syntax/index.scss:
--------------------------------------------------------------------------------
1 | body {
2 | color: red;
3 |
4 | .container {
5 | margin-top: 100px
6 |
7 | .test {
8 | vertical-align: middle;
9 | }
10 | }
11 | }
--------------------------------------------------------------------------------
/test/fixtures/mini-css-extract-babel-syntax/webpack.config.js:
--------------------------------------------------------------------------------
1 | // mini-css-extract-plugin test
2 | const FriendlyErrorsWebpackPlugin = require('../../../index');
3 | const MiniCssExtractPlugin = require('mini-css-extract-plugin');
4 |
5 | module.exports = {
6 | entry: __dirname + "/index.scss",
7 | output: {
8 | path: __dirname + "/dist",
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin(),
12 | new MiniCssExtractPlugin({
13 | filename: '[name].css',
14 | chunkFilename: '[id].css',
15 | }),
16 | ],
17 | module: {
18 | rules: [
19 | {
20 | test: /\.(s*)css$/,
21 | exclude: /node_modules/,
22 | use: [
23 | MiniCssExtractPlugin.loader,
24 | 'style-loader',
25 | 'css-loader',
26 | {
27 | loader: 'sass-loader',
28 | options: {
29 | implementation: require('sass'),
30 | }
31 | },
32 | ],
33 | }
34 | ]
35 | },
36 | };
--------------------------------------------------------------------------------
/test/fixtures/module-errors/index.js:
--------------------------------------------------------------------------------
1 | require('not-found');
2 | require('./non-existing');
3 | require('../non-existing');
4 |
--------------------------------------------------------------------------------
/test/fixtures/module-errors/webpack.config.js:
--------------------------------------------------------------------------------
1 |
2 | const FriendlyErrorsWebpackPlugin = require('../../../index');
3 |
4 | module.exports = {
5 | entry: __dirname + "/index.js",
6 | output: {
7 | path: __dirname + "/dist",
8 | filename: "bundle.js"
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin()
12 | ]
13 | };
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-module-errors/index.js:
--------------------------------------------------------------------------------
1 | require('./non-existing');
2 |
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-module-errors/index2.js:
--------------------------------------------------------------------------------
1 | require('not-found');
2 |
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-module-errors/webpack.config.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | {
3 | entry: __dirname + "/index.js",
4 | output: {
5 | path: __dirname + "/dist",
6 | filename: "bundle.js"
7 | }
8 | },
9 | {
10 | entry: __dirname + "/index2.js",
11 | output: {
12 | path: __dirname + "/dist",
13 | filename: "bundle2.js"
14 | }
15 | }
16 | ];
17 |
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-success/index.js:
--------------------------------------------------------------------------------
1 | module.exports = 'I am an entry point';
2 |
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-success/index2.js:
--------------------------------------------------------------------------------
1 | module.exports = 'I am another entry point';
2 |
--------------------------------------------------------------------------------
/test/fixtures/multi-compiler-success/webpack.config.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | {
3 | mode: 'development',
4 | entry: __dirname + "/index.js",
5 | output: {
6 | path: __dirname + "/dist",
7 | filename: "bundle.js"
8 | }
9 | },
10 | {
11 | mode: 'development',
12 | entry: __dirname + "/index2.js",
13 | output: {
14 | path: __dirname + "/dist",
15 | filename: "bundle2.js"
16 | }
17 | }
18 | ];
19 |
--------------------------------------------------------------------------------
/test/fixtures/multi-postcss-warnings/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | display: grid;
3 | grid-gap: 1px;
4 | }
--------------------------------------------------------------------------------
/test/fixtures/multi-postcss-warnings/index2.css:
--------------------------------------------------------------------------------
1 | body {
2 | display: grid;
3 | grid-auto-flow: row;
4 | }
--------------------------------------------------------------------------------
/test/fixtures/multi-postcss-warnings/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | autoprefixer: {
4 | grid: true,
5 | }
6 | }
7 | };
--------------------------------------------------------------------------------
/test/fixtures/multi-postcss-warnings/webpack.config.js:
--------------------------------------------------------------------------------
1 | // postcss-loader warnings test (multi-compiler version)
2 | const FriendlyErrorsWebpackPlugin = require('../../../index');
3 |
4 | const COMMON_CONFIG = {
5 | mode: 'production',
6 | output: {
7 | path: __dirname + '/dist',
8 | },
9 | plugins: [
10 | new FriendlyErrorsWebpackPlugin(),
11 | ],
12 | module: {
13 | rules: [
14 | {
15 | test: /\.css$/,
16 | exclude: /node_modules/,
17 | use: [
18 | {
19 | loader: 'css-loader',
20 | options: {
21 | importLoaders: 1,
22 | },
23 | },
24 | {
25 | loader: 'postcss-loader',
26 | options: {
27 | config: {
28 | path: __dirname + '/postcss.config.js'
29 | }
30 | },
31 | },
32 | ],
33 | }
34 | ]
35 | },
36 | };
37 |
38 | module.exports = [
39 | Object.assign({}, { entry: __dirname + '/index.css' }, COMMON_CONFIG),
40 | Object.assign({}, { entry: __dirname + '/index2.css' }, COMMON_CONFIG),
41 | ];
--------------------------------------------------------------------------------
/test/fixtures/postcss-warnings/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | display: grid;
3 | grid-gap: 1px;
4 | }
--------------------------------------------------------------------------------
/test/fixtures/postcss-warnings/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | autoprefixer: {
4 | grid: true,
5 | }
6 | }
7 | };
--------------------------------------------------------------------------------
/test/fixtures/postcss-warnings/webpack.config.js:
--------------------------------------------------------------------------------
1 | // postcss-loader warnings test
2 | const FriendlyErrorsWebpackPlugin = require('../../../index');
3 |
4 | module.exports = {
5 | mode: 'production',
6 | entry: __dirname + '/index.css',
7 | output: {
8 | path: __dirname + '/dist',
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin(),
12 | ],
13 | module: {
14 | rules: [
15 | {
16 | test: /\.css$/,
17 | exclude: /node_modules/,
18 | use: [
19 | {
20 | loader: 'css-loader',
21 | options: {
22 | importLoaders: 1,
23 | },
24 | },
25 | {
26 | loader: 'postcss-loader',
27 | options: {
28 | config: {
29 | path: __dirname + '/postcss.config.js'
30 | }
31 | },
32 | },
33 | ],
34 | }
35 | ]
36 | },
37 | };
--------------------------------------------------------------------------------
/test/fixtures/success/index.js:
--------------------------------------------------------------------------------
1 | module.exports = 'I am an entry point';
2 |
--------------------------------------------------------------------------------
/test/fixtures/success/webpack.config.js:
--------------------------------------------------------------------------------
1 | const FriendlyErrorsWebpackPlugin = require('../../../index');
2 |
3 | module.exports = {
4 | mode: 'development',
5 | entry: __dirname + "/index.js",
6 | output: {
7 | path: __dirname + "/dist",
8 | filename: "bundle.js"
9 | },
10 | plugins: [
11 | new FriendlyErrorsWebpackPlugin()
12 | ]
13 | };
14 |
--------------------------------------------------------------------------------
/test/integration.spec.js:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | const output = require('../src/output');
4 | const webpack = require('webpack');
5 | const FriendlyErrorsWebpackPlugin = require('../src/friendly-errors-plugin');
6 | const MemoryFileSystem = require('memory-fs');
7 | const path = require('path');
8 |
9 | const webpackPromise = function (config, globalPlugins) {
10 | const compiler = webpack(config);
11 | compiler.outputFileSystem = new MemoryFileSystem();
12 | if (Array.isArray(globalPlugins)) {
13 | globalPlugins.forEach(p => p.apply(compiler));
14 | }
15 |
16 | return new Promise((resolve, reject) => {
17 | compiler.run(err => {
18 | if (err) {
19 | reject(err)
20 | }
21 | resolve()
22 | });
23 | });
24 | };
25 |
26 | async function executeAndGetLogs(fixture, globalPlugins) {
27 | try {
28 | output.capture();
29 | await webpackPromise(require(fixture), globalPlugins);
30 | return output.capturedMessages;
31 | } finally {
32 | output.endCapture()
33 | }
34 | }
35 |
36 | it('integration : success', async() => {
37 |
38 | const logs = await executeAndGetLogs('./fixtures/success/webpack.config')
39 |
40 | expect(logs.join('\n')).toMatch(/DONE {2}Compiled successfully in (.\d*)ms/);
41 | });
42 |
43 | it('integration : module-errors', async() => {
44 |
45 | const logs = await executeAndGetLogs('./fixtures/module-errors/webpack.config.js');
46 |
47 | expect(logs).toEqual([
48 | 'ERROR Failed to compile with 3 errors',
49 | '',
50 | 'This dependency was not found:',
51 | '',
52 | '* not-found in ./test/fixtures/module-errors/index.js',
53 | '',
54 | 'To install it, you can run: npm install --save not-found',
55 | '',
56 | '',
57 | 'These relative modules were not found:',
58 | '',
59 | '* ../non-existing in ./test/fixtures/module-errors/index.js',
60 | '* ./non-existing in ./test/fixtures/module-errors/index.js',
61 | ]);
62 | });
63 |
64 | function filename(filePath) {
65 | return path.join(__dirname, path.normalize(filePath))
66 | }
67 |
68 | it('integration : should display eslint warnings', async() => {
69 |
70 | const logs = await executeAndGetLogs('./fixtures/eslint-warnings/webpack.config.js');
71 |
72 | expect(logs.join('\n')).toEqual(
73 | `WARNING Compiled with 2 warnings
74 |
75 | Module Warning (from ./node_modules/eslint-loader/index.js):
76 |
77 | ${filename('fixtures/eslint-warnings/index.js')}
78 | 3:7 warning 'unused' is assigned a value but never used no-unused-vars
79 | 4:7 warning 'unused2' is assigned a value but never used no-unused-vars
80 |
81 | ✖ 2 problems (0 errors, 2 warnings)
82 |
83 | Module Warning (from ./node_modules/eslint-loader/index.js):
84 |
85 | ${filename('fixtures/eslint-warnings/module.js')}
86 | 1:7 warning 'unused' is assigned a value but never used no-unused-vars
87 |
88 | ✖ 1 problem (0 errors, 1 warning)
89 |
90 | You may use special comments to disable some warnings.
91 | Use // eslint-disable-next-line to ignore the next line.
92 | Use /* eslint-disable */ to ignore all warnings in a file.`
93 | )
94 | });
95 |
96 | it('integration : should display eslint-webpack-plugin warnings', async() => {
97 |
98 | const logs = await executeAndGetLogs('./fixtures/eslint-webpack-plugin-warnings/webpack.config.js');
99 |
100 | expect(logs.join('\n')).toEqual(
101 | `WARNING Compiled with 1 warning
102 |
103 | ${filename('fixtures/eslint-webpack-plugin-warnings/index.js')}
104 | 3:7 warning 'unused' is assigned a value but never used no-unused-vars
105 | 4:7 warning 'unused2' is assigned a value but never used no-unused-vars
106 |
107 | ${filename('fixtures/eslint-webpack-plugin-warnings/module.js')}
108 | 1:7 warning 'unused' is assigned a value but never used no-unused-vars
109 |
110 | ✖ 3 problems (0 errors, 3 warnings)
111 |
112 | You may use special comments to disable some warnings.
113 | Use // eslint-disable-next-line to ignore the next line.
114 | Use /* eslint-disable */ to ignore all warnings in a file.`
115 | )
116 | });
117 |
118 | it('integration : babel syntax error with babel-loader 7 (babel 6)', async() => {
119 |
120 | const logs = await executeAndGetLogs('./fixtures/babel-syntax-babel-6/webpack.config');
121 |
122 | expect(logs).toEqual([
123 | 'ERROR Failed to compile with 1 error',
124 | '',
125 | 'error in ./test/fixtures/babel-syntax-babel-6/index.js',
126 | '',
127 | `Syntax Error: Unexpected token (5:11)
128 |
129 | 3 |${' '}
130 | 4 | render() {
131 | > 5 | return
132 | | ^
133 | 6 | }
134 | 7 | }`,
135 | ''
136 | ]);
137 | });
138 | it('integration : babel syntax error with babel-loader 8 (babel 7)', async() => {
139 |
140 | const logs = await executeAndGetLogs('./fixtures/babel-syntax-babel-7/webpack.config');
141 |
142 | expect(logs).toEqual([
143 | 'ERROR Failed to compile with 1 error',
144 | '',
145 | 'error in ./test/fixtures/babel-syntax-babel-7/index.js',
146 | '',
147 | `Syntax Error: Unexpected token (5:11)
148 |
149 | 3 |${' '}
150 | 4 | render() {
151 | > 5 | return
152 | | ^
153 | 6 | }
154 | 7 | }`,
155 | ''
156 | ]);
157 | });
158 |
159 | it('integration : mini CSS extract plugin babel error', async() => {
160 |
161 | const logs = await executeAndGetLogs('./fixtures/mini-css-extract-babel-syntax/webpack.config');
162 | const clean_logs = logs.toString().replace(/\"/g, ""); //<- double quotes issue with slash
163 | expect(clean_logs).toEqual(
164 | `ERROR Failed to compile with 1 error,,error in ./test/fixtures/mini-css-extract-babel-syntax/index.scss,,.test {
165 | ^
166 | Expected digit.
167 | ╷
168 | 7 │ .test {
169 | │ ^
170 | ╵
171 | stdin 7:4 root stylesheet
172 | in ${filename('fixtures/mini-css-extract-babel-syntax/index.scss')} (line 7, column 4),`
173 | );
174 | });
175 |
176 | it('integration : webpack multi compiler : success', async() => {
177 |
178 | // We apply the plugin directly to the compiler when targeting multi-compiler
179 | let globalPlugins = [new FriendlyErrorsWebpackPlugin()];
180 | const logs = await executeAndGetLogs('./fixtures/multi-compiler-success/webpack.config', globalPlugins);
181 |
182 | expect(logs.join('\n')).toMatch(/DONE {2}Compiled successfully in (.\d*)ms/)
183 | });
184 |
185 | it('integration : webpack multi compiler : module-errors', async() => {
186 |
187 | // We apply the plugin directly to the compiler when targeting multi-compiler
188 | let globalPlugins = [new FriendlyErrorsWebpackPlugin()];
189 | const logs = await executeAndGetLogs('./fixtures/multi-compiler-module-errors/webpack.config', globalPlugins);
190 |
191 | expect(logs).toEqual([
192 | 'ERROR Failed to compile with 2 errors',
193 | '',
194 | 'This dependency was not found:',
195 | '',
196 | '* not-found in ./test/fixtures/multi-compiler-module-errors/index2.js',
197 | '',
198 | 'To install it, you can run: npm install --save not-found',
199 | '',
200 | '',
201 | 'This relative module was not found:',
202 | '',
203 | '* ./non-existing in ./test/fixtures/multi-compiler-module-errors/index.js',
204 | ]);
205 | });
206 |
207 | it('integration : postcss-loader : warnings', async() => {
208 |
209 | const logs = await executeAndGetLogs('./fixtures/postcss-warnings/webpack.config');
210 | expect(logs).toEqual([
211 | 'WARNING Compiled with 1 warning',
212 | '',
213 | 'warning in ./test/fixtures/postcss-warnings/index.css',
214 | '',
215 | `Module Warning (from ./node_modules/postcss-loader/src/index.js):
216 | Warning
217 |
218 | (3:2) grid-gap only works if grid-template(-areas) is being used`,
219 | ''
220 | ]);
221 | });
222 |
223 | it('integration : postcss-loader : warnings (multi-compiler version)', async() => {
224 |
225 | const logs = await executeAndGetLogs('./fixtures/multi-postcss-warnings/webpack.config');
226 | expect(logs).toEqual([
227 | 'WARNING Compiled with 1 warning',
228 | '',
229 | 'warning in ./test/fixtures/multi-postcss-warnings/index.css',
230 | '',
231 | `Module Warning (from ./node_modules/postcss-loader/src/index.js):
232 | Warning
233 |
234 | (3:2) grid-gap only works if grid-template(-areas) is being used`,
235 | '',
236 | 'WARNING Compiled with 1 warning',
237 | '',
238 | 'warning in ./test/fixtures/multi-postcss-warnings/index2.css',
239 | '',
240 | `Module Warning (from ./node_modules/postcss-loader/src/index.js):
241 | Warning
242 |
243 | (3:2) grid-auto-flow works only if grid-template-rows and grid-template-columns are present in the same rule`,
244 | ''
245 | ]);
246 | });
247 |
--------------------------------------------------------------------------------
/test/unit/formatErrors.spec.js:
--------------------------------------------------------------------------------
1 | const formatErrors = require('../../src/core/formatErrors');
2 |
3 | const simple = (errors) => errors
4 | .filter(error => !error.type).map(e => e.message);
5 |
6 | const allCaps = (errors) => errors
7 | .filter(error => error.type == 'other').map(e => e.message.toUpperCase());
8 |
9 | const notFound = (errors) => errors
10 | .filter(error => error.type === 'not-found').map(() => 'Not found');
11 |
12 | const formatters = [allCaps];
13 |
14 | it('formats the error based on the matching formatters', () => {
15 | const errors = [
16 | { message: 'Error 1', type: undefined },
17 | { message: 'Error 2', type: 'other' },
18 | { message: 'Error 3', type: 'not-found' },
19 | ];
20 |
21 | expect(formatErrors(errors, [simple, allCaps, notFound], 'Error')).toEqual([
22 | 'Error 1',
23 | 'ERROR 2',
24 | 'Not found',
25 | ]);
26 | });
27 |
--------------------------------------------------------------------------------
/test/unit/formatters/defaultError.spec.js:
--------------------------------------------------------------------------------
1 | const defaultError = require('../../../src/formatters/defaultError');
2 | const stripAnsi = require('strip-ansi');
3 |
4 | const noColor = (arr) => arr.map(stripAnsi);
5 | const error = { message: 'Error message', file: './src/index.js' };
6 |
7 | it('Formats errors with no type', () => {
8 | expect(noColor(defaultError([error], 'Warning'))).toEqual([
9 | ' Warning in ./src/index.js',
10 | '',
11 | 'Error message',
12 | '',
13 | ]);
14 | });
15 |
16 | it('Does not format other errors', () => {
17 | const otherError = { type: 'other-error' };
18 | expect(noColor(defaultError([otherError], 'Error'))).toEqual([]);
19 | });
20 |
--------------------------------------------------------------------------------
/test/unit/formatters/moduleNotFound.spec.js:
--------------------------------------------------------------------------------
1 | const moduleNotFound = require('../../../src/formatters/moduleNotFound');
2 |
3 | it('Formats module-not-found errors', () => {
4 | const error = { type: 'module-not-found', module: 'redux' };
5 | expect(moduleNotFound([error])).toEqual([
6 | 'This dependency was not found:',
7 | '',
8 | '* redux',
9 | '',
10 | 'To install it, you can run: npm install --save redux'
11 | ]);
12 | });
13 |
14 | it('Groups all module-not-found into one', () => {
15 | const reduxError = { type: 'module-not-found', module: 'redux' };
16 | const reactError = { type: 'module-not-found', module: 'react' };
17 | expect(moduleNotFound([reduxError, reactError])).toEqual([
18 | 'These dependencies were not found:',
19 | '',
20 | '* redux',
21 | '* react',
22 | '',
23 | 'To install them, you can run: npm install --save redux react'
24 | ]);
25 | });
26 |
27 | it('Groups same module in module-not-found with 2 files', () => {
28 | const reduxError = { type: 'module-not-found', module: 'redux' };
29 | const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
30 | const reactError2 = { type: 'module-not-found', module: 'react', file: '../src/file2.js' };
31 | expect(moduleNotFound([reduxError, reactError1, reactError2])).toEqual([
32 | 'These dependencies were not found:',
33 | '',
34 | '* redux',
35 | '* react in ./src/file1.js, ../src/file2.js',
36 | '',
37 | 'To install them, you can run: npm install --save redux react'
38 | ]);
39 | });
40 |
41 | it('Groups same module in module-not-found with 3 files', () => {
42 | const reduxError = { type: 'module-not-found', module: 'redux' };
43 | const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
44 | const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
45 | const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
46 | expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3])).toEqual([
47 | 'These dependencies were not found:',
48 | '',
49 | '* redux',
50 | '* react in ./src/file1.js, ./src/file2.js and 1 other',
51 | '',
52 | 'To install them, you can run: npm install --save redux react'
53 | ]);
54 | });
55 |
56 | it('Groups same module in module-not-found with 4 files', () => {
57 | const reduxError = { type: 'module-not-found', module: 'redux' };
58 | const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
59 | const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
60 | const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
61 | const reactError4 = { type: 'module-not-found', module: 'react', file: './src/file4.js' };
62 | expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3, reactError4])).toEqual([
63 | 'These dependencies were not found:',
64 | '',
65 | '* redux',
66 | '* react in ./src/file1.js, ./src/file2.js and 2 others',
67 | '',
68 | 'To install them, you can run: npm install --save redux react'
69 | ]);
70 | });
71 |
72 | it('Does not format other errors', () => {
73 | const otherError = { type: 'other-error', module: 'foo' };
74 | expect(moduleNotFound([otherError])).toEqual([]);
75 | });
76 |
--------------------------------------------------------------------------------
/test/unit/plugin/friendlyErrors.spec.js:
--------------------------------------------------------------------------------
1 | const EventEmitter = require('events');
2 | const Stats = require('webpack/lib/Stats')
3 | const Module = require('webpack/lib/Module');
4 | EventEmitter.prototype.plugin = EventEmitter.prototype.on;
5 |
6 | const output = require("../../../src/output");
7 | const FriendlyErrorsPlugin = require("../../../index");
8 |
9 | var notifierPlugin;
10 | var mockCompiler;
11 |
12 | beforeEach(() => {
13 | notifierPlugin = new FriendlyErrorsPlugin();
14 | mockCompiler = new EventEmitter();
15 | notifierPlugin.apply(mockCompiler);
16 | });
17 |
18 | it('friendlyErrors : capture invalid message', () => {
19 |
20 | const logs = output.captureLogs(() => {
21 | mockCompiler.emit('invalid');
22 | });
23 |
24 | expect(logs).toEqual([
25 | 'WAIT Compiling...',
26 | ''
27 | ]);
28 | });
29 |
30 | it('friendlyErrors : capture compilation without errors', () => {
31 |
32 | const stats = successfulCompilationStats();
33 | const logs = output.captureLogs(() => {
34 | mockCompiler.emit('done', stats);
35 | });
36 |
37 | expect(logs).toEqual([
38 | 'DONE Compiled successfully in 100ms',
39 | ''
40 | ]);
41 | });
42 |
43 | it('friendlyErrors : default clearConsole option', () => {
44 | const plugin = new FriendlyErrorsPlugin();
45 | expect(plugin.shouldClearConsole).toBeTruthy()
46 | });
47 |
48 | it('friendlyErrors : clearConsole option', () => {
49 | const plugin = new FriendlyErrorsPlugin({ clearConsole: false });
50 | expect(plugin.shouldClearConsole).toBeFalsy()
51 | });
52 |
53 | function successfulCompilationStats(opts) {
54 | const options = Object.assign({ startTime: 0, endTime: 100 }, opts);
55 |
56 | const compilation = {
57 | errors: [],
58 | warnings: [],
59 | children: []
60 | };
61 | const stats = new Stats(compilation);
62 | stats.startTime = options.startTime;
63 | stats.endTime = options.endTime;
64 | return stats;
65 | }
66 |
--------------------------------------------------------------------------------
/test/unit/transformers/babelSyntax.spec.js:
--------------------------------------------------------------------------------
1 | const babelSyntax = require('../../../src/transformers/babelSyntax');
2 |
3 | it('Sets severity to 1000', () => {
4 | const error = { name: 'ModuleBuildError', message: 'SyntaxError' };
5 | expect(babelSyntax(error).severity).toEqual(1000);
6 | });
7 |
8 | it('Does not set type (it should be handle by the default formatter)', () => {
9 | const error = { name: 'ModuleBuildError', message: 'SyntaxError' };
10 | expect(babelSyntax(error).type).toEqual(undefined);
11 | });
12 |
13 | it('Ignores other errors', () => {
14 | const error = { name: 'OtherError' };
15 | expect(babelSyntax(error)).toEqual(error);
16 | });
17 |
--------------------------------------------------------------------------------
/test/unit/transformers/moduleNotFound.spec.js:
--------------------------------------------------------------------------------
1 | const moduleNotFound = require('../../../src/transformers/moduleNotFound');
2 |
3 | const error = {
4 | name: 'ModuleNotFoundError',
5 | message: 'Module not found : redux',
6 | webpackError: {
7 | dependencies: [{ request: 'redux' } ],
8 | },
9 | };
10 |
11 | it('Sets severity to 900', () => {
12 | expect(moduleNotFound(error).severity).toEqual(900);
13 | });
14 |
15 | it('Sets module name', () => {
16 | expect(moduleNotFound(error).module).toEqual('redux');
17 | });
18 |
19 | it('Sets the appropiate message', () => {
20 | const message = 'Module not found redux';
21 | expect(moduleNotFound(error).message).toEqual(message);
22 | });
23 |
24 | it('Sets the appropiate type', () => {
25 | expect(moduleNotFound({
26 | name: 'ModuleNotFoundError',
27 | message: 'Module not found',
28 | webpackError: error.webpackError,
29 | }).type).toEqual('module-not-found');
30 | });
31 |
32 | it('Ignores other errors', () => {
33 | const error = { name: 'OtherError' };
34 | expect(moduleNotFound(error)).toEqual(error);
35 | });
36 |
--------------------------------------------------------------------------------
/test/unit/utils/utils.spec.js:
--------------------------------------------------------------------------------
1 | const utils = require('../../../src/utils');
2 |
3 | const concat = utils.concat;
4 | const uniqueBy = utils.uniqueBy;
5 |
6 | it('concat should concat removing undefined and null values', () => {
7 | const result = concat(1, undefined, '', null);
8 | expect(result).toEqual(
9 | [1, '']
10 | );
11 | });
12 |
13 | it('concat should handle arrays', () => {
14 | const result = concat(1, [2, 3], null);
15 | expect(result).toEqual(
16 | [1, 2, 3]
17 | );
18 | });
19 |
20 | it('uniqueBy should dedupe based on criterion returned from iteratee function', () => {
21 | const result = uniqueBy([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 3 }], function(val) {
22 | return val.id;
23 | });
24 | expect(result).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]);
25 | });
26 |
--------------------------------------------------------------------------------