Skip to content

Commit f0c5312

Browse files
refactor: tests
1 parent 81d104a commit f0c5312

14 files changed

+572
-565
lines changed

package-lock.json

Lines changed: 176 additions & 196 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"url-loader": "^1.0.1",
6868
"husky": "^0.14.3",
6969
"jest": "^23.4.0",
70-
"jest-serializer-path": "^0.1.15",
7170
"lint-staged": "^7.2.0",
7271
"memory-fs": "^0.4.1",
7372
"nsp": "^3.2.1",
@@ -90,11 +89,9 @@
9089
]
9190
},
9291
"jest": {
92+
"testURL": "http://localhost",
9393
"collectCoverageFrom": [
9494
"src/**/*.js"
95-
],
96-
"snapshotSerializers": [
97-
"jest-serializer-path"
9895
]
9996
}
10097
}

src/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ export default function loader(content, map, meta) {
162162
);
163163
})
164164
.catch((err) => {
165-
if (err.file) {
166-
this.addDependency(err.file);
167-
}
168-
169165
cb(err.name === 'CssSyntaxError' ? new SyntaxError(err) : err);
170166
});
171167
}

src/runtime.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ module.exports = function(useSourceMap) {
1919

2020
// import a list of modules into the list
2121
list.i = function(modules, mediaQuery) {
22-
if (typeof modules === "string") {
23-
modules = [[null, modules, ""]];
22+
if (typeof modules === 'string') {
23+
modules = [[null, modules, '']];
2424
}
2525

2626
const isImported = {};
2727

2828
for (let i = 0; i < this.length; i++) {
2929
const id = this[i][0];
3030

31-
if (typeof id === "number") {
31+
if (typeof id === 'number') {
3232
isImported[id] = true;
3333
}
3434
}
@@ -40,11 +40,11 @@ module.exports = function(useSourceMap) {
4040
// this implementation is not 100% perfect for weird media query combinations
4141
// when a module is imported multiple times with different media queries.
4242
// I hope this will never occur (Hey this way we have smaller bundles)
43-
if (typeof item[0] !== "number" || !isImported[item[0]]) {
43+
if (typeof item[0] !== 'number' || !isImported[item[0]]) {
4444
if (mediaQuery && !item[2]) {
4545
item[2] = mediaQuery;
4646
} else if (mediaQuery) {
47-
item[2] = "(" + item[2] + ") and (" + mediaQuery + ")";
47+
item[2] = '(' + item[2] + ') and (' + mediaQuery + ')';
4848
}
4949

5050
list.push(item);
@@ -56,34 +56,34 @@ module.exports = function(useSourceMap) {
5656
};
5757

5858
function cssWithMappingToString(item, useSourceMap) {
59-
const content = item[1] || "";
59+
const content = item[1] || '';
6060
const sourceMap = item[3];
6161

6262
if (!sourceMap) {
6363
return content;
6464
}
6565

66-
if (useSourceMap && typeof btoa === "function") {
66+
if (useSourceMap && typeof btoa === 'function') {
6767
const sourceMapping = toComment(sourceMap);
6868
const sourceURLs = sourceMap.sources.map(function(source) {
69-
return "/*# sourceURL=" + sourceMap.sourceRoot + source + " */";
69+
return '/*# sourceURL=' + sourceMap.sourceRoot + source + ' */';
7070
});
7171

7272
return [content]
7373
.concat(sourceURLs)
7474
.concat([sourceMapping])
75-
.join("\n");
75+
.join('\n');
7676
}
7777

78-
return [content].join("\n");
78+
return [content].join('\n');
7979
}
8080

8181
// Adapted from convert-source-map (MIT)
8282
function toComment(sourceMap) {
8383
// eslint-disable-next-line no-undef
8484
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
8585
const data =
86-
"sourceMappingURL=data:application/json;charset=utf-8;base64," + base64;
86+
'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
8787

88-
return "/*# " + data + " */";
88+
return '/*# ' + data + ' */';
8989
}

test/__snapshots__/import-option.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ exports[`import true from modules: warnings 1`] = `Array []`;
224224

225225
exports[`import true invalid: errors 1`] = `
226226
Array [
227-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
227+
[ModuleBuildError: Module build failed (from /home/evilebottnawi/IdeaProjects/css-loader/src/index.js):
228228
Syntax Error: Unexpected format (1:1)
229229
230230
> 1 | @import;

test/__snapshots__/loader.test.js.snap

Lines changed: 15 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ module.exports = function (useSourceMap) {
8383
8484
// import a list of modules into the list
8585
list.i = function (modules, mediaQuery) {
86-
if (typeof modules === \\"string\\") {
87-
modules = [[null, modules, \\"\\"]];
86+
if (typeof modules === 'string') {
87+
modules = [[null, modules, '']];
8888
}
8989
9090
const isImported = {};
9191
9292
for (let i = 0; i < this.length; i++) {
9393
const id = this[i][0];
9494
95-
if (typeof id === \\"number\\") {
95+
if (typeof id === 'number') {
9696
isImported[id] = true;
9797
}
9898
}
@@ -104,11 +104,11 @@ module.exports = function (useSourceMap) {
104104
// this implementation is not 100% perfect for weird media query combinations
105105
// when a module is imported multiple times with different media queries.
106106
// I hope this will never occur (Hey this way we have smaller bundles)
107-
if (typeof item[0] !== \\"number\\" || !isImported[item[0]]) {
107+
if (typeof item[0] !== 'number' || !isImported[item[0]]) {
108108
if (mediaQuery && !item[2]) {
109109
item[2] = mediaQuery;
110110
} else if (mediaQuery) {
111-
item[2] = \\"(\\" + item[2] + \\") and (\\" + mediaQuery + \\")\\";
111+
item[2] = '(' + item[2] + ') and (' + mediaQuery + ')';
112112
}
113113
114114
list.push(item);
@@ -120,32 +120,32 @@ module.exports = function (useSourceMap) {
120120
};
121121
122122
function cssWithMappingToString(item, useSourceMap) {
123-
const content = item[1] || \\"\\";
123+
const content = item[1] || '';
124124
const sourceMap = item[3];
125125
126126
if (!sourceMap) {
127127
return content;
128128
}
129129
130-
if (useSourceMap && typeof btoa === \\"function\\") {
130+
if (useSourceMap && typeof btoa === 'function') {
131131
const sourceMapping = toComment(sourceMap);
132132
const sourceURLs = sourceMap.sources.map(function (source) {
133-
return \\"/*# sourceURL=\\" + sourceMap.sourceRoot + source + \\" */\\";
133+
return '/*# sourceURL=' + sourceMap.sourceRoot + source + ' */';
134134
});
135135
136-
return [content].concat(sourceURLs).concat([sourceMapping]).join(\\"/n\\");
136+
return [content].concat(sourceURLs).concat([sourceMapping]).join('\\\\n');
137137
}
138138
139-
return [content].join(\\"/n\\");
139+
return [content].join('\\\\n');
140140
}
141141
142142
// Adapted from convert-source-map (MIT)
143143
function toComment(sourceMap) {
144144
// eslint-disable-next-line no-undef
145145
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
146-
const data = \\"sourceMappingURL=data:application/json;charset=utf-8;base64,\\" + base64;
146+
const data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
147147
148-
return \\"/*# \\" + data + \\" */\\";
148+
return '/*# ' + data + ' */';
149149
}"
150150
`;
151151

@@ -166,8 +166,8 @@ module.exports = function escape(url) {
166166
167167
// Should url be wrapped?
168168
// See https://drafts.csswg.org/css-values-3/#urls
169-
if (/[\\"'() /t/n]/.test(url)) {
170-
return '\\"' + url.replace(/\\"/g, '//\\"').replace(//n/g, '//n') + '\\"';
169+
if (/[\\"'() \\\\t\\\\n]/.test(url)) {
170+
return '\\"' + url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n') + '\\"';
171171
}
172172
173173
return url;
@@ -202,7 +202,7 @@ exports[`loader error when no loader for url assets: warnings 1`] = `Array []`;
202202

203203
exports[`loader error when source code is invalid: errors 1`] = `
204204
Array [
205-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
205+
[ModuleBuildError: Module build failed (from /home/evilebottnawi/IdeaProjects/css-loader/src/index.js):
206206
Syntax Error: Unknown word (2:3)
207207
208208
1 | .some {
@@ -292,130 +292,3 @@ a:hover {
292292
`;
293293

294294
exports[`loader using together with "postcss-loader" (reuse ast): warnings 1`] = `Array []`;
295-
296-
exports[`loader validate options import - false (boolean): errors 1`] = `Array []`;
297-
298-
exports[`loader validate options import - false (boolean): warnings 1`] = `Array []`;
299-
300-
exports[`loader validate options import - true (boolean): errors 1`] = `Array []`;
301-
302-
exports[`loader validate options import - true (boolean): warnings 1`] = `Array []`;
303-
304-
exports[`loader validate options import - true (string): errors 1`] = `
305-
Array [
306-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
307-
CSS Loader
308-
309-
Options Validation Error
310-
311-
options.import should be boolean
312-
313-
314-
options.import should be boolean
315-
at validate (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:87:15)
316-
at validator (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:118:10)
317-
at Object.loader (<PROJECT_ROOT>/src/index.js:29:3)],
318-
]
319-
`;
320-
321-
exports[`loader validate options import - true (string): warnings 1`] = `Array []`;
322-
323-
exports[`loader validate options importLoaders - 0 (number): errors 1`] = `Array []`;
324-
325-
exports[`loader validate options importLoaders - 0 (number): warnings 1`] = `Array []`;
326-
327-
exports[`loader validate options importLoaders - 1 (number): errors 1`] = `Array []`;
328-
329-
exports[`loader validate options importLoaders - 1 (number): warnings 1`] = `Array []`;
330-
331-
exports[`loader validate options importLoaders - 1 (string): errors 1`] = `
332-
Array [
333-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
334-
CSS Loader
335-
336-
Options Validation Error
337-
338-
options.importLoaders should be number
339-
340-
341-
options.importLoaders should be number
342-
at validate (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:87:15)
343-
at validator (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:118:10)
344-
at Object.loader (<PROJECT_ROOT>/src/index.js:29:3)],
345-
]
346-
`;
347-
348-
exports[`loader validate options importLoaders - 1 (string): warnings 1`] = `Array []`;
349-
350-
exports[`loader validate options sourceMap - false (boolean): errors 1`] = `Array []`;
351-
352-
exports[`loader validate options sourceMap - false (boolean): warnings 1`] = `Array []`;
353-
354-
exports[`loader validate options sourceMap - true (boolean): errors 1`] = `Array []`;
355-
356-
exports[`loader validate options sourceMap - true (boolean): warnings 1`] = `Array []`;
357-
358-
exports[`loader validate options sourceMap - true (string): errors 1`] = `
359-
Array [
360-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
361-
CSS Loader
362-
363-
Options Validation Error
364-
365-
options.sourceMap should be boolean
366-
367-
368-
options.sourceMap should be boolean
369-
at validate (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:87:15)
370-
at validator (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:118:10)
371-
at Object.loader (<PROJECT_ROOT>/src/index.js:29:3)],
372-
]
373-
`;
374-
375-
exports[`loader validate options sourceMap - true (string): warnings 1`] = `Array []`;
376-
377-
exports[`loader validate options unknown option: errors 1`] = `
378-
Array [
379-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
380-
CSS Loader
381-
382-
Options Validation Error
383-
384-
options['unknow'] is an invalid additional property
385-
386-
387-
options['unknow'] is an invalid additional property
388-
at validate (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:87:15)
389-
at validator (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:118:10)
390-
at Object.loader (<PROJECT_ROOT>/src/index.js:29:3)],
391-
]
392-
`;
393-
394-
exports[`loader validate options unknown option: warnings 1`] = `Array []`;
395-
396-
exports[`loader validate options url - false (boolean): errors 1`] = `Array []`;
397-
398-
exports[`loader validate options url - false (boolean): warnings 1`] = `Array []`;
399-
400-
exports[`loader validate options url - true (boolean): errors 1`] = `Array []`;
401-
402-
exports[`loader validate options url - true (boolean): warnings 1`] = `Array []`;
403-
404-
exports[`loader validate options url - true (string): errors 1`] = `
405-
Array [
406-
[ModuleBuildError: Module build failed (from <PROJECT_ROOT>/src/index.js):
407-
CSS Loader
408-
409-
Options Validation Error
410-
411-
options.url should be boolean
412-
413-
414-
options.url should be boolean
415-
at validate (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:87:15)
416-
at validator (<PROJECT_ROOT>/node_modules/@webpack-contrib/schema-utils/dist/validate-options.js:118:10)
417-
at Object.loader (<PROJECT_ROOT>/src/index.js:29:3)],
418-
]
419-
`;
420-
421-
exports[`loader validate options url - true (string): warnings 1`] = `Array []`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`runtime should import modules 1`] = `"body { b: 2; }body { c: 3; }@media print{body { d: 4; }}.a { color: red; }@media (screen) and (print){body { a: 1; }}@media (screen) and (print){body { a: 1; }}"`;
4+
5+
exports[`runtime toString with a single module 1`] = `"body { a: 1; }"`;
6+
7+
exports[`runtime toString with media query 1`] = `"@media screen{body { a: 1; }}"`;
8+
9+
exports[`runtime toString with multiple modules 1`] = `"body { b: 2; }body { a: 1; }"`;
10+
11+
exports[`runtime toString with source mapping 1`] = `
12+
"body { a: 1; }
13+
/*# sourceURL=webpack://./path/to/test.scss */
14+
/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC5zY3NzIiwic291cmNlcyI6WyIuL3BhdGgvdG8vdGVzdC5zY3NzIl0sIm1hcHBpbmdzIjoiQUFBQTsiLCJzb3VyY2VSb290Ijoid2VicGFjazovLyJ9 */"
15+
`;
16+
17+
exports[`runtime toString without source mapping if btoa not avalibale 1`] = `"body { a: 1; }"`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`runtimeEscape escape 1`] = `true`;
4+
5+
exports[`runtimeEscape escape 2`] = `"image.png"`;
6+
7+
exports[`runtimeEscape escape 3`] = `"image.png"`;
8+
9+
exports[`runtimeEscape escape 4`] = `"image.png"`;
10+
11+
exports[`runtimeEscape escape 5`] = `"\\"image other.png\\""`;
12+
13+
exports[`runtimeEscape escape 6`] = `"\\"image other.png\\""`;
14+
15+
exports[`runtimeEscape escape 7`] = `"\\"image other.png\\""`;
16+
17+
exports[`runtimeEscape escape 8`] = `"\\"image\\\\\\"other.png\\""`;
18+
19+
exports[`runtimeEscape escape 9`] = `"\\"image\\\\nother.png\\""`;

0 commit comments

Comments
 (0)