【问题标题】:Why is jest throwing `Error: "toString()" failed`为什么开玩笑抛出`错误:“toString()”失败`
【发布时间】:2018-03-19 10:30:05
【问题描述】:

当我尝试在我的 react 应用程序中运行我的 jest 测试套件时,它会吐出这个错误:

Error: "toString()" failed
    at stringSlice (buffer.js:558:43)
    at Buffer.toString (buffer.js:631:10)
    at Object.fs.readFileSync (fs.js:601:41)
    at Object.<anonymous> (/home/andrew/Documents/wise-fox/The-App/node_modules/jest-haste-map/build/worker.js:38:65)
    at Generator.next (<anonymous>)
    at step (/home/andrew/Documents/wise-fox/The-App/node_modules/jest-haste-map/build/worker.js:64:1189)
    at /home/andrew/Documents/wise-fox/The-App/node_modules/jest-haste-map/build/worker.js:64:1419
    at new Promise (<anonymous>)
    at Object.<anonymous> (/home/andrew/Documents/wise-fox/The-App/node_modules/jest-haste-map/build/worker.js:64:1100)
    at Object.worker (/home/andrew/Documents/wise-fox/The-App/node_modules/jest-haste-map/build/worker.js:64:46)

我什至检查了一些以前的 git 快照,即使它之前没有吐出这个错误,仍然会出现同样的错误。

这个错误不是很有帮助,我不确定问题出在哪里。只要知道下一步要尝试什么,任何帮助将不胜感激。

反应 16 Linux 基本操作系统

编辑:

其中一个失败的测试。

import React from 'react';
import { shallow } from 'enzyme';
import Header from '../../components/Header/Header';

let wrapper;

beforeEach(() => {
  wrapper = shallow(<Header />);
});

test('should render Header correctly', () => {
  expect(wrapper).toMatchSnapshot();
});

【问题讨论】:

  • 你能至少展示你失败的测试吗?否则几乎不可能回答
  • 当然可以。等一下……
  • 都是用 .toMatchSnapshot() 进行的浅渲染

标签: javascript reactjs testing jestjs


【解决方案1】:

显然问题是我需要在 jest 正常工作之前运行 webpack。不知道为什么,但这确实奏效了。

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 2019-07-01
    • 1970-01-01
    • 2021-07-13
    • 2020-03-11
    • 2018-07-20
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    相关资源
    最近更新 更多