Skip to content
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

Closed
6 tasks done
trivikr opened this issue Feb 4, 2023 · 6 comments · Fixed by #2808
Closed
6 tasks done

Jest compatibility API: expect.toHaveProperty does not support array index #2804

trivikr opened this issue Feb 4, 2023 · 6 comments · Fixed by #2808

Comments

@trivikr
Copy link
Contributor

trivikr commented Feb 4, 2023

Describe the bug

Jest compatibility API: expect.toHaveProperty does not support array index

Reproduction

Jest

Succeeds in jest@29.4.1

const obj = { fruits: ["apple", "banana", "orange"] };

test("toHaveProperty", () => {
  expect(obj).toHaveProperty(["fruits", 0], "apple");
})

Docs: https://jestjs.io/docs/expect#tohavepropertykeypath-value

Vitest

Fails with vitest@0.28.4

import { expect, test } from "vitest";

const obj = { fruits: ["apple", "banana", "orange"] };

test("toHaveProperty", () => {
  expect(obj).toHaveProperty(["fruits", 0], "apple");
})

Error thrown:

$  FAIL  example.spec.js > toHaveProperty
TypeError: key.replace is not a function
 ❯ example.spec.js:6:15
      4| 
      5| test("toHaveProperty", () => {
      6|   expect(obj).toHaveProperty(["fruits", 0], "apple");
       |               ^
      7| })

Docs: https://vitest.dev/api/expect.html#tohaveproperty

System Info

npmPackages:
    vitest: ^0.28.4 => 0.28.4

Used Package Manager

npm

Validations

@trivikr
Copy link
Contributor Author

trivikr commented Feb 4, 2023

This issue was noticed while writing expect comparison tests in vitest-codemod
trivikr/vitest-codemod@8f89939 (#76)

@sheremet-va
Copy link
Member

Does it work with index as a string?

@trivikr
Copy link
Contributor Author

trivikr commented Feb 4, 2023

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)

@trivikr
Copy link
Contributor Author

trivikr commented Feb 4, 2023

The bug is on line 283 of jest-expect.ts

args[0] = args[0].map(key => key.replace(/([.[\]])/g, '\\$1')).join('.')

It assumes all values in the array are strings.

@trivikr
Copy link
Contributor Author

trivikr commented Feb 4, 2023

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('.') 

@trivikr
Copy link
Contributor Author

trivikr commented Feb 4, 2023

One fix would be coercing key to String primitive before running replace on it.

Example fix posted at #2808

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants