`expect(promise).rejects.toThrow()` broken by `@testing-library/jest-dom/vitest` with `server.deps.inline: true`
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
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
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
- https://github.com/vitest-dev/vitest/issues/2806
- https://github.com/vitest-dev/vitest/pull/4815
Confirmed that modifying server.deps.inline to an array of package names worked.
https://github.com/chaijs/check-error/issues/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
@dcramer Do you use server.deps.inline?
@hi-ogawa not afaik https://github.com/dcramer/peated/blob/main/apps/server/vitest.config.ts
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.
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.