Skip to content

Commit c67e068

Browse files
committed
test: add test with module content
1 parent 7cbec4a commit c67e068

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

test/__mocks__/webpack.js

-3
This file was deleted.

test/__mocks__/webpack/lib/node/NodeTargetPlugin.js

-3
This file was deleted.

test/enforce-esm.test.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { getCompiler, compile } from './helpers';
2-
3-
jest.mock('webpack');
4-
jest.mock('webpack/lib/node/NodeTargetPlugin');
1+
import { getCompiler, source, compile } from './helpers';
52

63
it('should enforce esm for empty module with options.esModule', async (done) => {
74
const compiler = getCompiler(
@@ -14,18 +11,14 @@ it('should enforce esm for empty module with options.esModule', async (done) =>
1411
);
1512
const stats = await compile(compiler);
1613
expect(stats.hasErrors()).toBe(false);
17-
18-
const { chunks } = stats.toJson();
19-
const [chunk] = chunks;
20-
21-
expect(JSON.stringify(chunk.sizes)).toMatchInlineSnapshot(
22-
// javascript size is mostly a webpack info comments
23-
`"{\\"javascript\\":73,\\"unknown\\":23,\\"runtime\\":0}"`
24-
);
14+
expect(source('./simple.css', stats)).toMatchInlineSnapshot(`
15+
"// extracted by mini-css-extract-plugin
16+
export {};"
17+
`);
2518
done();
2619
});
2720

28-
it('should keep module without options.esModule', async (done) => {
21+
it('should keep empty module without options.esModule', async (done) => {
2922
const compiler = getCompiler(
3023
'./esm.js',
3124
{},
@@ -36,13 +29,8 @@ it('should keep module without options.esModule', async (done) => {
3629
);
3730
const stats = await compile(compiler);
3831
expect(stats.hasErrors()).toBe(false);
39-
40-
const { chunks } = stats.toJson();
41-
const [chunk] = chunks;
42-
43-
expect(JSON.stringify(chunk.sizes)).toMatchInlineSnapshot(
44-
// javascript size is mostly a webpack info comments
45-
`"{\\"javascript\\":62,\\"unknown\\":23,\\"runtime\\":657}"`
32+
expect(source('./simple.css', stats)).toMatchInlineSnapshot(
33+
`"// extracted by mini-css-extract-plugin"`
4634
);
4735
done();
4836
});

test/helpers/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import compile from './compile';
22
import getCompiler from './getCompiler';
3+
import source from './source';
34

4-
export { compile, getCompiler };
5+
export { source, compile, getCompiler };

test/helpers/source.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function getSource(name, stats) {
2+
const { modules } = stats.toJson({ source: true });
3+
4+
for (let i = 0; i < modules.length; i++) {
5+
const module = modules[i];
6+
7+
if (module.modules && module.modules.length > 0) {
8+
for (let j = 0; j < module.modules.length; j++) {
9+
if (module.modules[j].name === name) {
10+
return module.modules[j].source;
11+
}
12+
}
13+
} else if (module.name === name) {
14+
return module.source;
15+
}
16+
}
17+
18+
return undefined;
19+
}

0 commit comments

Comments
 (0)