运行 `react-scripts test --env=jsdom` 时,`ReferenceError: TextEncoder is not defined`

IT技术 reactjs tensorflow.js
2021-05-06 04:25:00

我在我的应用程序中使用了 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

3个回答

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

测试/自定义测试-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

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

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

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

    module.出口 = {
        测试环境:“节点”
    };

我在使用 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 配置中添加了环境,正如Cava在评论中所说:

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