Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 52c882e

Browse files
michael-ciniawskyjoshwiens
authored andcommittedNov 22, 2017
test: standardize test configuration (#214)
1 parent f321874 commit 52c882e

26 files changed

+3606
-1202
lines changed
 

‎package-lock.json

+3,170-985
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+20-26
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,29 @@
1111
"files": [
1212
"dist"
1313
],
14-
"directories": {
15-
"test": "test"
16-
},
1714
"dependencies": {
1815
"loader-utils": "^1.0.2",
1916
"schema-utils": "^0.3.0"
2017
},
2118
"devDependencies": {
22-
"babel-cli": "^6.24.1",
23-
"babel-jest": "^20.0.3",
24-
"babel-plugin-transform-object-rest-spread": "^6.23.0",
25-
"babel-polyfill": "^6.23.0",
26-
"babel-preset-env": "^1.5.1",
19+
"babel-cli": "^6.0.0",
20+
"babel-jest": "^21.0.0",
21+
"babel-plugin-transform-object-rest-spread": "^6.0.0",
22+
"babel-polyfill": "^6.0.0",
23+
"babel-preset-env": "^1.0.0",
2724
"cross-env": "^5.0.0",
25+
"del": "^3.0.0",
2826
"del-cli": "^1.0.0",
29-
"eslint": "^3.19.0",
30-
"eslint-config-webpack": "^1.2.3",
31-
"eslint-plugin-import": "^2.3.0",
32-
"jest": "^20.0.4",
33-
"lint-staged": "^3.6.0",
34-
"nsp": "^2.6.3",
35-
"pre-commit": "^1.2.2",
27+
"eslint": "^4.0.0",
28+
"eslint-config-webpack": "^1.0.0",
29+
"eslint-plugin-import": "^2.0.0",
30+
"jest": "^21.0.0",
31+
"lint-staged": "^5.0.0",
32+
"memory-fs": "^0.4.0",
33+
"nsp": "^2.0.0",
34+
"pre-commit": "^1.0.0",
3635
"standard-version": "^4.0.0",
37-
"webpack": "^3.6.0",
36+
"webpack": "^3.0.0",
3837
"webpack-defaults": "^1.6.0"
3938
},
4039
"peerDependencies": {
@@ -54,19 +53,14 @@
5453
"test": "jest",
5554
"test:watch": "jest --watch",
5655
"test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage",
57-
"travis:coverage": "npm run test:coverage -- --runInBand",
58-
"travis:test": "npm run test -- --runInBand",
5956
"travis:lint": "npm run lint && npm run security",
57+
"travis:test": "npm run test -- --runInBand",
58+
"travis:coverage": "npm run test:coverage -- --runInBand",
6059
"webpack-defaults": "webpack-defaults"
6160
},
62-
"repository": {
63-
"type": "git",
64-
"url": "https://github.com/webpack/file-loader.git"
65-
},
66-
"bugs": {
67-
"url": "https://github.com/webpack/file-loader/issues"
68-
},
69-
"homepage": "https://github.com/webpack/file-loader",
61+
"repository": "https://github.com/webpack/file-loader.git",
62+
"bugs": "https://github.com/webpack/file-loader/issues",
63+
"homepage": "https://webpack.js.org/loaders/file-loader",
7064
"pre-commit": "lint-staged",
7165
"lint-staged": {
7266
"*.js": [

‎test/Errors.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import loader from '../src';
22

33
describe('Errors', () => {
4-
test('Validation Error', () => {
5-
const err = () => loader.call({
6-
query: { useRelativePath: 1 },
7-
emitFile: true,
8-
});
4+
test('Loader Error', () => {
5+
const err = () => loader.call({ emitFile: false });
96

107
expect(err).toThrow();
118
expect(err).toThrowErrorMatchingSnapshot();
129
});
1310

14-
test('Loader Error', () => {
15-
const err = () => loader.call({ emitFile: false });
11+
test('Validation Error', () => {
12+
const err = () => loader.call({
13+
query: { useRelativePath: 1 },
14+
emitFile: true,
15+
});
1616

1717
expect(err).toThrow();
1818
expect(err).toThrowErrorMatchingSnapshot();
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Loader Defaults 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;

‎test/cjs.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import fileLoader from '../src';
2-
import cjsFileLoader from '../src/cjs';
1+
import loader from '../src';
2+
import CJSLoader from '../src/cjs';
33

4-
describe('cjs', () => {
4+
describe('CJS', () => {
55
it('should export loader', () => {
6-
expect(cjsFileLoader).toEqual(fileLoader);
6+
expect(CJSLoader).toEqual(loader);
77
});
88

99
it('should export `raw` flag', () => {
10-
expect(cjsFileLoader.raw).toEqual(true);
10+
expect(CJSLoader.raw).toEqual(true);
1111
});
1212
});

‎test/emitFile.test.js

-30
This file was deleted.

‎test/fixtures/emitFile/fixture.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable */
2+
import svg from '../file.svg';
3+
4+
export default svg

‎test/fixtures/file.gif

13 KB
Loading

‎test/fixtures/file.jpg

12.5 KB
Loading

‎test/fixtures/file.png

6.62 KB
Loading

‎test/fixtures/file.svg

+48
Loading

‎test/fixtures/fixture.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable */
2+
import png from './file.png';
3+
4+
export default png;

‎test/helpers/compiler.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable
2+
import/order,
3+
multiline-ternary,
4+
no-param-reassign,
5+
*/
6+
import del from 'del';
7+
import path from 'path';
8+
import webpack from 'webpack';
9+
import MemoryFS from 'memory-fs';
10+
11+
const module = (config) => {
12+
return {
13+
rules: config.rules || config.loader
14+
? [
15+
{
16+
test: config.loader.test || /\.txt$/,
17+
use: {
18+
loader: path.resolve(__dirname, '../../src'),
19+
options: config.loader.options || {},
20+
},
21+
},
22+
]
23+
: [],
24+
};
25+
};
26+
27+
const plugins = config => ([
28+
new webpack.optimize.CommonsChunkPlugin({
29+
name: ['runtime'],
30+
minChunks: Infinity,
31+
}),
32+
].concat(config.plugins || []));
33+
34+
const output = (config) => {
35+
return {
36+
path: path.resolve(
37+
__dirname,
38+
`../outputs/${config.output ? config.output : ''}`,
39+
),
40+
filename: '[name].bundle.js',
41+
};
42+
};
43+
44+
export default function (fixture, config, options) {
45+
// webpack Config
46+
config = {
47+
devtool: config.devtool || 'sourcemap',
48+
context: path.resolve(__dirname, '..', 'fixtures'),
49+
entry: `./${fixture}`,
50+
output: output(config),
51+
module: module(config),
52+
plugins: plugins(config),
53+
};
54+
// Compiler Options
55+
options = Object.assign({ output: false }, options);
56+
57+
if (options.output) del.sync(config.output.path);
58+
59+
const compiler = webpack(config);
60+
61+
if (!options.output) compiler.outputFileSystem = new MemoryFS();
62+
63+
return new Promise((resolve, reject) => compiler.run((err, stats) => {
64+
if (err) reject(err);
65+
66+
resolve(stats);
67+
}));
68+
}

‎test/loader.test.js

+18-149
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,20 @@
1-
/* eslint-disable no-useless-escape, no-unused-vars */
2-
import loader from '../src';
3-
4-
const run = function run(resourcePath, query, content = new Buffer('1234')) {
5-
let file = null;
6-
7-
const context = {
8-
resourcePath,
9-
query: `?${query || ''}`,
10-
options: {
11-
context: '/this/is/the/context',
12-
},
13-
emitFile(url, content2) {
14-
expect(content2).toEqual(content);
15-
file = url;
16-
},
17-
};
18-
19-
const result = loader.call(context, content);
20-
21-
return {
22-
file,
23-
result,
24-
};
25-
};
26-
27-
function runWithOptions(resourcePath, options, content = new Buffer('1234')) {
28-
let file = null;
29-
30-
const context = {
31-
resourcePath,
32-
query: options,
33-
options: {
34-
context: '/this/is/the/context',
35-
},
36-
emitFile(url, content2) {
37-
expect(content2).toEqual(content);
38-
file = url;
39-
},
40-
};
41-
42-
const result = loader.call(context, content);
43-
44-
return {
45-
file,
46-
result,
47-
};
48-
}
49-
const test = function test(excepted, resourcePath, query, content) {
50-
expect(run(resourcePath, query, content).file).toEqual(excepted);
51-
};
52-
53-
describe('correct-filename', () => {
54-
it('should process defaults correctly', () => {
55-
test('81dc9bdb52d04dc20036dbd8313ed055.txt', '/file.txt', '');
56-
test('81dc9bdb52d04dc20036dbd8313ed055.png', '/file.png', '');
57-
test('81dc9bdb52d04dc20036dbd8313ed055.txt', 'file.txt', '');
58-
test('81dc9bdb52d04dc20036dbd8313ed055.bin', '', '');
59-
});
60-
61-
it('should process name correctly', () => {
62-
test('file.txt', '/file.txt', 'name=[name].[ext]');
63-
test('file.png', '/file.png', 'name=[name].[ext]');
64-
test('file.txt', 'file.txt', 'name=[name].[ext]');
65-
test('file.bin', '', 'name=[name].[ext]');
66-
test('file', '/file.txt', 'name=[name]');
67-
test('81dc9bdb52d04dc20036dbd8313ed055', '/file.txt', 'name=[hash]');
68-
test('81dc9bdb52d04dc20036dbd8313ed055/file.txt', '/file.txt', 'name=[hash]/[name].[ext]');
69-
test('file.txt', '/this/is/the/context/file.txt', 'name=[path][name].[ext]');
70-
test('dir/file.txt', '/this/is/the/context/dir/file.txt', 'name=[path][name].[ext]');
71-
test('dir/sub/file.txt', '/this/is/the/context/dir/sub/file.txt', 'name=[path][name].[ext]');
72-
test('_/_/dir/sub/file.txt', '/this/is/dir/sub/file.txt', 'name=[path][name].[ext]');
73-
test('dir/sub/file.txt', '/this/is/dir/sub/file.txt', 'name=[path][name].[ext]&context=/this/is');
74-
});
75-
76-
it('should process hash correctly', () => {
77-
test('d93591bdf7860e1e4ee2fca799911215.txt', '/file.txt', '', new Buffer('4321'));
78-
});
79-
80-
it('should process hash options correctly', () => {
81-
test('81dc9.txt', '/file.txt', 'name=[hash:5].[ext]');
82-
test('d4045.txt', '/file.txt', 'name=[sha512:hash:5].[ext]');
83-
test('1lQ3UNSdIS0c9dQ5brCZO1.txt', '/file.txt', 'name=[hash:base64].[ext]');
84-
test('caYJDUvUOiGAdDsiHKffIEj.txt', '/file.txt', 'name=[hash:base52].[ext]');
85-
test('sntmopgidsdqrofkjywoyldtiij.txt', '/file.txt', 'name=[hash:base26].[ext]');
86-
test('sntmopgids.txt', '/file.txt', 'name=[hash:base26:10].[ext]');
87-
});
88-
});
89-
90-
describe('publicPath option', () => {
91-
it('should be supported', () => {
92-
expect(run('/file.txt', 'publicPath=http://cdn/').result).toEqual(
93-
'module.exports = "http://cdn/81dc9bdb52d04dc20036dbd8313ed055.txt";',
94-
);
95-
});
96-
97-
it('should override public path when given empty string', () => {
98-
expect(run('/file.txt', 'publicPath=').result).toEqual(
99-
'module.exports = "81dc9bdb52d04dc20036dbd8313ed055.txt";',
100-
);
101-
});
102-
103-
it('should use webpack public path when not set', () => {
104-
expect(run('/file.txt').result).toEqual(
105-
'module.exports = __webpack_public_path__ + "81dc9bdb52d04dc20036dbd8313ed055.txt";',
106-
);
107-
});
108-
});
109-
110-
describe('useRelativePath option', () => {
111-
it('should be supported', () => {
112-
expect(run('/this/is/the/context/file.txt', 'useRelativePath=true').result).toEqual(
113-
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";',
114-
);
115-
116-
expect(run('/this/is/file.txt', 'useRelativePath=true').result).toEqual(
117-
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";',
118-
);
119-
120-
expect(run('/this/file.txt', 'context=/this/is/the/&useRelativePath=true').result).toEqual(
121-
'module.exports = __webpack_public_path__ + \"../../81dc9bdb52d04dc20036dbd8313ed055.txt\";',
122-
);
123-
124-
expect(run('/this/file.txt', 'context=/&useRelativePath=true').result).toEqual(
125-
'module.exports = __webpack_public_path__ + \"this/81dc9bdb52d04dc20036dbd8313ed055.txt\";',
126-
);
127-
});
128-
});
129-
130-
describe('outputPath function', () => {
131-
it('should be supported', () => {
132-
const options = {};
133-
options.outputPath = value => '/path/set/by/func';
134-
135-
expect(runWithOptions('/this/is/the/context/file.txt', options).result)
136-
.toEqual(
137-
'module.exports = __webpack_public_path__ + \"/path/set/by/func\";',
138-
);
139-
});
140-
141-
it('should be ignored if you set useRelativePath', () => {
142-
const options = {};
143-
options.outputPath = value => '/path/set/by/func';
144-
options.useRelativePath = true;
145-
146-
expect(runWithOptions('/this/is/the/context/file.txt', options).result)
147-
.toEqual(
148-
'module.exports = __webpack_public_path__ + \"./81dc9bdb52d04dc20036dbd8313ed055.txt\";',
149-
);
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from './helpers/compiler';
5+
6+
describe('Loader', () => {
7+
test('Defaults', async () => {
8+
const config = {
9+
loader: {
10+
test: /(png|jpg|svg)/,
11+
options: {},
12+
},
13+
};
14+
15+
const stats = await webpack('fixture.js', config);
16+
const { source } = stats.toJson().modules[1];
17+
18+
expect(source).toMatchSnapshot();
15019
});
15120
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options context {String} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options emitFile {Boolean} True (Default) 1`] = `"64eababb117f90535085779cc0325003.svg"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options name {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
4+
5+
exports[`Options name {String} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options outputPath {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
4+
5+
exports[`Options outputPath {String} 1`] = `"module.exports = __webpack_public_path__ + \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options publicPath {Function} 1`] = `"module.exports = \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
4+
5+
exports[`Options publicPath {String} 1`] = `"module.exports = \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Options useRelativePath options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
4+
5+
exports[`Options useRelativePath this.options.context 1`] = `"module.exports = __webpack_public_path__ + \\"./9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;

‎test/options/context.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('context', () => {
8+
test('{String}', async () => {
9+
const config = {
10+
loader: {
11+
test: /(png|jpg|svg)/,
12+
options: {
13+
context: `${__dirname}`,
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
});
24+
});

‎test/options/emitFile.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('emitFile', () => {
8+
describe('{Boolean}', () => {
9+
test('True (Default)', async () => {
10+
const config = {
11+
loader: {
12+
test: /(png|jpg|svg)/,
13+
options: {
14+
emitFile: true,
15+
},
16+
},
17+
};
18+
19+
const stats = await webpack('emitFile/fixture.js', config);
20+
const { assets } = stats.toJson().modules[1];
21+
22+
expect(assets[0]).toMatchSnapshot();
23+
});
24+
25+
test('False', async () => {
26+
const config = {
27+
loader: {
28+
test: /(png|jpg|svg)/,
29+
options: {
30+
emitFile: false,
31+
},
32+
},
33+
};
34+
35+
const stats = await webpack('emitFile/fixture.js', config);
36+
const { assets } = stats.toJson().modules[1];
37+
38+
expect(assets[0]).toBe(undefined);
39+
});
40+
});
41+
});
42+
});

‎test/options/name.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('name', () => {
8+
test('{String}', async () => {
9+
const config = {
10+
loader: {
11+
test: /(png|jpg|svg)/,
12+
options: {
13+
name: '[hash].[ext]',
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
24+
test('{Function}', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
name() {
30+
return '[hash].[ext]';
31+
},
32+
},
33+
},
34+
};
35+
36+
const stats = await webpack('fixture.js', config);
37+
const { source } = stats.toJson().modules[1];
38+
39+
expect(source).toMatchSnapshot();
40+
});
41+
});
42+
});

‎test/options/outputPath.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('outputPath', () => {
8+
test('{String}', async () => {
9+
const config = {
10+
loader: {
11+
test: /(png|jpg|svg)/,
12+
options: {
13+
outputPath: '/test/',
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
24+
test('{Function}', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
outputPath(url) {
30+
return `test/${url}`;
31+
},
32+
},
33+
},
34+
};
35+
36+
const stats = await webpack('fixture.js', config);
37+
const { source } = stats.toJson().modules[1];
38+
39+
expect(source).toMatchSnapshot();
40+
});
41+
});
42+
});

‎test/options/publicPath.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('publicPath', () => {
8+
test('{String}', async () => {
9+
const config = {
10+
loader: {
11+
test: /(png|jpg|svg)/,
12+
options: {
13+
publicPath: '/test/',
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
24+
test('{Function}', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
publicPath(url) {
30+
return `test/${url}`;
31+
},
32+
},
33+
},
34+
};
35+
36+
const stats = await webpack('fixture.js', config);
37+
const { source } = stats.toJson().modules[1];
38+
39+
expect(source).toMatchSnapshot();
40+
});
41+
});
42+
});

‎test/options/useRelativePath.test.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable
2+
prefer-destructuring,
3+
*/
4+
import webpack from '../helpers/compiler';
5+
6+
describe('Options', () => {
7+
describe('useRelativePath', () => {
8+
test('this.options.context', async () => {
9+
const config = {
10+
loader: {
11+
test: /(png|jpg|svg)/,
12+
options: {
13+
useRelativePath: true,
14+
},
15+
},
16+
};
17+
18+
const stats = await webpack('fixture.js', config);
19+
const { source } = stats.toJson().modules[1];
20+
21+
expect(source).toMatchSnapshot();
22+
});
23+
24+
test('options.context', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
context: '/relative/',
30+
useRelativePath: true,
31+
},
32+
},
33+
};
34+
35+
const stats = await webpack('fixture.js', config);
36+
const { source } = stats.toJson().modules[1];
37+
38+
expect(source).toMatchSnapshot();
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)
This repository has been archived.