Skip to content

Commit 1f1da6e

Browse files
committed
test: refactor to use chain function 🎨
- implement chain-of-responsibility pattern - the chainer are called `on` function - ignore prettier rule on test files - change loader.test.js to use `on` function
1 parent ebc6cb6 commit 1f1da6e

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

‎.eslintrc.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ module.exports = {
88
{ singleQuote: true, trailingComma: 'es5', arrowParens: 'always' },
99
],
1010
'prefer-destructuring': ['error', {'object': true, 'array': false}],
11+
'newline-per-chained-call': ['error', { 'ignoreChainWithDepth': 4 }],
1112
'line-comment-position': ['off'],
1213
},
14+
overrides: [{
15+
files: ['*.test.*', 'on.js'],
16+
rules: {
17+
'semi': ['error', 'always', { 'omitLastInOneLineBlock': true }],
18+
'quotes': ['error', 'single'],
19+
'comma-dangle': ['error', 'always-multiline'],
20+
'arrow-parens': ['error', 'as-needed'],
21+
'prettier/prettier': 'off',
22+
}
23+
}]
1324
};

‎.prettierignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.test.*

‎test/helpers/on.js‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { statSync } from 'fs';
2+
import { resolve, dirname } from 'path';
3+
4+
// TODO: test approximate size (need ability to convert Unit, e.g kb to KB)
5+
6+
function inLoopExpect(array, prop, isNot = false) {
7+
return {
8+
get not() {
9+
return inLoopExpect(array, prop, true);
10+
},
11+
12+
toContain(expected) {
13+
for (const element of array) {
14+
if (isNot) {
15+
if (prop) expect(element[prop]).not.toContain(expected);
16+
else expect(element).not.toContain(expected);
17+
} else if (prop) expect(element[prop]).toContain(expected);
18+
else expect(element).toContain(expected);
19+
}
20+
},
21+
22+
toBeLessThan(expected) {
23+
for (const element of array) {
24+
if (isNot) {
25+
if (prop) expect(element[prop]).not.toBeLessThan(expected);
26+
else expect(element).not.toBeLessThan(expected);
27+
} else if (prop) expect(element[prop]).toBeLessThan(expected);
28+
else expect(element).toBeLessThan(expected);
29+
}
30+
},
31+
32+
toMatchSnapshot() {
33+
for (const element of array) {
34+
if (isNot) {
35+
if (prop) expect(element[prop]).not.toMatchSnapshot();
36+
else expect(element).not.toMatchSnapshot();
37+
} else if (prop) expect(element[prop]).toMatchSnapshot();
38+
else expect(element).toMatchSnapshot();
39+
}
40+
},
41+
};
42+
}
43+
44+
function chainer(statModules) {
45+
return {
46+
get: prop => inLoopExpect(statModules, prop),
47+
get source() { return inLoopExpect(statModules, 'source') },
48+
get providedExports() { return inLoopExpect(statModules, 'providedExports') },
49+
50+
get originSize() {
51+
return inLoopExpect(
52+
statModules.map(({ issuer, name }) => statSync(resolve(dirname(issuer), name)).size)
53+
);
54+
},
55+
56+
withExtension(extension) {
57+
return chainer(
58+
statModules.filter(({ name }) => name.includes(extension))
59+
);
60+
},
61+
62+
withoutExtension(extension) {
63+
return chainer(
64+
statModules.filter(({ name }) => !name.includes(extension))
65+
);
66+
},
67+
};
68+
}
69+
70+
export default function(stats) {
71+
return chainer(stats.toJson().modules);
72+
}

‎test/loader.test.js‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import webpack from './helpers/compiler';
2+
import on from './helpers/on';
23

34
describe('Loader', () => {
45
describe('Defaults', () => {
@@ -10,38 +11,22 @@ describe('Loader', () => {
1011

1112
test('match snapshots', async () => {
1213
const stats = await webpack('fixture.js', config);
13-
for (const { source } of stats.toJson().modules)
14-
expect(source).toMatchSnapshot();
14+
on(stats).source.toMatchSnapshot();
1515
});
1616

1717
test('indeed from rust code', async () => {
1818
const stats = await webpack('fixture.js', config);
19-
stats
20-
.toJson()
21-
.modules.filter(({ name }) => name.includes('.rust.wasm'))
22-
.forEach(({ providedExports }) =>
23-
expect(providedExports).toContain('rust_eh_personality')
24-
);
19+
on(stats).withExtension('.rust.wasm').providedExports.toContain('rust_eh_personality');
2520
});
2621

2722
test('not from rust code', async () => {
2823
const stats = await webpack('fixture.js', config);
29-
stats
30-
.toJson()
31-
.modules.filter(({ name }) => !name.includes('.rust.wasm'))
32-
.forEach(({ providedExports }) =>
33-
expect(providedExports).not.toContain('rust_eh_personality')
34-
);
24+
on(stats).withoutExtension('.rust.wasm').providedExports.not.toContain('rust_eh_personality');
3525
});
3626

3727
test('all wasm must specify the allocated memory', async () => {
3828
const stats = await webpack('fixture.js', config);
39-
stats
40-
.toJson()
41-
.modules.filter(({ name }) => name.includes('.wasm'))
42-
.forEach(({ providedExports }) =>
43-
expect(providedExports).toContain('memory')
44-
);
29+
on(stats).withExtension('.wasm').providedExports.toContain('memory');
4530
});
4631
});
4732
});

0 commit comments

Comments
 (0)