Skip to content

Commit

Permalink
feat: eager loading of wasm in jest environment (nodejs#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfedr authored and crysmags committed Feb 27, 2024
1 parent 8742379 commit 370a30c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/client.js
Expand Up @@ -408,6 +408,8 @@ const constants = require('./llhttp/constants')
const EMPTY_BUF = Buffer.alloc(0)

async function lazyllhttp () {
const llhttpWasmData = process.env.JEST_WORKER_ID ? require('./llhttp/llhttp.wasm.js') : undefined

let mod
try {
mod = await WebAssembly.compile(Buffer.from(require('./llhttp/llhttp_simd.wasm.js'), 'base64'))
Expand All @@ -418,7 +420,7 @@ async function lazyllhttp () {
// being enabled, but the occurring of this other error
// * https://github.com/emscripten-core/emscripten/issues/11495
// got me to remove that check to avoid breaking Node 12.
mod = await WebAssembly.compile(Buffer.from(require('./llhttp/llhttp.wasm.js'), 'base64'))
mod = await WebAssembly.compile(Buffer.from(llhttpWasmData || require('./llhttp/llhttp.wasm.js'), 'base64'))
}

return await WebAssembly.instantiate(mod, {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,7 @@
"test": "npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:jest && tsd",
"test:node-fetch": "node scripts/verifyVersion.js 16 || mocha test/node-fetch",
"test:fetch": "node scripts/verifyVersion.js 16 || tap test/fetch/*.js",
"test:jest": "jest test/jest/test",
"test:jest": "jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",
"test:typescript": "tsd",
Expand Down Expand Up @@ -114,5 +114,8 @@
"esnext"
]
}
},
"jest": {
"testMatch": ["<rootDir>/test/jest/**"]
}
}
6 changes: 3 additions & 3 deletions test/jest/test.js
Expand Up @@ -12,7 +12,7 @@ test('should work in jest', async () => {
res.setHeader('Content-Type', 'text/plain')
res.end('hello')
})
await new Promise((resolve, reject) => {
await expect(new Promise((resolve, reject) => {
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
client.request({
Expand All @@ -28,9 +28,9 @@ test('should work in jest', async () => {
if (err) {
reject(err)
} else {
resolve(result)
resolve(result.body.text())
}
})
})
})
})).resolves.toBe('hello')
})

0 comments on commit 370a30c

Please sign in to comment.