vitest-dev / vitest-dev/vitest

Expect calls custom `inspect()` function unexpectedly

Open
#4,478 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p2-edge-case upstream
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

Description

Describe the bug

Any object that has an inspect() function will be called when passed to expect(). If this function happens to expect an argument, for example inspect(fn: () => void) then the test will fail for the wrong reason. This doesn't seem to mentioned in the api and means you need to guard around this currently by doing more targeted tests.

Reproduction

Using vitest@^0.34.0 (including beta 1.0.0 releases) the following test:

class Example {
  public inspect(fn: () => void) {
    fn();
  }
}

it('custom inspect function', (): void => {
  expect(
    new Example(),
  ).toStrictEqual(undefined);
});

Will output as expected:

 FAIL  src/example.test.ts > custom inspect function
TypeError: fn is not a function
 ❯ Example.inspect src/event/service/foo.test.ts:3:5
      1| class Example {
      2|   public inspect(fn: () => void) {
      3|     fn();
       |     ^
      4|   }
      5| }
 ❯ inspectCustom node_modules/loupe/loupe.js:793:20
 ❯ Object.inspect node_modules/loupe/loupe.js:827:20

This appears to be down to loupe but again, this is unexpected.

System Info
System:
    OS: macOS 14.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 2.27 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.1 - ~/.nvm/versions/node/v18.18.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.1/bin/yarn
    npm: 10.2.0 - ~/.nvm/versions/node/v18.18.1/bin/npm
    pnpm: 8.9.0 - ~/.nvm/versions/node/v18.18.1/bin/pnpm
  Browsers:
    Chrome: 119.0.6045.123
    Safari: 17.0
  npmPackages:
    @vitest/coverage-v8: ^0.34.5 => 0.34.6 
    vitest: ^0.34.5 => 0.34.6
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the expect formatting path and the loupe/loupe.js stack entries shown in the reproduction. Add a regression test for an object whose inspect function requires an argument, then run the relevant expect tests and confirm that passing the object does not invoke that function or produce the unrelated TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.