Jest: Cannot assign to read only property '...' of object '...'

Created on 15 Aug 2020  路  9Comments  路  Source: facebook/jest

馃悰 Bug Report

Manually transferring https://github.com/facebook/react/issues/18758 to here. During debugging, it was determined to be a Jest issue.

@gaearon rightly agreed with me that the issue won't get fixed by sitting in the React repo. But they used their facebook admin powers to lock the issue, instead of using GitHub's "Transfer Issue" feature to get it here with all its detail in an instant. Ah, well.

People (including React repo maintainers, apparently) are seeing variations on TypeError: Cannot assign to read only property 'Symbol(...)' of object '[object ...]' when they do expect(...).toBe(...) and expect(...).toEqual(...).

The initial response is that it's not a very helpful error message.

Here's one clue that might explain it:

It happens when calling replaceMatchedToAsymmetricMatcher() inside printDiffOrStringify()

https://github.com/facebook/jest/blob/master/packages/jest-matcher-utils/src/index.ts#L359

@SimenB any ideas?

_Originally posted by @pahan35 in https://github.com/facebook/react/issues/18758#issuecomment-636249433_

And a suggestion about what should be done about it:

We see a current message because replaceMatchedToAsymmetricMatcher fails on preparing the diff.

IMHO it's not about improving the error message, it's about fixing replaceMatchedToAsymmetricMatcher to print the diff for DOM elements without failures.

_Originally posted by @pahan35 in https://github.com/facebook/react/issues/18758#issuecomment-647179531_

Bug Help Wanted

Most helpful comment

Found problem at

https://github.com/facebook/jest/blob/c9c8dba4dd8de34269bdb971173659399bcbfd55/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts#L58-L74

We need to set symbol key property descriptor.

But I got error on typescript that symbol type cannot be object key.
An index signature parameter type must be either 'string' or 'number'.

Will continue work tomorrow.

If anyone interested in, welcom to take it.

https://github.com/microsoft/TypeScript/issues/1863 Probably related to the error

All 9 comments

Hmm I didn鈥檛 even know there鈥檚 a way to transfer issues. 馃榾

Sorry guys.
I'm trying to figure out the problem and solve it.

Found problem at https://github.com/facebook/jest/blob/c9c8dba4dd8de34269bdb971173659399bcbfd55/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts#L58-L74

We need to set symbol key property descriptor.

But I got error on typescript that symbol type cannot be object key.
An index signature parameter type must be either 'string' or 'number'.

Will continue work tomorrow.

If anyone interested in, welcom to take it.

Found problem at

https://github.com/facebook/jest/blob/c9c8dba4dd8de34269bdb971173659399bcbfd55/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts#L58-L74

We need to set symbol key property descriptor.

But I got error on typescript that symbol type cannot be object key.
An index signature parameter type must be either 'string' or 'number'.

Will continue work tomorrow.

If anyone interested in, welcom to take it.

https://github.com/microsoft/TypeScript/issues/1863 Probably related to the error

Here is the minimal to reproduce this error.

test('test', () => {
  const a = {};
  const b = {};
  const symbolKey = Symbol.for('key');
  Object.defineProperty(a, symbolKey, {
    configurable: true,
    enumerable: true,
    value: {
      a: 1,
    },
    writable: false,
  });
  Object.defineProperty(b, symbolKey, {
    configurable: true,
    enumerable: true,
    value: {
      a: 1,
    },
    writable: false,
  });
  expect(a).toBe(b);
});

@WeiAnAn we can do a // @ts-expect-error to suppress it - better than failing at runtime 馃檪

That's awesome, thanks a lot for the fix, @WeiAnAn and @SimenB! 馃槃

Was this page helpful?
0 / 5 - 0 ratings