【问题标题】:`ReferenceError: TextEncoder is not defined` when running `react-scripts test --env=jsdom`运行`react-scripts test --env = jsdom`时`ReferenceError:未定义TextEncoder`
【发布时间】:2019-08-29 14:20:48
【问题描述】:

我在我的应用程序中使用了 TensorFlow 编码器。当应用程序运行时,它在我的浏览器中运行良好,但在测试它的构建时出现问题:

$ npx react-scripts test --env=jsdom
FAIL  src/App.test.js
  ● Test suite failed to run

    ReferenceError: TextEncoder is not defined

      16 | import TextField from '@material-ui/core/TextField';
      17 | import Typography from '@material-ui/core/Typography';
    > 18 | import * as mobilenet from '@tensorflow-models/mobilenet';
         | ^
      19 | import * as UniversalSentenceEncoder from '@tensorflow-models/universal-sentence-encoder';
      20 | import * as tf from '@tensorflow/tfjs';
      21 | import axios from 'axios';

      at new PlatformBrowser (node_modules/@tensorflow/tfjs-core/src/platforms/platform_browser.ts:26:28)
      at Object.<anonymous> (node_modules/@tensorflow/tfjs-core/src/platforms/platform_browser.ts:50:30)
      at Object.<anonymous> (node_modules/@tensorflow/tfjs-core/src/index.ts:29:1)
      at Object.<anonymous> (node_modules/@tensorflow/tfjs-converter/src/executor/graph_model.ts:18:1)
      at Object.<anonymous> (node_modules/@tensorflow/tfjs-converter/src/index.ts:17:1)
      at Object.<anonymous> (node_modules/@tensorflow-models/mobilenet/dist/index.js:38:14)
      at Object.<anonymous> (src/components/model.js:18:1)
      at Object.<anonymous> (src/App.js:8:1)
      at Object.<anonymous> (src/App.test.js:3:1)

我想摆脱那个错误。我试过使用“文本编码”包,但我不确定如何在导入发生之前正确定义 TextEncoder。

也许我可以为--env 设置不同的选项?

如果没有--env=jsdom,我也会遇到同样的错误。我相信我是在遇到类似类型的未定义错误后添加的,它纠正了一个问题。

这是我的测试:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

it('renders without crashing', () => {
  const div = document.createElement('div');
  ReactDOM.render(<App />, div);
  ReactDOM.unmountComponentAtNode(div);
});

所以设置--env=node 也不起作用,因为:ReferenceError: document is not defined

【问题讨论】:

标签: reactjs tensorflow.js


【解决方案1】:

jsdom 似乎没有在 DOM 的全局中定义 TextEncoder。所以你可以用 node.js 填写它。

test/custom-test-env.js:

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

/**
 * A custom environment to set the TextEncoder that is required by TensorFlow.js.
 */
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;
        }
    }
}

npx react-scripts test --env=./test/custom-test-env.js

【讨论】:

  • 谢谢!我通过删除顶部的require('util') 并在setup() 中使用:if (typeof TextEncoder === 'undefined') { const { TextEncoder } = require('util'); this.global.TextEncoder = TextEncoder; } 做了一点未来证明。
  • 因为typeof TextEncoder === 'undefined' 将始终返回false。需要改成:typeof this.global.TextEncoder === 'undefined'
  • 我这样做了,并在jest.config.js 上添加'testEnvironment': '&lt;rootDir&gt;/test/custom-test-env.js'
  • 很好的解决方案,谢谢。如果您只想将此环境用于 1 个文件或测试,您可以将这些行添加到单元测试文件的顶部: /** * @jest-environment ./tests/environments/custom-js-dom-env.ts * /
  • @SerdarD。 'custom-js-dom-env.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module. ts(1208)。如果我在测试运行时添加一个空的export {},我会得到:Unexpected token 'export'
【解决方案2】:

我的 Node.Js 项目遇到了同样的错误。出于测试目的,我在那里开玩笑。所以以下步骤解决了我的问题

step-1: 在项目的根文件夹中添加一个名为 jest.config.js 的文件

step-2: 在 jest.config.file 中添加以下行:

模块.exports = { 测试环境:“节点” };

【讨论】:

  • 谢谢。这对我有用,是更清洁、更短的解决方案。
【解决方案3】:

我在使用 mongodb 时遇到了这个问题。我使用了@Phoenix 解决方案,稍作改动。

首先我用jest-environment-node代替jest-environment-jsdom

const NodeEnvironment = require('jest-environment-node');

// A custom environment to set the TextEncoder that is required by mongodb.
module.exports = class CustomTestEnvironment extends NodeEnvironment {
  async setup() {
    await super.setup();
    if (typeof this.global.TextEncoder === 'undefined') {
      const { TextEncoder } = require('util');
      this.global.TextEncoder = TextEncoder;
    }
  }
}

然后我在 jest configs 中为所有测试添加了环境,正如 Cava 在 cmets 中所说:

// package.json
{
  ...
  "jest": {
    ...
    "testEnvironment": "<rootDir>/tests/custom-test-env.js"
  }
  ...
}

【讨论】:

  • 我还必须添加一个解码器,但这对我来说完全有用!谢谢!
  • Here is an example 的自定义环境配置文件,其中包含更多的分配,并且对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-04
相关资源
最近更新 更多