Skip to content

Commit 06e6d08

Browse files
refactor: tests
1 parent eab41ba commit 06e6d08

File tree

5 files changed

+206
-14
lines changed

5 files changed

+206
-14
lines changed

test/SyntaxError.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { normalizeErrors } from './helpers/utils';
33

44
describe('SyntaxError', () => {
55
test('basic', async () => {
6-
const stats = await webpack('broken.js');
6+
const stats = await webpack('broken.css');
77

88
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
99
'warnings'

test/__snapshots__/loader.test.js.snap

+191
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,196 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`loader basic (css endpoint): errors 1`] = `Array []`;
4+
5+
exports[`loader basic (css endpoint): module 1`] = `
6+
Array [
7+
Array [
8+
1,
9+
"@charset \\"UTF-8\\";
10+
11+
.class {
12+
color: red;
13+
background: url({./url/img.png});
14+
}
15+
16+
.u-m\\\\+ {
17+
a: b c d;
18+
}
19+
20+
.grid.\\\\-top {
21+
a: b c d;
22+
}
23+
24+
.u-m\\\\00002b {
25+
a: b c d;
26+
}
27+
28+
.class {
29+
content: '\\"\\\\\\\\f10c\\"';
30+
}
31+
32+
.class {
33+
font-family: '微软雅黑';
34+
}
35+
36+
.class {
37+
content: '\\\\e901';
38+
}
39+
40+
.class {
41+
content: \\"\\\\F10C\\"
42+
}
43+
44+
.class {
45+
content: \\"\\\\f10C\\"
46+
}
47+
48+
.class {
49+
content: \\"\\\\F10C \\\\F10D\\"
50+
}
51+
52+
:root {
53+
--foo: 1px;
54+
--bar: 2px;
55+
}
56+
57+
:root {
58+
--title-align: center;
59+
--sr-only: {
60+
position: absolute;
61+
width: 1px;
62+
height: 1px;
63+
padding: 0;
64+
overflow: hidden;
65+
clip: rect(0,0,0,0);
66+
white-space: nowrap;
67+
clip-path: inset(50%);
68+
border: 0;
69+
};
70+
}
71+
",
72+
"",
73+
],
74+
]
75+
`;
76+
77+
exports[`loader basic (css endpoint): runtime 1`] = `
78+
"'use strict';
79+
80+
/* eslint-disable */
81+
82+
// CSS Loader (Runtime)
83+
module.exports = function (useSourceMap) {
84+
const list = [];
85+
86+
// Return the list of modules as css string
87+
list.toString = function toString() {
88+
return this.map(function (item) {
89+
const content = cssWithMappingToString(item, useSourceMap);
90+
91+
if (item[2]) {
92+
return '@media ' + item[2] + '{' + content + '}';
93+
}
94+
95+
return content;
96+
}).join('');
97+
};
98+
99+
// Import a list of modules into the list
100+
list.i = function (modules, mediaQuery) {
101+
if (typeof modules === 'string') {
102+
modules = [[null, modules, '']];
103+
}
104+
105+
const isImported = {};
106+
107+
for (let i = 0; i < this.length; i++) {
108+
const id = this[i][0];
109+
110+
if (typeof id === 'number') {
111+
isImported[id] = true;
112+
}
113+
}
114+
115+
for (let i = 0; i < modules.length; i++) {
116+
const item = modules[i];
117+
118+
// Skip already imported module.
119+
// This implementation is not 100% perfect for weird media query combinations
120+
// when a module is imported multiple times with different media queries.
121+
// I hope this will never occur (Hey this way we have smaller bundles).
122+
if (typeof item[0] !== 'number' || !isImported[item[0]]) {
123+
if (mediaQuery && !item[2]) {
124+
item[2] = mediaQuery;
125+
} else if (mediaQuery) {
126+
item[2] = '(' + item[2] + ') and (' + mediaQuery + ')';
127+
}
128+
129+
list.push(item);
130+
}
131+
}
132+
};
133+
134+
return list;
135+
};
136+
137+
function cssWithMappingToString(item, useSourceMap) {
138+
const content = item[1] || '';
139+
const sourceMap = item[3];
140+
141+
if (!sourceMap) {
142+
return content;
143+
}
144+
145+
if (useSourceMap && typeof btoa === 'function') {
146+
const sourceMapping = toComment(sourceMap);
147+
const sourceURLs = sourceMap.sources.map(function (source) {
148+
return '/*# sourceURL=' + sourceMap.sourceRoot + source + ' */';
149+
});
150+
151+
return [content].concat(sourceURLs).concat([sourceMapping]).join('\\\\n');
152+
}
153+
154+
return [content].join('\\\\n');
155+
}
156+
157+
// Adapted from convert-source-map (MIT)
158+
function toComment(sourceMap) {
159+
// eslint-disable-next-line no-undef
160+
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
161+
const data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
162+
163+
return '/*# ' + data + ' */';
164+
}"
165+
`;
166+
167+
exports[`loader basic (css endpoint): runtimeEscape 1`] = `
168+
"'use strict';
169+
170+
/* eslint-disable */
171+
172+
module.exports = function escape(url) {
173+
if (typeof url !== 'string') {
174+
return url;
175+
}
176+
177+
// If url is already wrapped in quotes, remove them
178+
if (/^['\\"].*['\\"]$/.test(url)) {
179+
url = url.slice(1, -1);
180+
}
181+
182+
// Should url be wrapped?
183+
// See https://drafts.csswg.org/css-values-3/#urls
184+
if (/[\\"'() \\\\t\\\\n]/.test(url)) {
185+
return '\\"' + url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n') + '\\"';
186+
}
187+
188+
return url;
189+
};"
190+
`;
191+
192+
exports[`loader basic (css endpoint): warnings 1`] = `Array []`;
193+
3194
exports[`loader basic: errors 1`] = `Array []`;
4195

5196
exports[`loader basic: module 1`] = `

test/fixtures/broken.js

-4
This file was deleted.

test/fixtures/empty.js

-4
This file was deleted.

test/loader.test.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ describe('loader', () => {
1818
expect(stats.compilation.errors).toMatchSnapshot('errors');
1919
});
2020

21+
test('basic (css endpoint)', async () => {
22+
const stats = await webpack('basic.css');
23+
const [runtime, runtimeEscape, module] = stats.toJson().modules;
24+
25+
expect(runtime.source).toMatchSnapshot('runtime');
26+
expect(runtimeEscape.source).toMatchSnapshot('runtimeEscape');
27+
expect(evaluated(module.source)).toMatchSnapshot('module');
28+
29+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
30+
expect(stats.compilation.errors).toMatchSnapshot('errors');
31+
});
32+
2133
test('empty options', async () => {
22-
const stats = await webpack('empty.js');
34+
const stats = await webpack('empty.css');
2335
const [, module] = stats.toJson().modules;
2436

2537
expect(evaluated(module.source)).toMatchSnapshot('module');
@@ -39,7 +51,7 @@ describe('loader', () => {
3951
},
4052
],
4153
};
42-
const stats = await webpack('basic.js', config);
54+
const stats = await webpack('basic.css', config);
4355

4456
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
4557
expect(stats.compilation.errors).toMatchSnapshot('errors');
@@ -78,7 +90,4 @@ describe('loader', () => {
7890
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
7991
expect(stats.compilation.errors).toMatchSnapshot('errors');
8092
});
81-
82-
// Todo message api test
83-
// Todo test warnings
8493
});

0 commit comments

Comments
 (0)