question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`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" (because TextEncoder 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:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
wsmdcommented, Dec 4, 2019

I’ve done some investigation on this and it seems that the constructor of the Uint8Array returned from TextEncoder.prototype.encode and the global Uint8Array constructor are not the same (causing the assertion to fail via the iterableEquality); note that this only happens in the jest node environment.

// in Jest (using the node environment)
new TextEncoder().encode('').constructor === Uint8Array // false

// outside Jest (regular node runtime)
new TextEncoder().encode('').constructor === Uint8Array // true

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’s global to reference the actual Uint8Array (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.

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >
Bountysource
expect (...).toEqual(...)` fails on strings encoded with `TextEncoder`
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found