在 React Native 中使用 jest 进行故事书快照测试的问题

时间:2021-05-29 03:12:29

标签: react-native jestjs storybook

我有什么

我正在尝试在我的 react-native/typescript 应用程序中使用带有 Storyshots 插件的 jest 和 Storybook 进行快照测试,但是当我尝试运行简单测试时遇到了一些问题。

根据 storybook documentation 中的快照测试部分,您唯一需要做的就是创建一个 storybook.test.js 文件,其中包含以下内容:

storybook.test.js

import initStoryshots from '@storybook/addon-storyshots';
initStoryshots();

此后,一切都应该按预期工作,但控制台抛出以下错误:

● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to
 import a file which Jest cannot parse, e.g.
it's not plain JavaScript.

    By default, if Jest sees a Babel config,
it will use that to transform your files, ign
oring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Mo
dules, see https://jestjs.io/docs/en/ecmascri
pt-modules for how to enable it.
     • To have some of your "node_modules" fi
les transformed, you can specify a custom "tr
ansformIgnorePatterns" in your config.
     • If you need a custom transformation sp
ecify a "transform" option in your config.
     • If you simply want to mock your non-JS
 modules (e.g. binary assets) you can stub th
em out with the "moduleNameMapper" config opt
ion.

    You'll find more details and examples of
these config options in the docs:
    https://jestjs.io/docs/en/configuration.h
tml

    Details:

    C:\Users\myuser\Desktop\myapp\node_m
odules\react-native-swipe-gestures\index.js:3
    import React, { Component } from "react";
    ^^^^^^

    SyntaxError: Cannot use import statement
outside a module

      at Runtime.createScriptFromCode (node_m
odules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/@st
orybook/react-native/dist/preview/components/
OnDeviceUI/navigation/index.js:29:53)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        11.468 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

jest.config.js

module.exports = {
  preset: 'react-native',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset']
};

如果我从我的测试中删除 @storybook/addon-storyshots 相关的东西,一切正常,没有任何问题。所以我不知道我是否需要一些额外的配置来使用 React Native 进行工作故事书/玩笑快照测试。

0 个答案:

没有答案