【问题标题】:Testing Expo Constant with Jest, Jest encountered an unexpected token用 Jest 测试 Expo Constant,Jest 遇到了一个意外的令牌
【发布时间】:2019-10-07 07:35:05
【问题描述】:

我正在将 SDK 从 Expo 托管工作流切换到 Expo 裸工作流,以减少 Expo 中包含的一些不必要的库。我目前正在使用 Expo 中的一些库,例如 Secure-store、Constants 和 Web 浏览器,而不是使用整个 Expo 包。我有一些测试最初是在 jest、jest-expo 和 Expo 中运行的。我从 jest 中删除了 expo,并尝试仅使用上面的三个 expo 库以及 @unimodules 来编译 jest。测试文件具有导入语句,例如:

import * as SecureStore from 'expo-secure-store';
import * as WebBrowser from 'expo-web-browser';
import Constants from 'expo-constants';

但是,在编译 jest 时,它一直在给我

Jest 遇到了意外的令牌 这通常意味着您正在尝试导入 Jest 无法解析的文件,例如它不是纯 JavaScript。 >SyntaxError:意外的令牌导入 从'expo-constants'导入常量; >ScriptTransformer._transformAndBuildScript(node_modules/@jest/transform/build/ScriptTransformer.js:471:17)

似乎我可能已经解决了 expo-secure-store 的意外导入,因为我之前遇到了该错误。我从 babel6 切换到 babel7,添加了一个 babel.config.js,如下所示:

module.exports = {
    presets: [
      '@babel/preset-env'
    ],
    env: {
        test: {
          presets: [['@babel/preset-env']]
        }
      },
    plugins: [
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-transform-modules-commonjs"
    ]
};

我正在从 jest-preset: jest-expo 切换到 jest-preset: react-native。

我的工作流程有问题吗?任何帮助表示赞赏。

【问题讨论】:

    标签: react-native jestjs babeljs expo


    【解决方案1】:

    有点晚了,但我遇到了这个问题,我修复了添加

    "transformIgnorePatterns": [
        "node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|@sentry/.*)"
      ]
    

    遵循https://docs.expo.io/guides/testing-with-jest/的指南

    【讨论】:

    • 修复了这段代码的哪一部分?指南来自?
    • 添加了世博指南的缺失链接。这里也有解释:jestjs.io/docs/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2019-01-09
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多