This repository was archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwasm-loader-spec.js
81 lines (64 loc) · 2.26 KB
/
wasm-loader-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const loader = require('./../index');
const Module = require('module');
const { readFileSync } = require('fs');
const wasm = readFileSync(__dirname + '/../factorial.wasm');
const __NOTAREALMODULE__ = '__NOTAREALMODULE__';
const FACTORIALS = [1, 1, 2, 6, 24, 120];
/**
* NOTE: Some gnarly node module gymnastics are down here.
* eval() type nonsense wouldn't really work here as we
* are compiling ouput to be a valid node Module :)
*
* All-in-all it's a nice way to test if the loader is working.
*/
describe('wasm-loader', () => {
let loaderContext;
beforeEach(() => {
loaderContext = {
resourceQuery: '',
resourcePath: './counter.wasm',
options: { context: __dirname + '/../' }
};
// Mess with the node module cache to keep tests pure
Module._cache[__NOTAREALMODULE__] = null;
});
it('should create a promise', (done) => {
loaderContext.callback = (unused, output) => {
const module = new Module(__NOTAREALMODULE__, null);
module._compile(output, __NOTAREALMODULE__);
const shouldBePromise = module.exports();
expect(typeof shouldBePromise.then).toBe('function');
done();
}
loader.call(loaderContext, wasm);
});
it('should create a promise', (done) => {
loaderContext.callback = (unused, output) => {
const module = new Module(__NOTAREALMODULE__);
module._compile(output, __NOTAREALMODULE__);
module.exports().then(({ instance: { exports: { _Z4facti: factorial } } }) => {
for(let i = 0; i < FACTORIALS.length; i++) {
expect(factorial(i)).toBe(FACTORIALS[i]);
}
done();
});
}
loader.call(loaderContext, wasm);
});
describe('optimizations', () => {
it('should remove the exports if unused', (done) => {
loaderContext.resourceQuery = "?dce=1&foo&bar";
loaderContext.callback = (unused, output) => {
const module = new Module(__NOTAREALMODULE__, null);
module._compile(output, __NOTAREALMODULE__);
const makeModule = module.exports();
makeModule.then(res => {
const factorial = res.instance.exports._Z4facti;
expect(factorial).not.toBe('function');
done();
});
}
loader.call(loaderContext, wasm);
});
});
});