【问题标题】:Jest configuration笑话配置
【发布时间】:2016-11-29 18:26:53
【问题描述】:

我正在将 Jest 测试框架添加到我的 React Native 项目中。我收到以下错误:

Failed to get mock metadata: /Users/me/Documents/Development/project/node_modules/global/window.js

我的测试文件如下所示:

import 'react-native'
import React from 'react'
import { MyComponent } from '../components/MyComponent'

import renderer from 'react-test-renderer'

it('renders correctly', () => {
  const tree = renderer.create(<MyComponent />).toJSON()
  expect(tree).toMatchSnapshot()
})

还有我的 package.json 中的 Jest 配置:

"jest": {
    "preset": "jest-react-native",
    "testPathIgnorePatterns": ["/node_modules/", "/example/", "/lib/"],
    "testRegex": "(/tests/.*|\\.(test|spec))\\.(js|jsx)$",
    "automock": "true",
    "unmockedModulePathPatterns": [ "lodash" ],
    "transformIgnorePatterns": [
      "node_modules/(?!@exponent/ex-navigation",
      ")"
    ]
  }

按照提示的错误提示,我查看了http://facebook.github.io/jest/docs/manual-mocks.html#content

【问题讨论】:

    标签: javascript react-native jestjs


    【解决方案1】:

    我认为您在 package.json 中的玩笑配置有问题。

    这是一个示例 jest config sn-p:

    "jest": {
        "preset": "react-native",
        "cacheDirectory": "./cache",
        "coveragePathIgnorePatterns": [
          "./app/utils/vendor"
        ],
        "coverageThreshold": {
          "global": {
            "statements": 80
          }
        },
        "transformIgnorePatterns": [
          "/node_modules/(?!react-native|react-clone-referenced-element|react-navigation)"
        ]
      }
    

    预设:预设是模仿 React Native 应用程序环境的节点环境。因为它不加载任何 DOM 或浏览器 API,它极大地改善了 Jest 的启动时间。

    cacheDirectory:帮助您大大提高测试速度。它通过创建已编译模块的缓存来实现这一点,这样下次运行测试时就不必编译 node_modules。

    coveragePathIgnorePatterns:定义覆盖报告要跳过的文件。

    coverageThreshold:定义所有测试通过的阈值限制。如果覆盖率小于定义的限制,则测试将失败。这有助于我们始终保持良好的覆盖率。

    transformIgnorePatterns:我们在这里传递所有需要转译的 NPM 模块。这些模块基本上是 ES6/7 模块。

    PS:我写了一篇关于如何为 react-native 项目设置 jest 的博客。这是网址:http://rahulgaba.com/react-native/2017/05/19/Jest-test-suite-with-superpowers.html

    【讨论】:

      【解决方案2】:

      在你的 package.json 中设置 "automock": "false"(使用 jest-react-native 预设 isn't supported 自动模拟)

      【讨论】:

        猜你喜欢
        • 2019-10-15
        • 2018-09-23
        • 2020-05-14
        • 2019-08-17
        • 2020-01-22
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        相关资源
        最近更新 更多