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

expect(promise).rejects.toThrow() broken by @testing-library/jest-dom/vitest with server.deps.inline: true #5285

Closed
6 tasks done
CornCobs opened this issue Feb 23, 2024 · 8 comments
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@CornCobs
Copy link

Describe the bug

I'm using vitest in global mode. If I use @testing-library/jest-dom by importing it in setupTests.ts like so:

import "@testing-library/jest-dom/vitest";

Most test matchers work as expected, including those added by jest-dom. However, the matcher for rejected promises, expect(promise).rejects.toThrow() throws an unexpected exception:

TypeError: errorLike is not a constructor
 ❯ Object.getConstructorName node_modules/check-error/index.js:110:48

Verified that without importing jest-dom, this issue does not occur.

Not sure if this is an issue from @testing-library/jest-dom, as the matcher that is broken seems unrelated. Furthermore, since @testing-library/jest-dom is relatively popular, I felt it would be useful to raise this issue here.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-6dgu5v?file=src%2F__tests__%2Findex.spec.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-react-swc: ^3.6.0 => 3.6.0 
    @vitest/ui: latest => 1.3.0 
    vite: latest => 5.1.3 
    vitest: latest => 1.3.0

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 24, 2024

It looks like server.deps.inline: true is also a necessary factor to reproduce this. I feel it's a bit dangerous flag since it looks like it's also inlining vitest, @vitest/runner, etc... and actually it doesn't seem to even work when running the most basic template https://stackblitz.com/edit/vitest-dev-vitest-rlq7to?file=vite.config.ts

I think the recommended usage is to only put only packages which are not compatible plain NodeJS ESM and it's mostly used as a workaround https://vitest.dev/config/#server-deps-inline


Related to inline: true

@hi-ogawa hi-ogawa added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Feb 24, 2024
@hi-ogawa hi-ogawa changed the title expect(promise).rejects.toThrow() broken by @testing-library/jest-dom/vitest expect(promise).rejects.toThrow() broken by @testing-library/jest-dom/vitest with server.deps.inline: true Feb 25, 2024
@CornCobs
Copy link
Author

Confirmed that modifying server.deps.inline to an array of package names worked.

@dcramer
Copy link

dcramer commented Apr 7, 2024

chaijs/check-error#51

I wouldnt call this a repro, but I hit this as well: https://github.com/dcramer/peated/pull/153/files

One thing I want to note is I'm not using the referenced library, as my tests are not DOM coupled:

➜  ~/s/peated (ref/collapse-worker) ✗ cat pnpm-lock.yaml| grep jest
  /@jest/[email protected]:
      eslint-plugin-jest: 26.9.0(@typescript-eslint/[email protected])([email protected])([email protected])
      eslint-plugin-jest-dom: 4.0.3([email protected])
      - jest
  /[email protected]([email protected]):
  /[email protected](@typescript-eslint/[email protected])([email protected])([email protected]):
      jest: '*'
      jest:
      '@jest/schemas': 29.6.3

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Apr 7, 2024

@dcramer Do you use server.deps.inline?

@dcramer
Copy link

dcramer commented Apr 8, 2024

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Apr 8, 2024

I thought this is sever.deps.inline: true only issue, but maybe I was wrong. Last time I checked, globals: true seems also relevant, so maybe that one is the factor.

Thanks for reporting. It's a huge repro, so if you can minimize it, then that would be great, but at least we have things to look at.

@clemp6r
Copy link

clemp6r commented May 3, 2024

I had the same error, on my project it was somehow related to vitest-fail-on-console. I fixed the issue by upgrading it from 0.5.1 to 0.6.3.

@sheremet-va
Copy link
Member

Looks like the issue happens when you have different Vitest versions installed: chaijs/check-error#51

@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

5 participants