vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.61k stars 1.13k forks source link

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

Open CornCobs opened 6 months ago

CornCobs commented 6 months ago

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 commented 6 months ago

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

CornCobs commented 6 months ago

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

dcramer commented 5 months ago

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/schemas@29.6.3:
      eslint-plugin-jest: 26.9.0(@typescript-eslint/eslint-plugin@5.62.0)(eslint@8.57.0)(typescript@5.3.3)
      eslint-plugin-jest-dom: 4.0.3(eslint@8.57.0)
      - jest
  /eslint-plugin-jest-dom@4.0.3(eslint@8.57.0):
  /eslint-plugin-jest@26.9.0(@typescript-eslint/eslint-plugin@5.62.0)(eslint@8.57.0)(typescript@5.3.3):
      jest: '*'
      jest:
      '@jest/schemas': 29.6.3
hi-ogawa commented 5 months ago

@dcramer Do you use server.deps.inline?

dcramer commented 5 months ago

@hi-ogawa not afaik https://github.com/dcramer/peated/blob/main/apps/server/vitest.config.ts

hi-ogawa commented 5 months ago

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 commented 4 months ago

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.