简体   繁体   English

如何在玩笑中解决“SyntaxError: Cannot use import statement outside a module”

[英]How to resolve "SyntaxError: Cannot use import statement outside a module" in jest

I have a react-native application When trying to run yarn jest, I have this error:我有一个 react-native 应用程序在尝试运行 yarn jest 时,出现以下错误:

在此处输入图像描述

react native version: "0.64.4"反应本机版本:“0.64.4”

Dependencies:依赖项:

"react-native": "0.64.4", "jest": "29.2.2" “本机反应”:“0.64.4”,“笑话”:“29.2.2”

The test file looks like this:测试文件如下所示:

import 'react-native';
import React from 'react';
import App from '../src/App';

import renderer from 'react-test-renderer';
it('renders correctly', () => {
  renderer.create(<App />);
});`

I tried many solutions but none of them worked for me.我尝试了很多解决方案,但没有一个对我有用。

Such kind of an error always occurs when using ES6 without babel being configured,在没有配置 babel 的情况下使用 ES6 时,总是会出现这种错误,

Can you try to run the tests with babel?你能尝试用 babel 运行测试吗?

Also, in your jest configuration, do your have testEnvironment set to "node", If yes, remove it.另外,在你的玩笑配置中,是否将 testEnvironment 设置为“节点”,如果是,请将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“未捕获的语法错误:无法在模块外使用导入语句”? - How to resolve “Uncaught SyntaxError: Cannot use import statement outside a module”? Jest 或 Mocha 与 Vue:SyntaxError:无法在模块外使用 import 语句 - Jest or Mocha with Vue: SyntaxError: Cannot use import statement outside a module 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module Jest - SyntaxError: 无法在模块外使用 import 语句 - Jest - SyntaxError: Cannot use import statement outside a module Jest-Native“SyntaxError:无法在模块外使用导入语句” - Jest-Native "SyntaxError: Cannot use import statement outside a module" 如何修复 SyntaxError:无法在使用 Jest 的模块外部使用 import 语句? - How do I fix SyntaxError: Cannot use import statement outside a module using Jest? “SyntaxError: Cannot use import statement outside a module” 导入模块时导入 Jest 中的副作用模块 - "SyntaxError: Cannot use import statement outside a module" when importing a module which imports a module for side effects in Jest SyntaxError: 无法在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside of module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module Jest 无法使用 Typescript 导入(语法错误:无法在模块外使用导入语句) - Jest fails to import with Typescript (SyntaxError: Cannot use import statement outside a module)
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM