|
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'; |
2 | 9 |
|
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 | +}); |
0 commit comments