Skip to content

Commit e40485e

Browse files
refactor: tests
1 parent 2dcac11 commit e40485e

File tree

6 files changed

+104
-87
lines changed

6 files changed

+104
-87
lines changed

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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 33 additions & 2 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 27 additions & 24 deletions
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
});
@@ -130,14 +132,15 @@ describe('loader', () => {
130132
},
131133
],
132134
};
135+
const testId = './sass-loader/basic.scss';
133136
const stats = await webpack('sass-loader/basic.scss', config);
134137
const { modules } = stats.toJson();
135-
const [, , , module] = modules;
136-
const evaluatedModule = evaluated(module.source, modules);
138+
const module = modules.find((m) => m.id === testId);
137139

138140
expect(module.source).toMatchSnapshot('module');
139-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
140-
141+
expect(evaluated(module.source, modules)).toMatchSnapshot(
142+
'module (evaluated)'
143+
);
141144
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
142145
expect(stats.compilation.errors).toMatchSnapshot('errors');
143146
});
@@ -245,15 +248,15 @@ describe('loader', () => {
245248
},
246249
],
247250
};
251+
const testId = './messages-api/basic.css';
248252
const stats = await webpack('messages-api/basic.css', config);
249253
const { modules } = stats.toJson();
250-
const [, , , module] = modules;
251-
const evaluatedModule = evaluated(module.source, modules);
254+
const module = modules.find((m) => m.id === testId);
252255

253-
// We don't need evaluated module here, because modules doesn't exists in graph
254256
expect(module.source).toMatchSnapshot('module');
255-
expect(evaluatedModule).toMatchSnapshot('module (evaluated)');
256-
257+
expect(evaluated(module.source, modules)).toMatchSnapshot(
258+
'module (evaluated)'
259+
);
257260
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
258261
'warnings'
259262
);

test/source-map-option.test.js

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
1-
import path from 'path';
2-
31
import webpack from './helpers/compiler';
42
import evaluated from './helpers/evaluated';
5-
import { normalizeModule } from './helpers/utils';
3+
import { normalizeModule, generateRulesWithSourceMap } from './helpers/utils';
64

75
describe('sourceMap', () => {
8-
const generateRulesWithSourceMap = (enableSourceMap, sourceMap) => {
9-
return {
10-
rules: [
11-
{
12-
test: /\.css$/,
13-
use: [
14-
{
15-
loader: path.resolve(__dirname, '../src'),
16-
options: {
17-
sourceMap: enableSourceMap,
18-
},
19-
},
20-
{
21-
loader: path.resolve(__dirname, 'fixtures/source-map-loader.js'),
22-
options: {
23-
sourceMap,
24-
},
25-
},
26-
],
27-
},
28-
],
29-
};
30-
};
31-
326
describe('true', () => {
337
it('basic', async () => {
348
const config = {
@@ -38,13 +12,14 @@ describe('sourceMap', () => {
3812
},
3913
},
4014
};
41-
const stats = await webpack('source-map/basic.css', config);
15+
const testId = './source-map/basic.css';
16+
const stats = await webpack(testId, config);
4217
const { modules } = stats.toJson();
18+
const module = modules.find((m) => m.id === testId);
4319

44-
expect(
45-
normalizeModule(evaluated(modules[modules.length - 1].source))
46-
).toMatchSnapshot('module');
47-
20+
expect(normalizeModule(evaluated(module.source))).toMatchSnapshot(
21+
'module (evaluated)'
22+
);
4823
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
4924
expect(stats.compilation.errors).toMatchSnapshot('errors');
5025
});
@@ -59,13 +34,14 @@ describe('sourceMap', () => {
5934
sourcesContent: ['.class { a: b c d; }'],
6035
version: 3,
6136
});
62-
const stats = await webpack('source-map/basic.css', config);
37+
const testId = './source-map/basic.css';
38+
const stats = await webpack(testId, config);
6339
const { modules } = stats.toJson();
40+
const module = modules.find((m) => m.id === testId);
6441

65-
expect(
66-
normalizeModule(evaluated(modules[modules.length - 1].source))
67-
).toMatchSnapshot('module');
68-
42+
expect(normalizeModule(evaluated(module.source))).toMatchSnapshot(
43+
'module (evaluated)'
44+
);
6945
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
7046
expect(stats.compilation.errors).toMatchSnapshot('errors');
7147
});
@@ -83,13 +59,14 @@ describe('sourceMap', () => {
8359
version: 3,
8460
})
8561
);
86-
const stats = await webpack('source-map/basic.css', config);
62+
const testId = './source-map/basic.css';
63+
const stats = await webpack(testId, config);
8764
const { modules } = stats.toJson();
65+
const module = modules.find((m) => m.id === testId);
8866

8967
expect(
90-
normalizeModule(evaluated(modules[modules.length - 1].source))
91-
).toMatchSnapshot('module');
92-
68+
normalizeModule(evaluated(module.source, modules))
69+
).toMatchSnapshot('module (evaluated)');
9370
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
9471
expect(stats.compilation.errors).toMatchSnapshot('errors');
9572
});
@@ -104,13 +81,14 @@ describe('sourceMap', () => {
10481
},
10582
},
10683
};
107-
const stats = await webpack('source-map/basic.css', config);
84+
const testId = './source-map/basic.css';
85+
const stats = await webpack(testId, config);
10886
const { modules } = stats.toJson();
87+
const module = modules.find((m) => m.id === testId);
10988

110-
expect(evaluated(modules[modules.length - 1].source)).toMatchSnapshot(
111-
'module'
89+
expect(evaluated(module.source, modules)).toMatchSnapshot(
90+
'module (evaluated)'
11291
);
113-
11492
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
11593
expect(stats.compilation.errors).toMatchSnapshot('errors');
11694
});
@@ -125,13 +103,14 @@ describe('sourceMap', () => {
125103
sourcesContent: ['.class { a: b c d; }'],
126104
version: 3,
127105
});
128-
const stats = await webpack('source-map/basic.css', config);
106+
const testId = './source-map/basic.css';
107+
const stats = await webpack(testId, config);
129108
const { modules } = stats.toJson();
109+
const module = modules.find((m) => m.id === testId);
130110

131-
expect(evaluated(modules[modules.length - 1].source)).toMatchSnapshot(
132-
'module'
111+
expect(evaluated(module.source, modules)).toMatchSnapshot(
112+
'module (evaluated)'
133113
);
134-
135114
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
136115
expect(stats.compilation.errors).toMatchSnapshot('errors');
137116
});

test/url-option.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import evaluated from './helpers/evaluated';
33

44
describe('url', () => {
55
it('true', async () => {
6-
const stats = await webpack('url/url.css');
6+
const testId = './url/url.css';
7+
const stats = await webpack(testId);
78
const { modules } = stats.toJson();
8-
const [, , , , , , , , , , , , , , , , module] = modules;
9+
const module = modules.find((m) => m.id === testId);
910

1011
expect(module.source).toMatchSnapshot('module');
1112
expect(evaluated(module.source, modules)).toMatchSnapshot(
@@ -23,9 +24,10 @@ describe('url', () => {
2324
},
2425
},
2526
};
27+
const testId = './url/url.css';
2628
const stats = await webpack('url/url.css', config);
2729
const { modules } = stats.toJson();
28-
const [, , module] = modules;
30+
const module = modules.find((m) => m.id === testId);
2931

3032
expect(module.source).toMatchSnapshot('module');
3133
expect(evaluated(module.source, modules)).toMatchSnapshot(

0 commit comments

Comments
 (0)