【问题标题】:create-react-app can't find react in tests when aliased with preact与 preact 别名时,create-react-app 在测试中找不到反应
【发布时间】:2018-01-22 13:11:21
【问题描述】:

我用“create-react-app”创建了一个项目。我已经通过弹出它并在 webpack 配置中使用别名将 preact 添加到项目中。

alias: {
      'react': 'preact-compat-enzyme',
      'react-dom': 'preact-compat-enzyme',
      'react-dom/server': 'preact-render-to-string',
      'react-addons-test-utils': 'preact-test-utils',
      'react-addons-transition-group': 'preact-transition-group'
    }

这在代码中有效,我能够呈现应用程序。但是,在测试中我收到以下错误

Cannot find module 'react' from 'App.test.js'

为什么找不到模块?除了别名之外,我们还有什么需要添加到配置中的吗?

【问题讨论】:

    标签: reactjs unit-testing jestjs create-react-app preact


    【解决方案1】:

    这需要一些挖掘,但我通过在 moduleNameMapper package.json 中添加以下内容使其工作。

    "moduleNameMapper": {
          "^react$": "preact-compat-enzyme",
          "^react-dom$": "preact-compat-enzyme",
          "^react-dom/server$": "preact-render-to-string",
          "^react-addons-test-utils$": "preact-test-utils",
          "^react-addons-transition-group$": "preact-transition-group",
    },
    

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 1970-01-01
      • 2020-01-25
      • 2018-05-21
      • 2018-01-09
      • 2020-04-02
      • 2019-08-23
      • 1970-01-01
      • 2019-04-26
      相关资源
      最近更新 更多