Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit 2c36cac

Browse files
committed
Improve testing DX concerning errors
Jest hoists console.* output so it's impossible to tell which test it's coming from. By simplifying the API to expect or not expect compiling to throw we can see exactly what happened.
1 parent 4d2c41b commit 2c36cac

File tree

3 files changed

+93
-56
lines changed

3 files changed

+93
-56
lines changed

packages/css-customs-loader/test/__snapshots__/index.test.js.snap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,50 @@ module.exports[\\"customSelectors\\"] = {
1717
};"
1818
`;
1919

20+
exports[`emits an error when css-customs-loader is placed after css-loader 1`] = `
21+
"./fixtures/basic.css
22+
Module build failed (from ../index.js):
23+
Error: css-customs-loader should be added BEFORE css-loader. /Users/silvenon/Code/css-customs/node_modules/css-loader/index.js?importLoaders=2!/Users/silvenon/Code/css-customs/packages/css-customs-loader/index.js!/Users/silvenon/Code/css-customs/node_modules/postcss-loader/src/index.js!/Users/silvenon/Code/css-customs/packages/css-customs-loader/test/fixtures/basic.css
24+
at Object.<anonymous>.exports.addBeforeCssLoader.request [as addBeforeCssLoader] (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/errors.js:3:6)
25+
at Object.addBeforeCssLoader (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/index.js:20:27)"
26+
`;
27+
28+
exports[`emits an error when postcss-loader is missing 1`] = `
29+
"./fixtures/basic.css
30+
Module build failed (from ../index.js):
31+
Error:
32+
postcss-loader is missing, you need to use it in order for css-customs-loader to work properly.
33+
34+
at Object.<anonymous> (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/errors.js:8:32)
35+
at Runtime._execModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:867:68)
36+
at Runtime._loadModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:577:12)
37+
at Runtime.requireModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:433:10)
38+
at Runtime.requireModuleOrMock (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:598:21)
39+
at Object.require (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/index.js:8:15)
40+
at Runtime._execModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:867:68)
41+
at Runtime._loadModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:577:12)
42+
at Runtime.requireModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:433:10)
43+
at Runtime.requireModuleOrMock (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:598:21)"
44+
`;
45+
46+
exports[`emits an error when postcss-preset-env is missing 1`] = `
47+
"./fixtures/basic.css
48+
Module build failed (from ../index.js):
49+
Error:
50+
postcss-preset-env is missing from your plugins, you need to use it in order for css-customs-loader to work properly.
51+
52+
at Object.<anonymous> (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/errors.js:14:35)
53+
at Runtime._execModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:867:68)
54+
at Runtime._loadModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:577:12)
55+
at Runtime.requireModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:433:10)
56+
at Runtime.requireModuleOrMock (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:598:21)
57+
at Object.require (/Users/silvenon/Code/css-customs/packages/css-customs-loader/lib/index.js:8:15)
58+
at Runtime._execModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:867:68)
59+
at Runtime._loadModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:577:12)
60+
at Runtime.requireModule (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:433:10)
61+
at Runtime.requireModuleOrMock (/Users/silvenon/Code/css-customs/node_modules/jest-runtime/build/index.js:598:21)"
62+
`;
63+
2064
exports[`exposes CSS Modules in the same object as customs 1`] = `
2165
"exports = module.exports = require(\\"<path to node_modules>/css-loader/lib/css-base.js\\")(false);
2266
// imports

packages/css-customs-loader/test/compile.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
/* eslint-disable no-console */
21
const webpack = require('webpack')
32
const MemoryFs = require('memory-fs')
43
const path = require('path')
54

65
// https://webpack.js.org/contribute/writing-a-loader/#testing
76

8-
module.exports = ({ entry, logErrors = true, writeToDisk = false, rules }) => {
7+
module.exports = ({ entry, writeToDisk = false, rules }) => {
98
const compiler = webpack({
109
entry,
1110
output: {
@@ -27,8 +26,8 @@ module.exports = ({ entry, logErrors = true, writeToDisk = false, rules }) => {
2726
if (err != null) {
2827
return reject(err)
2928
}
30-
if (stats.hasErrors() && logErrors) {
31-
console.error(stats.toJson().errors.join('\n\n'))
29+
if (stats.hasErrors()) {
30+
return reject(stats.toJson().errors.join('\n\n'))
3231
}
3332
resolve({ stats, entry })
3433
})

packages/css-customs-loader/test/index.test.js

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,58 @@ const getCompiledOutput = ({ stats, entry }) =>
1212

1313
describe(`emits an error`, () => {
1414
test(`when css-customs-loader is placed after css-loader`, async () => {
15-
const { stats } = await compile({
16-
entry: './fixtures/basic.css',
17-
logErrors: false,
18-
rules: [
19-
{
20-
test: /\.css$/,
21-
use: [
22-
'css-loader?importLoaders=2',
23-
cssCustomsLoader,
24-
'postcss-loader',
25-
],
26-
},
27-
],
28-
})
29-
expect(stats.hasErrors()).toBe(true)
15+
await expect(
16+
compile({
17+
entry: './fixtures/basic.css',
18+
rules: [
19+
{
20+
test: /\.css$/,
21+
use: [
22+
'css-loader?importLoaders=2',
23+
cssCustomsLoader,
24+
'postcss-loader',
25+
],
26+
},
27+
],
28+
})
29+
).rejects.toMatchSnapshot()
3030
})
3131

3232
test(`when postcss-preset-env is missing`, async () => {
33-
const { stats } = await compile({
34-
entry: './fixtures/basic.css',
35-
logErrors: false,
36-
rules: [
37-
{
38-
test: /\.css$/,
39-
use: [
40-
cssCustomsLoader,
41-
'css-loader?importLoaders=1',
42-
{
43-
loader: 'postcss-loader',
44-
options: {
45-
plugins: () => [require('postcss-brand-colors')],
33+
await expect(
34+
compile({
35+
entry: './fixtures/basic.css',
36+
rules: [
37+
{
38+
test: /\.css$/,
39+
use: [
40+
cssCustomsLoader,
41+
'css-loader?importLoaders=1',
42+
{
43+
loader: 'postcss-loader',
44+
options: {
45+
plugins: () => [require('postcss-brand-colors')],
46+
},
4647
},
47-
},
48-
],
49-
},
50-
],
51-
})
52-
expect(stats.hasErrors()).toBe(true)
48+
],
49+
},
50+
],
51+
})
52+
).rejects.toMatchSnapshot()
5353
})
5454

5555
test(`when postcss-loader is missing`, async () => {
56-
const { stats } = await compile({
57-
entry: './fixtures/basic.css',
58-
logErrors: false,
59-
rules: [
60-
{
61-
test: /\.css$/,
62-
use: [cssCustomsLoader, 'css-loader'],
63-
},
64-
],
65-
})
66-
expect(stats.hasErrors()).toBe(true)
56+
await expect(
57+
compile({
58+
entry: './fixtures/basic.css',
59+
rules: [
60+
{
61+
test: /\.css$/,
62+
use: [cssCustomsLoader, 'css-loader'],
63+
},
64+
],
65+
})
66+
).rejects.toMatchSnapshot()
6767
})
6868
})
6969

@@ -77,7 +77,6 @@ it(`exposes CSS customs in the default export object`, async () => {
7777
},
7878
],
7979
})
80-
expect(stats.hasErrors()).toBe(false)
8180
const output = getCompiledOutput({ stats, entry })
8281
expect(output).toMatchSnapshot()
8382
})
@@ -96,7 +95,6 @@ it(`exposes CSS Modules in the same object as customs`, async () => {
9695
},
9796
],
9897
})
99-
expect(stats.hasErrors()).toBe(false)
10098
const output = getCompiledOutput({ stats, entry })
10199
expect(output).toMatchSnapshot()
102100
})
@@ -115,13 +113,12 @@ it(`can export only locals`, async () => {
115113
},
116114
],
117115
})
118-
expect(stats.hasErrors()).toBe(false)
119116
const output = getCompiledOutput({ stats, entry })
120117
expect(output).toMatchSnapshot()
121118
})
122119

123120
it(`supports files with external @imports`, async () => {
124-
const { stats } = await compile({
121+
await compile({
125122
entry: './fixtures/external.css',
126123
rules: [
127124
{
@@ -140,7 +137,6 @@ it(`supports files with external @imports`, async () => {
140137
},
141138
],
142139
})
143-
expect(stats.hasErrors()).toBe(false)
144140
})
145141

146142
it(`uses PostCSS plugins before postcss-preset-env`, async () => {
@@ -165,7 +161,6 @@ it(`uses PostCSS plugins before postcss-preset-env`, async () => {
165161
},
166162
],
167163
})
168-
expect(stats.hasErrors()).toBe(false)
169164
const output = getCompiledOutput({ stats, entry })
170165
expect(output).toMatchSnapshot()
171166
})
@@ -185,7 +180,6 @@ it('uses webpack loaders after postcss-loader', async () => {
185180
},
186181
],
187182
})
188-
expect(stats.hasErrors()).toBe(false)
189183
const output = getCompiledOutput({ stats, entry })
190184
expect(output).toMatchSnapshot()
191185
})

0 commit comments

Comments
 (0)