vitest icon indicating copy to clipboard operation
vitest copied to clipboard

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

Open CornCobs opened this issue 2 years ago • 7 comments

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

CornCobs avatar Feb 23 '24 16:02 CornCobs

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

hi-ogawa avatar Feb 24 '24 04:02 hi-ogawa

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

CornCobs avatar Feb 26 '24 03:02 CornCobs

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 avatar Apr 07 '24 17:04 dcramer

@dcramer Do you use server.deps.inline?

hi-ogawa avatar Apr 07 '24 23:04 hi-ogawa

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

dcramer avatar Apr 08 '24 04:04 dcramer

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.

hi-ogawa avatar Apr 08 '24 04:04 hi-ogawa

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.

clemp6r avatar May 03 '24 11:05 clemp6r