|
| 1 | +// @flow |
| 2 | +import { rollup } from 'rollup'; |
| 3 | +import { require } from './util'; |
| 4 | +import rust from '../src/main.js'; |
| 5 | + |
| 6 | +describe('test export option for importjs', () => { |
| 7 | + const input = 'test/fixtures/hook_function/index.js'; |
| 8 | + |
| 9 | + test('export of WebAssembly.compile', async () => { |
| 10 | + const bundle = await rollup({ |
| 11 | + input, |
| 12 | + plugins: [rust({ export: 'async-module' })] |
| 13 | + }); |
| 14 | + |
| 15 | + const compileWasm = await require(bundle); |
| 16 | + |
| 17 | + // Usage |
| 18 | + const importObj = { |
| 19 | + hook: { |
| 20 | + before: jest.fn(), |
| 21 | + after: jest.fn() |
| 22 | + } |
| 23 | + }; |
| 24 | + const module = await compileWasm(); |
| 25 | + const instance = new WebAssembly.Instance(module, importObj); |
| 26 | + const { add } = instance.exports; |
| 27 | + |
| 28 | + expect(module).toBeInstanceOf(WebAssembly.Module); |
| 29 | + expect(instance).toBeInstanceOf(WebAssembly.Instance); |
| 30 | + // $FlowFixMe: false alarm |
| 31 | + expect(add(1, 2)).toBe(3); |
| 32 | + expect(importObj.hook.before).toHaveBeenCalledWith(1, 2); |
| 33 | + expect(importObj.hook.after).toHaveBeenCalledWith(3); |
| 34 | + }); |
| 35 | + |
| 36 | + test('export of WebAssembly.instantiate(WebAssembly.Module)', async () => { |
| 37 | + const bundle = await rollup({ |
| 38 | + input, |
| 39 | + plugins: [rust({ export: 'async-instance' })] |
| 40 | + }); |
| 41 | + |
| 42 | + const compileWasm = await require(bundle); |
| 43 | + |
| 44 | + // Usage |
| 45 | + const importObj = { |
| 46 | + hook: { |
| 47 | + before: jest.fn(), |
| 48 | + after: jest.fn() |
| 49 | + } |
| 50 | + }; |
| 51 | + const instance = await compileWasm(importObj); |
| 52 | + const { add } = instance.exports; |
| 53 | + |
| 54 | + expect(instance).toBeInstanceOf(WebAssembly.Instance); |
| 55 | + expect(add(1, 2)).toBe(3); |
| 56 | + expect(importObj.hook.before).toHaveBeenCalledWith(1, 2); |
| 57 | + expect(importObj.hook.after).toHaveBeenCalledWith(3); |
| 58 | + }); |
| 59 | + |
| 60 | + test('export of WebAssembly.instantiate(Buffer)', async () => { |
| 61 | + const bundle = await rollup({ |
| 62 | + input, |
| 63 | + plugins: [rust({ export: 'async' })] |
| 64 | + }); |
| 65 | + |
| 66 | + const compileWasm = await require(bundle); |
| 67 | + |
| 68 | + // Usage |
| 69 | + const importObj = { |
| 70 | + hook: { |
| 71 | + before: jest.fn(), |
| 72 | + after: jest.fn() |
| 73 | + } |
| 74 | + }; |
| 75 | + const { module, instance } = await compileWasm(importObj); |
| 76 | + const { add } = instance.exports; |
| 77 | + |
| 78 | + expect(module).toBeInstanceOf(WebAssembly.Module); |
| 79 | + expect(instance).toBeInstanceOf(WebAssembly.Instance); |
| 80 | + expect(add(1, 2)).toBe(3); |
| 81 | + expect(importObj.hook.before).toHaveBeenCalledWith(1, 2); |
| 82 | + expect(importObj.hook.after).toHaveBeenCalledWith(3); |
| 83 | + }); |
| 84 | +}); |
0 commit comments