'Why expect({"[\"": 2}).toHaveProperty("[\"") is not true in jest?

I would like to test that object has a property with the name ["

In node js I can definie object "x" with property "[\""

> x = {"[\"": 2}
{ '["': 2 }
> x["[\""]
2

and of course

> '["' ===  "[\""
true

but when I typed test

expect({"[\"": 2}).toHaveProperty("[\"")

I received error:

Error: expect(received).toHaveProperty(path)

Expected path: "[\""
Received path: []

Received value: {"[\"": 2}

These versions also not works

expect({"[\"": 2}).toHaveProperty(`["`);
const p = `["`;
expect({[p]: 2}).toHaveProperty(p);

Update 1

There was a hypothesis that it is because of libraries

"esbuild-jest": "^0.5.0",
"esbuild-node-tsc": "^1.8.5",
"jest-text-transformer": "^1.0.2",

But I created pure js project with only jest@^27.5.1 installed and reproduced it

enter image description here

Update 2

There was a hypothesis that it depends on the operating system. I reproduced it on Macbook Pro and Arch Linux. For node js versions 12, 14, 16.

Working workaround:

expect({"[\"": 2}[`["`]).toBeDefined();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source