-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jest compatibility API: expect.toHaveProperty does not support array index #2804
Comments
This issue was noticed while writing expect comparison tests in vitest-codemod |
Does it work with index as a string? |
It works with index as a string import { expect, test } from "vitest";
const obj = { fruits: ["apple", "banana", "orange"] };
test("toHaveProperty", () => {
expect(obj).toHaveProperty(["fruits", "0"], "apple");
}) DEV v0.28.4 /Users/trivikram/workspace/test
✓ example.spec.js (1)
Test Files 1 passed (1)
Tests 1 passed (1)
Start at 08:53:40
Duration 197ms (transform 45ms, setup 0ms, collect 5ms, tests 1ms) |
The bug is on line 283 of jest-expect.ts vitest/packages/expect/src/jest-expect.ts Line 283 in aa1aa4d
It assumes all values in the array are strings. |
One fix would be coercing key to String primitive before running replace on it. args[0] = args[0].map(key => String(key).replace(/([.[\]])/g, '\\$1')).join('.') |
Example fix posted at #2808 |
Describe the bug
Jest compatibility API: expect.toHaveProperty does not support array index
Reproduction
Jest
Succeeds in jest@29.4.1
Docs: https://jestjs.io/docs/expect#tohavepropertykeypath-value
Vitest
Fails with vitest@0.28.4
Error thrown:
Docs: https://vitest.dev/api/expect.html#tohaveproperty
System Info
npmPackages: vitest: ^0.28.4 => 0.28.4
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: