Skip to content

Commit

Permalink
refactor: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 17, 2018
1 parent eab41ba commit 49482d2
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 11 deletions.
2 changes: 1 addition & 1 deletion test/SyntaxError.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { normalizeErrors } from './helpers/utils';

describe('SyntaxError', () => {
test('basic', async () => {
const stats = await webpack('broken.js');
const stats = await webpack('broken.css');

expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
Expand Down
191 changes: 191 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,196 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader basic (css endpoint): errors 1`] = `Array []`;

exports[`loader basic (css endpoint): module 1`] = `
Array [
Array [
1,
"@charset \\"UTF-8\\";
.class {
color: red;
background: url({./url/img.png});
}
.u-m\\\\+ {
a: b c d;
}
.grid.\\\\-top {
a: b c d;
}
.u-m\\\\00002b {
a: b c d;
}
.class {
content: '\\"\\\\\\\\f10c\\"';
}
.class {
font-family: '微软雅黑';
}
.class {
content: '\\\\e901';
}
.class {
content: \\"\\\\F10C\\"
}
.class {
content: \\"\\\\f10C\\"
}
.class {
content: \\"\\\\F10C \\\\F10D\\"
}
:root {
--foo: 1px;
--bar: 2px;
}
:root {
--title-align: center;
--sr-only: {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
clip-path: inset(50%);
border: 0;
};
}
",
"",
],
]
`;

exports[`loader basic (css endpoint): runtime 1`] = `
"'use strict';
/* eslint-disable */
// CSS Loader (Runtime)
module.exports = function (useSourceMap) {
const list = [];
// Return the list of modules as css string
list.toString = function toString() {
return this.map(function (item) {
const content = cssWithMappingToString(item, useSourceMap);
if (item[2]) {
return '@media ' + item[2] + '{' + content + '}';
}
return content;
}).join('');
};
// Import a list of modules into the list
list.i = function (modules, mediaQuery) {
if (typeof modules === 'string') {
modules = [[null, modules, '']];
}
const isImported = {};
for (let i = 0; i < this.length; i++) {
const id = this[i][0];
if (typeof id === 'number') {
isImported[id] = true;
}
}
for (let i = 0; i < modules.length; i++) {
const item = modules[i];
// Skip already imported module.
// This implementation is not 100% perfect for weird media query combinations
// when a module is imported multiple times with different media queries.
// I hope this will never occur (Hey this way we have smaller bundles).
if (typeof item[0] !== 'number' || !isImported[item[0]]) {
if (mediaQuery && !item[2]) {
item[2] = mediaQuery;
} else if (mediaQuery) {
item[2] = '(' + item[2] + ') and (' + mediaQuery + ')';
}
list.push(item);
}
}
};
return list;
};
function cssWithMappingToString(item, useSourceMap) {
const content = item[1] || '';
const sourceMap = item[3];
if (!sourceMap) {
return content;
}
if (useSourceMap && typeof btoa === 'function') {
const sourceMapping = toComment(sourceMap);
const sourceURLs = sourceMap.sources.map(function (source) {
return '/*# sourceURL=' + sourceMap.sourceRoot + source + ' */';
});
return [content].concat(sourceURLs).concat([sourceMapping]).join('\\\\n');
}
return [content].join('\\\\n');
}
// Adapted from convert-source-map (MIT)
function toComment(sourceMap) {
// eslint-disable-next-line no-undef
const base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));
const data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
return '/*# ' + data + ' */';
}"
`;

exports[`loader basic (css endpoint): runtimeEscape 1`] = `
"'use strict';
/* eslint-disable */
module.exports = function escape(url) {
if (typeof url !== 'string') {
return url;
}
// If url is already wrapped in quotes, remove them
if (/^['\\"].*['\\"]$/.test(url)) {
url = url.slice(1, -1);
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url)) {
return '\\"' + url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n') + '\\"';
}
return url;
};"
`;

exports[`loader basic (css endpoint): warnings 1`] = `Array []`;

exports[`loader basic: errors 1`] = `Array []`;

exports[`loader basic: module 1`] = `
Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/broken.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/empty.js

This file was deleted.

16 changes: 14 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ describe('loader', () => {
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

test('basic (css endpoint)', async () => {
const stats = await webpack('basic.css');
const [runtime, runtimeEscape, module] = stats.toJson().modules;

expect(runtime.source).toMatchSnapshot('runtime');
expect(runtimeEscape.source).toMatchSnapshot('runtimeEscape');
expect(evaluated(module.source)).toMatchSnapshot('module');

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

test('empty options', async () => {
const stats = await webpack('empty.js');
const stats = await webpack('empty.css');
const [, module] = stats.toJson().modules;

expect(evaluated(module.source)).toMatchSnapshot('module');
Expand All @@ -39,7 +51,7 @@ describe('loader', () => {
},
],
};
const stats = await webpack('basic.js', config);
const stats = await webpack('basic.css', config);

expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
Expand Down

0 comments on commit 49482d2

Please sign in to comment.