Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] ReferenceError: TextDecoder is not defined - running tests with React Testing Library. #11662

Closed
tielushko opened this issue Nov 10, 2021 · 8 comments

Comments

@tielushko
Copy link

tielushko commented Nov 10, 2021

Hello!

I am currently trying to use the React Testing library to run my unit/integrated tests, and there's a problem I've been noticing running any component/file that includes apache-arrow imports.

I am currently using the es2015-cjs bundle due to some tree-shaking issues that's been happening with the library, but even when after the solution was introduced and I used es6 import statements, the TextDecoder error was still present.

Here is the output from the terminal:

ReferenceError: TextDecoder is not defined

image

It appears that the issue is coming from here: https://github.com/apache/arrow/blob/master/js/src/util/utf8.ts

I would appreciate any guidance on this issue!

@sisco0
Copy link

sisco0 commented Nov 15, 2021

What is your current NodeJS version? https://nodejs.org/api/util.html#class-utiltextdecoder
Please take a look at the referenced Pull Request to see if it solves your issue. TextDecoder is used differently in old NodeJS versions, as stated in nodejs/node@e5383ad#diff-bd5ca5b3bb337c44d0c11c2e0eaef432332dcdf6eaf65a6b4171a7b3380720a3

@tielushko
Copy link
Author

Hey @sisco0 !

Thanks for reaching out back to me. I was running version 14, but after research I decided to most recent LTS 16.13.0 in hopes it would resolve the issue, however the error was still present. Here's the environment I am running:

System:
OS: Windows 10 10.0.19042
Binaries:
Node: 16.13.0
Yarn: Not Found
npm: 7.13.0
Browsers:
Chrome: 95.0.4638.69
Edge: Spartan (44.19041.906.0), Chromium (95.0.1020.38)

@sisco0
Copy link

sisco0 commented Dec 2, 2021

Hello @tielushko , did you have a chance to test the attached Pull Request solution?

@tielushko
Copy link
Author

I did test it as well @sisco0, but it seemed to still have the issue.

@stopyransky
Copy link

I can only confirm the issue.
For me is present when trying to import Table:
import { Table } from 'apache-arrow';

Shouldn't the utf8.ts use Node's util module?

const util = require('util');
const decoder = new util.TextEncoder();

instead of

const decoder = new TextEncoder();

@tielushko
Copy link
Author

@stopyransky I tried to use the normal package with ES6 imports as well, and it was breaking for me too. I use the es5 packaged one due to the issues the package had in the past with tree-shaking. Either way you import it, the Decoder issue is present.

@alippai
Copy link
Contributor

alippai commented Dec 15, 2021

It can be similar to this issue jsdom/jsdom#2524

@tielushko
Copy link
Author

tielushko commented Feb 23, 2022

The top answer here:

https://stackoverflow.com/questions/57712235/referenceerror-textencoder-is-not-defined-when-running-react-scripts-test/57713960#57713960

seemed to resolve my issues. I also added the TextDecoder from utils and ran the test with the environment specified.

const Environment = require("jest-environment-jsdom");

/**
 * A custom environment to set the TextEncoder and TextDecoder
 */
module.exports = class CustomTestEnvironment extends Environment {
  async setup() {
    await super.setup();
    if (typeof this.global.TextEncoder === "undefined") {
      const { TextEncoder } = require("util");
      this.global.TextEncoder = TextEncoder;
    }

    if (typeof this.global.TextDecoder === "undefined") {
      const { TextDecoder } = require("util");
      this.global.TextDecoder = TextDecoder;
    }
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants