简体   繁体   English

"在反应应用程序中使用反应测试渲染器和故事书执行组件测试时出错"

[英]Error to execute component test with react-test-renderer and storybook in a react app

I doing the storybook tutorial<\/a> , but in the part of the tutorial when I created and ran the tests, this throws an exception indicating the following error in the file .storybook\/config.js<\/strong> :我在做故事书教程<\/a>,但是在我创建和运行测试时的教程部分中,这会引发异常,指示文件.storybook\/config.js<\/strong>中出现以下错误:

LOG ERROR<\/strong>日志错误<\/strong>

react-scripts test
FAIL src/storybook.test.js
Test suite failed to run

TypeError: require.context is not a function

  2 | import "../src/index.css";
  3 |
> 4 | const req = require.context("../src", true, /\.stories.js$/);
    |                     ^
  5 |
  6 | function loadStories() {
  7 |   req.keys().forEach(filename => req(filename));

  at Object.context (.storybook/config.js:4:21)
  at configure (node_modules/@storybook/addon-storyshots/dist/frameworks/configure.js:38:11)
  at Object.load (node_modules/@storybook/addon-storyshots/dist/frameworks/react/loader.js:26:26)
  at loadFramework (node_modules/@storybook/addon-storyshots/dist/frameworks/frameworkLoader.js:31:17)
  at testStorySnapshots (node_modules/@storybook/addon-storyshots/dist/api/index.js:53:36)
  at Object.<anonymous> (src/storybook.test.js:2:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        6.812s
Ran all test suites related to changed files.

You should use requireContext instead of require.context :您应该使用requireContext而不是require.context

Add the package to dev dependencies:将包添加到开发依赖项:

yarn add --dev require-context.macro

and Import it in the config and use it, your config should look like gthis:并在配置中导入并使用它,您的配置应该类似于 gthis:

import { configure } from "@storybook/react";
import requireContext from 'require-context.macro';

import "../src/index.css";

const req = requireContext('../src', true, /\.stories\.js$/);

function loadStories() {
  req.keys().forEach(filename => req(filename));
}

configure(loadStories, module);

运行这一行: yarn add --dev require-context.macro

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 React 组件作为常规 JS 传递给 react-test-renderer - How to pass React component as regular JS to react-test-renderer 使用 react-test-renderer 测试异步 componentDidMount() - Testing async componentDidMount() with react-test-renderer 如何在 react-test-renderer 渲染器实例中查找文本元素? - How to find for a text element, in react-test-renderer renderer instance? 在使用 react-test-renderer 的 Jest 快照测试中,Refs 为空 - Refs are null in Jest snapshot tests with react-test-renderer 具有react,react-native,react-dom,react-test-renderer的软件包的版本冲突 - Version conflict of packages with react, react-native, react-dom, react-test-renderer 使用Jest和react-test-renderer使用Material-UI时,为什么我的渲染器失败? - Why is my renderer failing when using Material-UI using Jest and react-test-renderer? 反应测试渲染器测试失败类型错误:无法读取未定义的属性(读取“当前”) - react-test-renderer test failing TypeError: Cannot read properties of undefined (reading 'current') TypeError:无法读取未定义的 React-test-renderer 的属性“__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED” - TypeError: Cannot read property '__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined React-test-renderer 如何使用 react-test-renderer 或其他替代库渲染普通的 html 代码? - How to render plain html code with react-test-renderer or other alternative libs? 与孩子反应测试组件 - React test component with children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM