Skip to content

Commit 526c229

Browse files
test: refactor (#97)
1 parent 7f769aa commit 526c229

15 files changed

+765
-339
lines changed
+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;
4+
5+
exports[`source-map-loader should leave normal files untouched: errors 1`] = `Array []`;
6+
7+
exports[`source-map-loader should leave normal files untouched: warnings 1`] = `Array []`;
8+
9+
exports[`source-map-loader should process external SourceMaps (external sources): css 1`] = `
10+
"with SourceMap
11+
// comment"
12+
`;
13+
14+
exports[`source-map-loader should process external SourceMaps (external sources): errors 1`] = `Array []`;
15+
16+
exports[`source-map-loader should process external SourceMaps (external sources): map 1`] = `
17+
Object {
18+
"file": "external-source-map2.js",
19+
"mappings": "AAAA",
20+
"sources": Array [
21+
"/test/fixtures/external-source-map2.txt - (normalized for test)",
22+
],
23+
"sourcesContent": Array [
24+
"with SourceMap",
25+
],
26+
"version": 3,
27+
}
28+
`;
29+
30+
exports[`source-map-loader should process external SourceMaps (external sources): warnings 1`] = `Array []`;
31+
32+
exports[`source-map-loader should process external SourceMaps: css 1`] = `
33+
"with SourceMap
34+
// comment"
35+
`;
36+
37+
exports[`source-map-loader should process external SourceMaps: errors 1`] = `Array []`;
38+
39+
exports[`source-map-loader should process external SourceMaps: map 1`] = `
40+
Object {
41+
"file": "external-source-map.js",
42+
"mappings": "AAAA",
43+
"sources": Array [
44+
"external-source-map.txt",
45+
],
46+
"sourcesContent": Array [
47+
"with SourceMap",
48+
],
49+
"version": 3,
50+
}
51+
`;
52+
53+
exports[`source-map-loader should process external SourceMaps: warnings 1`] = `Array []`;
54+
55+
exports[`source-map-loader should process inlined SourceMaps with charset: css 1`] = `
56+
"with SourceMap
57+
// comment"
58+
`;
59+
60+
exports[`source-map-loader should process inlined SourceMaps with charset: errors 1`] = `Array []`;
61+
62+
exports[`source-map-loader should process inlined SourceMaps with charset: map 1`] = `
63+
Object {
64+
"file": "charset-inline-source-map.js",
65+
"mappings": "AAAA",
66+
"sources": Array [
67+
"charset-inline-source-map.txt",
68+
],
69+
"sourcesContent": Array [
70+
"with SourceMap",
71+
],
72+
"version": 3,
73+
}
74+
`;
75+
76+
exports[`source-map-loader should process inlined SourceMaps with charset: warnings 1`] = `Array []`;
77+
78+
exports[`source-map-loader should process inlined SourceMaps: css 1`] = `
79+
"with SourceMap
80+
// comment"
81+
`;
82+
83+
exports[`source-map-loader should process inlined SourceMaps: errors 1`] = `Array []`;
84+
85+
exports[`source-map-loader should process inlined SourceMaps: map 1`] = `
86+
Object {
87+
"file": "inline-source-map.js",
88+
"mappings": "AAAA",
89+
"sources": Array [
90+
"inline-source-map.txt",
91+
],
92+
"sourcesContent": Array [
93+
"with SourceMap",
94+
],
95+
"version": 3,
96+
}
97+
`;
98+
99+
exports[`source-map-loader should process inlined SourceMaps: warnings 1`] = `Array []`;
100+
101+
exports[`source-map-loader should skip invalid base64 SourceMap: css 1`] = `
102+
"without SourceMap
103+
// @sourceMappingURL=data:application/source-map;base64,\\"something invalid\\"
104+
// comment"
105+
`;
106+
107+
exports[`source-map-loader should skip invalid base64 SourceMap: errors 1`] = `Array []`;
108+
109+
exports[`source-map-loader should skip invalid base64 SourceMap: warnings 1`] = `Array []`;
110+
111+
exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: css 1`] = `
112+
"with SourceMap
113+
// comment"
114+
`;
115+
116+
exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: errors 1`] = `Array []`;
117+
118+
exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: map 1`] = `
119+
Object {
120+
"file": "absolute-sourceRoot-source-map.js",
121+
"mappings": "AAAA",
122+
"sources": Array [
123+
"/test/fixtures/absolute-sourceRoot-source-map.txt - (normalized for test)",
124+
],
125+
"sourcesContent": Array [
126+
"with SourceMap
127+
// comment",
128+
],
129+
"version": 3,
130+
}
131+
`;
132+
133+
exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;
134+
135+
exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: css 1`] = `
136+
"with SourceMap
137+
// comment"
138+
`;
139+
140+
exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: errors 1`] = `Array []`;
141+
142+
exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: map 1`] = `
143+
Object {
144+
"file": "relative-sourceRoot-source-map.js",
145+
"mappings": "AAAA",
146+
"sources": Array [
147+
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
148+
],
149+
"sourcesContent": Array [
150+
"with SourceMap
151+
// comment",
152+
],
153+
"version": 3,
154+
}
155+
`;
156+
157+
exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;
158+
159+
exports[`source-map-loader should use last SourceMap directive: css 1`] = `
160+
"with SourceMap
161+
anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";
162+
// comment"
163+
`;
164+
165+
exports[`source-map-loader should use last SourceMap directive: errors 1`] = `Array []`;
166+
167+
exports[`source-map-loader should use last SourceMap directive: map 1`] = `
168+
Object {
169+
"file": "inline-source-map.js",
170+
"mappings": "AAAA",
171+
"sources": Array [
172+
"inline-source-map.txt",
173+
],
174+
"sourcesContent": Array [
175+
"with SourceMap",
176+
],
177+
"version": 3,
178+
}
179+
`;
180+
181+
exports[`source-map-loader should use last SourceMap directive: warnings 1`] = `Array []`;
182+
183+
exports[`source-map-loader should warn on invalid SourceMap: css 1`] = `
184+
"with SourceMap
185+
//#sourceMappingURL=invalid-source-map.map
186+
// comment"
187+
`;
188+
189+
exports[`source-map-loader should warn on invalid SourceMap: errors 1`] = `Array []`;
190+
191+
exports[`source-map-loader should warn on invalid SourceMap: warnings 1`] = `
192+
Array [
193+
"ModuleWarning: Module Warning (from \`replaced original path\`):
194+
(Emitted value instead of an instance of Error) Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
195+
]
196+
`;
197+
198+
exports[`source-map-loader should warn on invalid base64 SourceMap: css 1`] = `
199+
"without SourceMap
200+
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
201+
// comment"
202+
`;
203+
204+
exports[`source-map-loader should warn on invalid base64 SourceMap: errors 1`] = `Array []`;
205+
206+
exports[`source-map-loader should warn on invalid base64 SourceMap: warnings 1`] = `
207+
Array [
208+
"ModuleWarning: Module Warning (from \`replaced original path\`):
209+
(Emitted value instead of an instance of Error) Cannot parse inline SourceMap: data:application/source-map;base64,invalid/base64=",
210+
]
211+
`;
212+
213+
exports[`source-map-loader should warn on missing SourceMap: css 1`] = `
214+
"with SourceMap
215+
//#sourceMappingURL=missing-source-map.map
216+
// comment"
217+
`;
218+
219+
exports[`source-map-loader should warn on missing SourceMap: errors 1`] = `Array []`;
220+
221+
exports[`source-map-loader should warn on missing SourceMap: warnings 1`] = `
222+
Array [
223+
"ModuleWarning: Module Warning (from \`replaced original path\`):
224+
(Emitted value instead of an instance of Error) Cannot find SourceMap 'missing-source-map.map': Error: Can't resolve './missing-source-map.map' in '/test/fixtures'",
225+
]
226+
`;
227+
228+
exports[`source-map-loader should warn on missing source file: css 1`] = `
229+
"with SourceMap
230+
// comment"
231+
`;
232+
233+
exports[`source-map-loader should warn on missing source file: errors 1`] = `Array []`;
234+
235+
exports[`source-map-loader should warn on missing source file: map 1`] = `
236+
Object {
237+
"file": "missing-source-map2.js",
238+
"mappings": "AAAA",
239+
"sources": Array [
240+
"missing-source-map2.txt",
241+
],
242+
"sourcesContent": Array [
243+
null,
244+
],
245+
"version": 3,
246+
}
247+
`;
248+
249+
exports[`source-map-loader should warn on missing source file: warnings 1`] = `
250+
Array [
251+
"ModuleWarning: Module Warning (from \`replaced original path\`):
252+
(Emitted value instead of an instance of Error) Cannot find source file 'missing-source-map2.txt': Error: Can't resolve './missing-source-map2.txt' in '/test/fixtures'",
253+
]
254+
`;

test/helpers/compile.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default (compiler) => {
2+
return new Promise((resolve, reject) => {
3+
compiler.run((error, stats) => {
4+
if (error) {
5+
return reject(error);
6+
}
7+
8+
return resolve(stats);
9+
});
10+
});
11+
};

test/helpers/execute.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Module from 'module';
2+
import path from 'path';
3+
4+
const parentModule = module;
5+
6+
export default (code) => {
7+
const resource = 'test.js';
8+
const module = new Module(resource, parentModule);
9+
// eslint-disable-next-line no-underscore-dangle
10+
module.paths = Module._nodeModulePaths(
11+
path.resolve(__dirname, '../fixtures')
12+
);
13+
module.filename = resource;
14+
15+
// eslint-disable-next-line no-underscore-dangle
16+
module._compile(code, resource);
17+
18+
return module.exports;
19+
};

test/helpers/getCodeFromBundle.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import vm from 'vm';
2+
3+
import readAsset from './readAsset';
4+
5+
function getCodeFromBundle(stats, compiler, asset) {
6+
let code = null;
7+
8+
if (
9+
stats &&
10+
stats.compilation &&
11+
stats.compilation.assets &&
12+
stats.compilation.assets[asset || 'main.bundle.js']
13+
) {
14+
code = readAsset(asset || 'main.bundle.js', compiler, stats);
15+
}
16+
17+
if (!code) {
18+
throw new Error("Can't find compiled code");
19+
}
20+
21+
const result = vm.runInNewContext(
22+
`${code};\nmodule.exports = sourceMapLoaderExport;`,
23+
{
24+
module: {},
25+
}
26+
);
27+
28+
// eslint-disable-next-line no-underscore-dangle
29+
return result.__esModule ? result.default : result;
30+
}
31+
32+
export default getCodeFromBundle;

test/helpers/getCompiler.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from 'path';
2+
3+
import webpack from 'webpack';
4+
import { createFsFromVolume, Volume } from 'memfs';
5+
6+
export default (fixture, loaderOptions = {}, config = {}) => {
7+
const fullConfig = {
8+
mode: 'development',
9+
devtool: config.devtool || 'source-map',
10+
context: path.resolve(__dirname, '../fixtures'),
11+
entry: path.resolve(__dirname, '../fixtures', fixture),
12+
output: {
13+
path: path.resolve(__dirname, '../outputs'),
14+
filename: '[name].bundle.js',
15+
chunkFilename: '[name].chunk.js',
16+
library: 'sourceMapLoaderExport',
17+
},
18+
module: {
19+
rules: [
20+
{
21+
test: /\.js/i,
22+
rules: [
23+
{
24+
loader: require.resolve('./testLoader'),
25+
},
26+
{
27+
loader: path.resolve(__dirname, '../../src'),
28+
options: loaderOptions || {},
29+
},
30+
],
31+
},
32+
],
33+
},
34+
plugins: [],
35+
...config,
36+
};
37+
38+
const compiler = webpack(fullConfig);
39+
40+
if (!config.outputFileSystem) {
41+
const outputFileSystem = createFsFromVolume(new Volume());
42+
// Todo remove when we drop webpack@4 support
43+
outputFileSystem.join = path.join.bind(path);
44+
45+
compiler.outputFileSystem = outputFileSystem;
46+
}
47+
48+
return compiler;
49+
};

test/helpers/getErrors.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import normalizeErrors from './normalizeErrors';
2+
3+
export default (stats) => {
4+
return normalizeErrors(stats.compilation.errors.sort());
5+
};

test/helpers/getWarnings.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import normalizeErrors from './normalizeErrors';
2+
3+
export default (stats) => {
4+
return normalizeErrors(stats.compilation.warnings.sort());
5+
};

0 commit comments

Comments
 (0)