Using expect.any(String) in object comparison in jest
Asked Answered
T

2

7

I have a code like below

    expect(insertedData).toEqual({
                id: expect.any(String),
                clientId: 'client1',
                ...

I expect id should match any string, but what I get as result is

enter image description here

Not sure what I missed.

Also I used like below still same error

expect(insertedData).toEqual(expect.objectContaining({
Tetracaine answered 7/12, 2020 at 0:20 Comment(2)
It is probably because the constructor of the id and lastModifiedDate fields' values are not String. Can you check it?Torn
They're going to be an ObjectId and a Date, no?Kamasutra
N
1

It happens to me. Turns out the problem is not with the fields with expect.any() but the other field. Seems it just bad diff there.

Nupercaine answered 19/2 at 16:4 Comment(0)
G
-1

The problem on your expression relies on the first expectation which is

toEqual()

the expectation should use toMatchObject like:

expect(insertedData).toMatchObject({...
Glasshouse answered 13/5, 2022 at 16:11 Comment(2)
expect(insertedData).objectContaining({... Gives the error Property 'objectContaining' does not exist on type 'JestMatchers<any>'.ts(2339)Persistence
The documentation reads: Matches anything but null or undefined. You can use it inside toEqual or toBeCalledWith instead of a literal value. For example, if you want to check that a mock function is called with a non-null argument:Nightwear

© 2022 - 2024 — McMap. All rights reserved.