Skip to content

Commit 6d066c5

Browse files
committed
test(negative): add test for all possible errors
1 parent b8464f1 commit 6d066c5

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

test/fixtures/invalid/fixture.js

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

test/fixtures/invalid/foo.wasm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint no-console: "off"*/
2+
console.log('foo');

test/fixtures/over4KB.wasm

13.8 KB
Binary file not shown.

test/negative.test.ts

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,95 @@
1-
import webpack from '@webpack-contrib/test-utils';
1+
// tslint:disable:no-eval
2+
import 'jest-extended';
3+
import { resolve } from 'path';
4+
import { readFileSync } from 'fs';
5+
import webpack = require('@webpack-contrib/test-utils');
6+
import on from './helpers/on';
7+
import { exportOptions } from './test-list';
8+
import { wasm2js } from '../dist';
29

3-
// tslint:disable-next-line:no-empty
4-
describe.skip('All possible error', () => test('', () => {}));
10+
describe('All possible error when using invalid wasm file', () => {
11+
let config: any;
12+
beforeEach(() =>
13+
(config = {
14+
rules: [
15+
{
16+
test: /\.wasm$/,
17+
type: 'javascript/auto',
18+
use: {
19+
loader: resolve(process.cwd(), 'dist')
20+
}
21+
}
22+
]
23+
}));
24+
25+
describe('Use as a library', () => {
26+
const wasmBuffer = readFileSync(
27+
resolve(__dirname, 'fixtures/invalid/foo.wasm')
28+
);
29+
30+
test.each(exportOptions.all)('export as %s should throw an error', opts =>
31+
expect(() => wasm2js(wasmBuffer, opts)).toThrowError()
32+
);
33+
34+
describe('Use empty callback error', () => {
35+
test.each(['instance', 'module'])(
36+
'export as %s should throw an error when imported',
37+
async opts => {
38+
const code = wasm2js(wasmBuffer, opts, () => ({}));
39+
40+
expect(() => eval(code)).toThrowError();
41+
}
42+
);
43+
44+
test('export as async-instance should throw an error when called', async () => {
45+
const code = wasm2js(wasmBuffer, 'async-instance', () => ({}));
46+
const exportedModule = eval(code);
47+
48+
expect(() => exportedModule()).toThrowError();
49+
});
50+
51+
test('export as buffer should be not a valid WebAssembly', async () => {
52+
const code = wasm2js(wasmBuffer, 'buffer', () => ({}));
53+
const exportedModule = eval(code);
54+
55+
expect(WebAssembly.validate(exportedModule)).toBeFalse();
56+
});
57+
58+
test.each(['async', 'async-module'])(
59+
'export as %s should Promise.reject when called',
60+
async opts => {
61+
const code = wasm2js(wasmBuffer, opts, () => ({}));
62+
const exportedModule = eval(code);
63+
64+
expect(exportedModule()).toReject();
65+
}
66+
);
67+
});
68+
});
69+
70+
describe('Use as a webpack-loader', () => {
71+
test.each(exportOptions.all)(
72+
'export as %s should produce an error',
73+
async opts => {
74+
config.rules[0].use.options = { export: opts };
75+
const stats = await webpack('invalid/fixture.js', config);
76+
77+
on(stats)
78+
.withExtension('.wasm')
79+
.errors.toBeGreaterThan(0);
80+
}
81+
);
82+
83+
test.each(exportOptions.wasm.lessThan_4KB)(
84+
'export as %s should produce an error when wasm code over 4KB',
85+
async opts => {
86+
config.rules[0].use.options = { export: opts };
87+
const stats = await webpack('over4KB.wasm', config);
88+
89+
on(stats)
90+
.withFile('over4KB.wasm')
91+
.errors.toBeGreaterThan(0);
92+
}
93+
);
94+
});
95+
});

test/test-list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ export const exportOptions = {
66
'async',
77
'async-instance',
88
'async-module'
9-
]
9+
],
10+
wasm: {
11+
lessThan_4KB: ['instance', 'module']
12+
// moreThan_4KB: ['async-instance', 'async-module', 'async'],
13+
}
1014
};

0 commit comments

Comments
 (0)