Skip to content

Commit 3a22a62

Browse files
refactor: tests
1 parent 2dcac11 commit 3a22a62

9 files changed

+663
-972
lines changed

package-lock.json

+536-872
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"jest": "^23.4.0",
7171
"lint-staged": "^7.2.0",
7272
"memory-fs": "^0.4.1",
73-
"node-sass": "^4.9.4",
73+
"sass": "^1.14.3",
7474
"postcss-loader": "^3.0.0",
7575
"postcss-preset-env": "^6.0.6",
7676
"pre-commit": "^1.2.2",
@@ -85,7 +85,9 @@
8585
],
8686
"jest": {
8787
"testEnvironment": "node",
88-
"coveragePathIgnorePatterns": ["/test/"]
88+
"coveragePathIgnorePatterns": [
89+
"/test/"
90+
]
8991
},
9092
"pre-commit": "lint-staged",
9193
"lint-staged": {

test/__snapshots__/loader.test.js.snap

+15-11
Original file line numberDiff line numberDiff line change
@@ -634,33 +634,37 @@ Array [
634634
Array [
635635
1,
636636
"body {
637-
color: white; }
637+
color: white;
638+
}
638639
639640
body {
640641
font: 100% Helvetica, sans-serif;
641-
color: #333; }
642+
color: #333;
643+
}
642644
643645
nav ul {
644646
margin: 0;
645647
padding: 0;
646-
list-style: none; }
647-
648+
list-style: none;
649+
}
648650
nav li {
649-
display: inline-block; }
650-
651+
display: inline-block;
652+
}
651653
nav a {
652654
display: block;
653655
padding: 6px 12px;
654-
text-decoration: none; }
656+
text-decoration: none;
657+
}
655658
656659
.box {
657660
-webkit-transform: rotate(30deg);
658661
-ms-transform: rotate(30deg);
659-
transform: rotate(30deg); }
662+
transform: rotate(30deg);
663+
}
660664
661665
.background {
662-
background: url(/webpack/public/path/5b1f36bc41ab31f5b801d48ba1d65781.png); }
663-
",
666+
background: url(/webpack/public/path/5b1f36bc41ab31f5b801d48ba1d65781.png);
667+
}",
664668
"",
665669
],
666670
]
@@ -676,7 +680,7 @@ var ___CSS_LOADER_URL___0___ = escape(require(\\"./img.png\\"));
676680
exports.i(require(\\"-!../../../src/index.js??ref--4-0!./style.css\\"), \\"\\");
677681
678682
// CSS module
679-
exports.push([module.id, \\"body {\\\\n color: white; }\\\\n\\\\nbody {\\\\n font: 100% Helvetica, sans-serif;\\\\n color: #333; }\\\\n\\\\nnav ul {\\\\n margin: 0;\\\\n padding: 0;\\\\n list-style: none; }\\\\n\\\\nnav li {\\\\n display: inline-block; }\\\\n\\\\nnav a {\\\\n display: block;\\\\n padding: 6px 12px;\\\\n text-decoration: none; }\\\\n\\\\n.box {\\\\n -webkit-transform: rotate(30deg);\\\\n -ms-transform: rotate(30deg);\\\\n transform: rotate(30deg); }\\\\n\\\\n.background {\\\\n background: url(\\" + ___CSS_LOADER_URL___0___ + \\"); }\\\\n\\", \\"\\"]);
683+
exports.push([module.id, \\"body {\\\\n color: white;\\\\n}\\\\n\\\\nbody {\\\\n font: 100% Helvetica, sans-serif;\\\\n color: #333;\\\\n}\\\\n\\\\nnav ul {\\\\n margin: 0;\\\\n padding: 0;\\\\n list-style: none;\\\\n}\\\\nnav li {\\\\n display: inline-block;\\\\n}\\\\nnav a {\\\\n display: block;\\\\n padding: 6px 12px;\\\\n text-decoration: none;\\\\n}\\\\n\\\\n.box {\\\\n -webkit-transform: rotate(30deg);\\\\n -ms-transform: rotate(30deg);\\\\n transform: rotate(30deg);\\\\n}\\\\n\\\\n.background {\\\\n background: url(\\" + ___CSS_LOADER_URL___0___ + \\");\\\\n}\\", \\"\\"]);
680684
681685
"
682686
`;

test/__snapshots__/source-map-option.test.js.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`sourceMap false basic: errors 1`] = `Array []`;
44

5-
exports[`sourceMap false basic: module 1`] = `
5+
exports[`sourceMap false basic: module (evaluated) 1`] = `
66
Array [
77
Array [
88
1,
@@ -19,7 +19,7 @@ exports[`sourceMap false basic: warnings 1`] = `Array []`;
1919

2020
exports[`sourceMap false map from other loader: errors 1`] = `Array []`;
2121

22-
exports[`sourceMap false map from other loader: module 1`] = `
22+
exports[`sourceMap false map from other loader: module (evaluated) 1`] = `
2323
Array [
2424
Array [
2525
1,
@@ -36,7 +36,7 @@ exports[`sourceMap false map from other loader: warnings 1`] = `Array []`;
3636

3737
exports[`sourceMap true basic: errors 1`] = `Array []`;
3838

39-
exports[`sourceMap true basic: module 1`] = `
39+
exports[`sourceMap true basic: module (evaluated) 1`] = `
4040
Array [
4141
Array [
4242
1,
@@ -69,7 +69,7 @@ exports[`sourceMap true basic: warnings 1`] = `Array []`;
6969

7070
exports[`sourceMap true map from other loader: errors 1`] = `Array []`;
7171

72-
exports[`sourceMap true map from other loader: module 1`] = `
72+
exports[`sourceMap true map from other loader: module (evaluated) 1`] = `
7373
Array [
7474
Array [
7575
1,
@@ -104,7 +104,7 @@ exports[`sourceMap true map from other loader: warnings 1`] = `Array []`;
104104

105105
exports[`sourceMap true map is string: errors 1`] = `Array []`;
106106

107-
exports[`sourceMap true map is string: module 1`] = `
107+
exports[`sourceMap true map is string: module (evaluated) 1`] = `
108108
Array [
109109
Array [
110110
1,

test/helpers/utils.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import stripAnsi from 'strip-ansi';
1+
import path from 'path';
2+
23
import postcss from 'postcss';
4+
import stripAnsi from 'strip-ansi';
35

46
function normalizeErrors(errors) {
57
return errors.map((error) => {
@@ -32,4 +34,33 @@ function runPostcss(input, plugins) {
3234
);
3335
}
3436

35-
module.exports = { normalizeErrors, normalizeModule, runPostcss };
37+
function generateRulesWithSourceMap(enableSourceMap, sourceMap) {
38+
return {
39+
rules: [
40+
{
41+
test: /\.css$/,
42+
use: [
43+
{
44+
loader: path.resolve(__dirname, '../../src'),
45+
options: {
46+
sourceMap: enableSourceMap,
47+
},
48+
},
49+
{
50+
loader: path.resolve(__dirname, '../fixtures/source-map-loader.js'),
51+
options: {
52+
sourceMap,
53+
},
54+
},
55+
],
56+
},
57+
],
58+
};
59+
}
60+
61+
export {
62+
normalizeErrors,
63+
normalizeModule,
64+
runPostcss,
65+
generateRulesWithSourceMap,
66+
};

test/import-option.test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ describe('import', () => {
1212
},
1313
},
1414
};
15-
const stats = await webpack('import/import.css', config);
15+
const testId = './import/import.css';
16+
const stats = await webpack(testId, config);
1617
const { modules } = stats.toJson();
17-
const [, , , , , , , module] = modules;
18+
const module = modules.find((m) => m.id === testId);
1819

1920
expect(module.source).toMatchSnapshot('module');
2021
expect(evaluated(module.source, modules)).toMatchSnapshot(
@@ -34,9 +35,10 @@ describe('import', () => {
3435
},
3536
},
3637
};
37-
const stats = await webpack('import/import.css', config);
38+
const testId = './import/import.css';
39+
const stats = await webpack(testId, config);
3840
const { modules } = stats.toJson();
39-
const [, module] = modules;
41+
const module = modules.find((m) => m.id === testId);
4042

4143
expect(module.source).toMatchSnapshot('module');
4244
expect(evaluated(module.source, modules)).toMatchSnapshot(

test/loader.test.js

+31-24
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ describe('loader', () => {
1313
const stats = await webpack('basic.js');
1414
const { modules } = stats.toJson();
1515
const [, runtime, escape, module] = modules;
16-
const evaluatedModule = evaluated(module.source, modules);
1716

1817
expect(runtime.source).toMatchSnapshot('runtime');
1918
expect(escape.source).toMatchSnapshot('escape');
2019
expect(module.source).toMatchSnapshot('module');
21-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
22-
20+
expect(evaluated(module.source, modules)).toMatchSnapshot(
21+
'module (evaluated)'
22+
);
2323
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
2424
expect(stats.compilation.errors).toMatchSnapshot('errors');
2525
});
@@ -28,26 +28,27 @@ describe('loader', () => {
2828
const stats = await webpack('basic.css');
2929
const { modules } = stats.toJson();
3030
const [, runtime, escape, module] = modules;
31-
const evaluatedModule = evaluated(module.source, modules);
3231

3332
expect(runtime.source).toMatchSnapshot('runtime');
3433
expect(escape.source).toMatchSnapshot('escape');
3534
expect(module.source).toMatchSnapshot('module');
36-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
37-
35+
expect(evaluated(module.source, modules)).toMatchSnapshot(
36+
'module (evaluated)'
37+
);
3838
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
3939
expect(stats.compilation.errors).toMatchSnapshot('errors');
4040
});
4141

4242
it('empty options', async () => {
43-
const stats = await webpack('empty.css');
43+
const testId = './empty.css';
44+
const stats = await webpack(testId);
4445
const { modules } = stats.toJson();
45-
const [, module] = modules;
46-
const evaluatedModule = evaluated(module.source, modules);
46+
const module = modules.find((m) => m.id === testId);
4747

4848
expect(module.source).toMatchSnapshot('module');
49-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
50-
49+
expect(evaluated(module.source, modules)).toMatchSnapshot(
50+
'module (evaluated)'
51+
);
5152
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
5253
expect(stats.compilation.errors).toMatchSnapshot('errors');
5354
});
@@ -95,14 +96,15 @@ describe('loader', () => {
9596
},
9697
],
9798
};
99+
const testId = './postcss-present-env.css';
98100
const stats = await webpack('postcss-present-env.css', config);
99101
const { modules } = stats.toJson();
100-
const [, , module] = modules;
101-
const evaluatedModule = evaluated(module.source, modules);
102+
const module = modules.find((m) => m.id === testId);
102103

103104
expect(module.source).toMatchSnapshot('module');
104-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
105-
105+
expect(evaluated(module.source, modules)).toMatchSnapshot(
106+
'module (evaluated)'
107+
);
106108
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
107109
expect(stats.compilation.errors).toMatchSnapshot('errors');
108110
});
@@ -119,6 +121,10 @@ describe('loader', () => {
119121
},
120122
{
121123
loader: 'sass-loader',
124+
options: {
125+
// eslint-disable-next-line global-require
126+
implementation: require('sass'),
127+
},
122128
},
123129
],
124130
},
@@ -130,14 +136,15 @@ describe('loader', () => {
130136
},
131137
],
132138
};
139+
const testId = './sass-loader/basic.scss';
133140
const stats = await webpack('sass-loader/basic.scss', config);
134141
const { modules } = stats.toJson();
135-
const [, , , module] = modules;
136-
const evaluatedModule = evaluated(module.source, modules);
142+
const module = modules.find((m) => m.id === testId);
137143

138144
expect(module.source).toMatchSnapshot('module');
139-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
140-
145+
expect(evaluated(module.source, modules)).toMatchSnapshot(
146+
'module (evaluated)'
147+
);
141148
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
142149
expect(stats.compilation.errors).toMatchSnapshot('errors');
143150
});
@@ -245,15 +252,15 @@ describe('loader', () => {
245252
},
246253
],
247254
};
255+
const testId = './messages-api/basic.css';
248256
const stats = await webpack('messages-api/basic.css', config);
249257
const { modules } = stats.toJson();
250-
const [, , , module] = modules;
251-
const evaluatedModule = evaluated(module.source, modules);
258+
const module = modules.find((m) => m.id === testId);
252259

253-
// We don't need evaluated module here, because modules doesn't exists in graph
254260
expect(module.source).toMatchSnapshot('module');
255-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
256-
261+
expect(evaluated(module.source, modules)).toMatchSnapshot(
262+
'module (evaluated)'
263+
);
257264
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
258265
'warnings'
259266
);

0 commit comments

Comments
 (0)