`expect(...).toEqual(...)` fails on strings encoded with `TextEncoder`
See original GitHub issue🐛 Bug Report
To Reproduce
Steps to reproduce the behavior:
- Create a project:
npx create-react-app jest-test1 --typescript
- Modify
package.json
:"test": "react-scripts test --env=node"
(becauseTextEncoder
is not available at all in jsdom) - Create a test like the one below
Expected behavior
I expect this test…
function encodeFoo() {
return new TextEncoder().encode("foo");
}
it('string encoded with TextEncoder equals to the expected byte array', () => {
expect(encodeFoo()).toEqual(new Uint8Array([102, 111, 111])); // fails
});
…to succeed.
Instead it fails with:
✕ string encoded with TextEncoder equals to the expected byte array (3ms)
● string encoded with TextEncoder equals to the expected byte array
expect(received).toEqual(expected) // deep equality
Expected: [102, 111, 111]
Received: serializes to the same string
14 |
15 | it('string encoded with TextEncoder equals to the expected byte array', () => {
> 16 | expect(encodeFoo()).toEqual(new Uint8Array([102, 111, 111])); // fails
| ^
17 | });
18 |
at Object.<anonymous> (src/App.test.tsx:16:23)
Link to repl or repo (highly encouraged)
https://github.com/cubuspl42/jest-test1
envinfo
System:
OS: macOS Mojave 10.14.5
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Binaries:
Node: 12.12.0 - /usr/local/bin/node
npm: 6.11.3 - /usr/local/bin/npm
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Jest toEqual is failing and I don't know why - Stack Overflow
Ok. As it seems, it's a Windows/ Git issue. This is the text with JSON.stringify console.log "type Query {\n updateTest(input: UpdateOne!):
Read more >util TextEncoder TypeScript Examples - ProgramCreek.com
Synchronously reads string from file * * @param path * @param encoding Default 'utf-8' * * @returns returns undefined on failure * *...
Read more >exceljs - npm
#971 and ERROR in node_modules/exceljs/index.d.ts(1661,34): error TS2503: ... getColumn(4).collapsed).to.equal(false); expect(worksheet.
Read more >@jest/expect | Yarn - Package Manager
This package extends expect library with jest-snapshot matchers. It exports jestExpect object, which can be used as standalone replacement of expect . The ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve done some investigation on this and it seems that the constructor of the
Uint8Array
returned fromTextEncoder.prototype.encode
and the globalUint8Array
constructor are not the same (causing the assertion to fail via theiterableEquality
); note that this only happens in the jest node environment.I’m not exactly sure why these two are referencing different constructors or where either one of the constructors is referencing a different one, so I’m wondering if anyone has any pointers on this (I’m really curious!).
That said, updating the
jest-environment-node
’sglobal
to reference the actualUint8Array
(inside of the following block) seems to resolve this issue.https://github.com/facebook/jest/blob/9ac2dcd55c0204960285498c590c1aa7860e6aa8/packages/jest-environment-node/src/index.ts#L54-L61
~I’m happy to open a PR.~ Update: I opened #9261 so we can continue the conversation there.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.