Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't run unit tests with jest when using esm modules, typescript and undici v5.16.0 #1878

Closed
marinrusu1997 opened this issue Jan 23, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@marinrusu1997
Copy link

Bug Description

When trying to run unit tests with jest for files which use undici an error is thrown.

Reproducible By

You can reproduce this error by cloning this repo. README contains steps to reproduce the bug.

Expected Behavior

Unit tests should complete successfully.

Logs & Screenshots

This is the error shown by jest:

 Must use import to load ES Module: /Users/marin/Development/jest-undici-esm-bug/node_modules/undici/lib/llhttp/llhttp.wasm

      at Runtime.requireModule (node_modules/jest-runtime/build/index.js:817:21)
          at async Promise.all (index 0)
          at async Promise.all (index 0)

Also notice that if you lower the undici version, let's say to v5.15.0, a different issue will be thrown

/Users/marin/Development/jest-undici-esm-bug/node_modules/undici/lib/client.js:1184
        if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
                            ^

TypeError: Cannot read properties of undefined (reading 'timeoutType')
    at _resume (/Users/marin/Development/jest-undici-esm-bug/node_modules/undici/lib/client.js:1184:29)
    at resume (/Users/marin/Development/jest-undici-esm-bug/node_modules/undici/lib/client.js:1148:3)
    at connect (/Users/marin/Development/jest-undici-esm-bug/node_modules/undici/lib/client.js:1133:3)

Environment

MacOS Monterey
Node v16.14.2
Undici v5.16.0

@marinrusu1997 marinrusu1997 added the bug Something isn't working label Jan 23, 2023
@KhafraDev
Copy link
Member

KhafraDev commented Jan 23, 2023

edit: see #1878 (comment) for a better solution!

This is an issue with jest.config.cjs:

diff --git a/jest.config.cjs b/jest.config.cjs
index 3841fe0..cb6e7d5 100644
--- a/jest.config.cjs
+++ b/jest.config.cjs
@@ -8,7 +8,7 @@ module.exports = {
                },
        },
        moduleNameMapper: {
-               '^(\\.{1,2}/.*)\\.js$': '$1',
+               '^(\\.{1,2}/.*)\\.js$': '$1.js',
        },
        testMatch: ["**/test/**/*.spec.ts"]
-};
\ No newline at end of file
+};
diff --git a/test/index.spec.ts b/test/index.spec.ts
index 87d3dbb..9068d1f 100644
--- a/test/index.spec.ts
+++ b/test/index.spec.ts
@@ -7,7 +7,7 @@ describe('get request', function () {
     afterAll(stopServer);

     it('makes get request', async () => {
-        const result = await get(BASE_URL);
+        const result = await (await get(BASE_URL)).body.text();
         expect(result).toBe('Hello World');
     });
 })

I would experiment with moduleNameMapper more as it might conflict with other requires/imports/whatever it's used for. The issue is that it would change './llhttp/llhttp.wasm.js' to './llhttp/llhttp.wasm', which caused the require to fail, for whatever reason. Who knows with jest?

@KhafraDev
Copy link
Member

Okay, it seems like ts-jest is at fault for suggesting a bad default for moduleNameWrapper.

@cwilso03
Copy link

cwilso03 commented Feb 7, 2023

Yeah, ts-jest's suggestion is to just drop the .js suffix on imports so Jest doesn't complain about the ESM import syntax.

To fix this in my project, I changed my jest.config.js file to specifically exclude llhttp.wasm.js from that modification, like so:

  moduleNameMapper: {
    '^(\\.{1,2}/.*/llhttp\\.wasm\\.js)$': '$1',
    '^(\\.{1,2}/.*)\\.js$': '$1'
  },

It's a bit of a hack, but until Jest has full ESM support, it gets the job done.

To help future searchers, I should note, I was led to this issue while trying to debug this problem when importing @elastic/elasticsearch. They're apparently using Undici somewhere in their dependency tree. As with the original poster, the problem only appeared with my Jest ts-jest ESM tests, and not with my actual codebase (which is ESM/TypeScript).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants