babel-plugin-rewire与摩卡和反应不起作用

我有一个有趣的情况:

我在摩卡测试中使用babel-plugin-rewire如下:

// given
MySystemUnderTest.__Rewire__("ChildComponent",({ prop }) => {
      return <div>{prop}</div>;
});

const mockProps = {
    prop: "hello,world!"
};

// when
const { getByText } = render(<MySystemUnderTest {...mockProps} />);

// then
await waitForElement(() => getByText("hello,world!"));

MySystemUnderTest.__ResetDependency__("ChildComponent");

我一直在使用WebStorm的mocha测试运行程序以及以下配置: --require @babel/polyfill --require @babel/register --require mocha-setup.js --require ignore-styles --timeout 10000 运行单个测试,它就像一个魅力。

但是,一旦我使用在我package.json中指定的命令使用glob运行整个测试套件:

"mocha": "cross-env NODE_ENV=test mocha --require @babel/polyfill --require @babel/register --require mocha-setup.js --require ignore-styles --timeout 10000 './src/**/*.test.js'",

使用重新接线的测试失败。没有警告,但是看起来子组件的根目录永远不会发生,因此会渲染整个树。

我的babel设置也在package.json中定义:

"babel": {
    "presets": [
      "@babel/preset-env","@babel/preset-react"
    ],"plugins": [
      "@babel/plugin-proposal-class-properties","babel-plugin-dynamic-import-node","@babel/plugin-transform-runtime","react-hot-loader/babel","rewire"
    ]
  }
  • 反应v 16.11.0
  • @ babel / core v 7.7.2
  • babel-plugin-rewire 1.2.0

使用带有react-scripts v 3.2.0的create react应用

jay444679776 回答:babel-plugin-rewire与摩卡和反应不起作用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2978364.html

大家都在问