Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit fda4d2e

Browse files
feat: more testing helpers
1 parent 9b0db8f commit fda4d2e

File tree

8 files changed

+103
-35
lines changed

8 files changed

+103
-35
lines changed
Lines changed: 5 additions & 0 deletions
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);
5+
};
Lines changed: 5 additions & 0 deletions
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);
5+
};

templates/test/helpers/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
import compile from './compile';
2+
import execute from './execute';
23
import getCompiler from './getCompiler';
4+
import getErrors from './getErrors';
5+
import getWarnings from './getWarnings';
36
import normalizeErrors from './normalizeErrors';
47
import readAsset from './readAsset';
8+
import readsAssets from './readAssets';
59

6-
export { compile, getCompiler, normalizeErrors, readAsset };
10+
export {
11+
compile,
12+
execute,
13+
getCompiler,
14+
getErrors,
15+
getWarnings,
16+
normalizeErrors,
17+
readAsset,
18+
readsAssets,
19+
};

templates/test/helpers/readAsset.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import path from 'path';
33
export default (asset, compiler, stats) => {
44
const usedFs = compiler.outputFileSystem;
55
const outputPath = stats.compilation.outputOptions.path;
6+
67
let data = '';
8+
let targetFile = asset;
9+
10+
const queryStringIdx = targetFile.indexOf('?');
11+
12+
if (queryStringIdx >= 0) {
13+
targetFile = targetFile.substr(0, queryStringIdx);
14+
}
715

816
try {
9-
data = usedFs.readFileSync(path.join(outputPath, asset)).toString();
17+
data = usedFs.readFileSync(path.join(outputPath, targetFile)).toString();
1018
} catch (error) {
1119
data = error.toString();
1220
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import readAsset from './readAsset';
2+
3+
export default function readAssets(compiler, stats) {
4+
const assets = {};
5+
6+
Object.keys(stats.compilation.assets).forEach((asset) => {
7+
assets[asset] = readAsset(asset, compiler, stats);
8+
});
9+
10+
return assets;
11+
}

templates/test/loader.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
compile,
33
execute,
44
getCompiler,
5-
normalizeErrors,
5+
getErrors,
6+
getWarnings,
67
readAsset,
78
} from './helpers';
89

@@ -14,9 +15,7 @@ describe('loader', () => {
1415
expect(
1516
execute(readAsset('main.bundle.js', compiler, stats))
1617
).toMatchSnapshot('result');
17-
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
18-
'warnings'
19-
);
20-
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
18+
expect(getErrors(stats)).toMatchSnapshot('errors');
19+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
2120
});
2221
});

templates/test/name-option.test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { compile, getCompiler, normalizeErrors, readAsset } from './helpers';
1+
import {
2+
compile,
3+
execute,
4+
getCompiler,
5+
getErrors,
6+
getWarnings,
7+
readAsset,
8+
} from './helpers';
29

310
describe('"name" option', () => {
411
it('should work with "Boolean" value equal "true"', async () => {
@@ -7,12 +14,10 @@ describe('"name" option', () => {
714
});
815
const stats = await compile(compiler);
916

10-
expect(readAsset('main.bundle.js', compiler, stats)).toMatchSnapshot(
11-
'result'
12-
);
13-
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
14-
'warnings'
15-
);
16-
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
17+
expect(
18+
execute(readAsset('main.bundle.js', compiler, stats))
19+
).toMatchSnapshot('result');
20+
expect(getErrors(stats)).toMatchSnapshot('errors');
21+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
1722
});
1823
});

templates/test/validate-options.test.js

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,48 @@ describe('validate options', () => {
2323
it(`should ${
2424
type === 'success' ? 'successfully validate' : 'throw an error on'
2525
} the "${key}" option with "${stringifyValue(value)}" value`, async () => {
26-
const compiler = getCompiler('simple.js', { [key]: value });
27-
28-
let stats;
29-
30-
try {
31-
stats = await compile(compiler);
32-
} finally {
33-
if (type === 'success') {
34-
expect(stats.hasErrors()).toBe(false);
35-
} else if (type === 'failure') {
36-
const {
37-
compilation: { errors },
38-
} = stats;
39-
40-
expect(errors).toHaveLength(1);
41-
expect(() => {
42-
throw new Error(errors[0].error.message);
43-
}).toThrowErrorMatchingSnapshot();
44-
}
45-
}
26+
// For loaders
27+
// const compiler = getCompiler('simple.js', { [key]: value });
28+
//
29+
// let stats;
30+
//
31+
// try {
32+
// stats = await compile(compiler);
33+
// } finally {
34+
// if (type === 'success') {
35+
// expect(stats.hasErrors()).toBe(false);
36+
// } else if (type === 'failure') {
37+
// const {
38+
// compilation: { errors },
39+
// } = stats;
40+
//
41+
// expect(errors).toHaveLength(1);
42+
// expect(() => {
43+
// throw new Error(errors[0].error.message);
44+
// }).toThrowErrorMatchingSnapshot();
45+
// }
46+
// }
47+
// For plugins
48+
// let error;
49+
//
50+
// try {
51+
// // eslint-disable-next-line no-new
52+
// new Plugin({ [key]: value });
53+
// } catch (errorFromPlugin) {
54+
// if (errorFromPlugin.name !== 'ValidationError') {
55+
// throw errorFromPlugin;
56+
// }
57+
//
58+
// error = errorFromPlugin;
59+
// } finally {
60+
// if (type === 'success') {
61+
// expect(error).toBeUndefined();
62+
// } else if (type === 'failure') {
63+
// expect(() => {
64+
// throw error;
65+
// }).toThrowErrorMatchingSnapshot();
66+
// }
67+
// }
4668
});
4769
}
4870

0 commit comments

Comments
 (0)