Jest: 快照测试导入不适用于 babel-preset

创建于 2016-08-15  ·  3评论  ·  资料来源: facebook/jest

我正在尝试一个简单的快照测试。 我正在测试的组件使用模块中的另一个组件。 尝试使用以下命令导入此未模拟的模块时,测试失败:

失败

Using Jest CLI v14.1.0, jasmine2, babel-jest, jest-react-native preset
 FAIL  js/app/SetupView/__tests__/SetupView-test.js (0s)
 Runtime Error
  - SyntaxError: Unexpected reserved word
        at transformAndBuildScript (../../../../../usr/local/lib/node_modules/jest-cli/node_modules/jest-runtime/build/transform.js:306:10)
        at Object.<anonymous> (js/app/SetupView/index.js:12:17)
        at Object.<anonymous> (js/app/SetupView/__tests__/SetupView-test.js:11:12)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 2.419s)

测试

'use strict';
import 'react-native';
import React from 'react';

import SetupView from '../index';

import renderer from 'react-test-renderer';

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

设置视图组件

'use strict';

import React from 'react';
import {
  View,
  Text,
  TouchableHighlight,
  Image,
  Animated,
} from 'react-native';

import DataContainer from 'react.datacontainer'; //<---fails here
import DashboardSettings from './DashboardSettings';
.
.
.

包.json

"jest": {
    "globals": {
      "__DEV__": true
    },
    "collectCoverage": false,
    "verbose": true,
    "preset": "jest-react-native",
    "modulePathIgnorePatterns": [
      "node_modules/react-native/node_modules/yeoman-generator",
      "node_modules/react-native/node_modules/fbjs"
    ],
    "unmockedModulePathPatterns": [
      "react.datacontainer",
      "react.data",
      "react.base.theme",
      "react.layout"
    ]
  }

仅供参考,我的根目录中有一个 .babelrc 文件指向 react-native 预设并且测试运行但如果我将第 12 行注释掉会失败,因为DataContainer未定义。

最有用的评论

你能试试jest --no-cache吗? 如果您在上次调用之前更改了 babelrc,我们可能无法正确更新缓存。

如果这不起作用,请在 github 上创建一个 repo,我可以通过 npm install 和 npm test 进行故障排除。

提示:使用 react-native-preset 时不需要 unmockedModulePathPatterns,因为无论如何都会禁用自动模拟。

所有3条评论

你能试试jest --no-cache吗? 如果您在上次调用之前更改了 babelrc,我们可能无法正确更新缓存。

如果这不起作用,请在 github 上创建一个 repo,我可以通过 npm install 和 npm test 进行故障排除。

提示:使用 react-native-preset 时不需要 unmockedModulePathPatterns,因为无论如何都会禁用自动模拟。

那对我不起作用。 似乎问题在于导入 node_module react.datacontainer 。 在进一步挖掘之后,我相信 jest 预设不是编译用 es6 编写的 node_modules。

不幸的是,我目前无法创建存储库。 但如果它有帮助的话,这里是“react.datacontainer”模块的样子。 https://github.com/ForceDotComLabs/react.force.datacontainer

您可以通过此配置选项为此模块启用预处理: http ://facebook.github.io/jest/docs/tutorial-react-native.html#preprocessorignorepatterns -customization

如果你愿意,你也可以在这个项目的 github 页面上创建一个问题,要求他们预编译他们的依赖项并在 npm 上发布构建文件。

此页面是否有帮助?
0 / 5 - 0 等级