vitest-dev / vitest-dev/vitest
Expect calls custom `inspect()` function unexpectedly
Nobody has claimed this yet.
- 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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