From f1958fb31585ebb5d5ae011c1f0769953246e576 Mon Sep 17 00:00:00 2001 From: Fred Cox Date: Tue, 22 Feb 2022 13:19:17 +0000 Subject: [PATCH] feat: eager loading of wasm in jest environment --- lib/client.js | 4 +++- package.json | 5 ++++- test/jest/test.js | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/client.js b/lib/client.js index d4e026d4a30..8de98497f25 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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')) @@ -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, { diff --git a/package.json b/package.json index 6a6f6453e26..1a9143b8c3d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -114,5 +114,8 @@ "esnext" ] } + }, + "jest": { + "testMatch": ["/test/jest/**"] } } diff --git a/test/jest/test.js b/test/jest/test.js index a0cc57aae76..079a41ff195 100644 --- a/test/jest/test.js +++ b/test/jest/test.js @@ -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({ @@ -28,9 +28,9 @@ test('should work in jest', async () => { if (err) { reject(err) } else { - resolve(result) + resolve(result.body.text()) } }) }) - }) + })).resolves.toBe('hello') })