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

Commit 74c41c5

Browse files
feat: improve tests
1 parent a4db4c2 commit 74c41c5

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

templates/test/fixtures/foo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log('foo');
1+
export default 'foo';

templates/test/fixtures/simple.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import foo from './foo';
22

3+
__export__ = foo;
4+
35
export default foo;

templates/test/helpers/execute.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Module from 'module';
2+
import path from 'path';
3+
4+
const parentModule = module;
5+
6+
export default (code) => {
7+
const resource = 'test.js';
8+
const module = new Module(resource, parentModule);
9+
// eslint-disable-next-line no-underscore-dangle
10+
module.paths = Module._nodeModulePaths(
11+
path.resolve(__dirname, '../fixtures')
12+
);
13+
module.filename = resource;
14+
15+
// eslint-disable-next-line no-underscore-dangle
16+
module._compile(
17+
`let __export__;${code};module.exports = __export__;`,
18+
resource
19+
);
20+
21+
return module.exports;
22+
};

templates/test/helpers/getCompiler.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22

33
import webpack from 'webpack';
4-
import memfs from 'memfs';
4+
import { createFsFromVolume, Volume } from 'memfs';
55

66
export default (fixture, loaderOptions = {}, config = {}) => {
77
const fullConfig = {
@@ -34,9 +34,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
3434
const compiler = webpack(fullConfig);
3535

3636
if (!config.outputFileSystem) {
37-
const outputFileSystem = memfs;
38-
39-
outputFileSystem.vol.reset();
37+
const outputFileSystem = createFsFromVolume(new Volume());
4038
// Todo remove when we drop webpack@4 support
4139
outputFileSystem.join = path.join.bind(path);
4240

templates/test/loader.test.js

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

39
describe('loader', () => {
410
it('should work', async () => {
511
const compiler = getCompiler('simple.js');
612
const stats = await compile(compiler);
713

8-
expect(readAsset('main.bundle.js', compiler, stats)).toMatchSnapshot(
9-
'result'
10-
);
14+
expect(
15+
execute(readAsset('main.bundle.js', compiler, stats))
16+
).toMatchSnapshot('result');
1117
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
1218
'warnings'
1319
);

0 commit comments

Comments
 (0)