相关疑难解决方法(0)

Jest是否支持ES6导入/导出?

如果我使用import/exportES6,那么我的所有测试都会失败并显示错误:

意外的保留字

我将我的测试对象转换为使用旧学校IIFY语法,突然我的测试通过了.或者,采用更简单的测试用例:

   var Validation = require('../src/components/validation/validation');//PASS
   //import * as Validation from '../src/components/validation/validation'//FAIL
Run Code Online (Sandbox Code Playgroud)

同样的错误.显然这里导入/导出存在问题.使用ES5语法重写我的代码只是为了让我的测试框架感到满意是不切实际的.

我有巴贝尔开玩笑.我尝试了github问题的各种建议.不行,到目前为止.

的package.json

 "scripts": {
    "start": "webpack-dev-server",
    "test": "jest"
  },
      "jest": {
        "testPathDirs": [
          "__tests__"
        ],
        "testPathIgnorePatterns": [
          "/node_modules/"
        ],
        "testFileExtensions": ["es6", "js"],
        "moduleFileExtensions": ["js", "json", "es6"]
      },
Run Code Online (Sandbox Code Playgroud)

babelrc

{
  "presets": ["es2015", "react"],
  "plugins": ["transform-decorators-legacy"]
}
Run Code Online (Sandbox Code Playgroud)

有没有解决这个问题?

ecmascript-6 jestjs babel-jest

34
推荐指数
4
解决办法
2万
查看次数

Jest - SyntaxError: 不能在模块外使用 import 语句

我在jest:24.9.0没有任何配置的情况下使用,从 create-react-app 全局安装。在这些文件中,我使用了 es6 模块。使用时没有错误"test": "react-scripts test"

但是,当我开始使用jestwith 时,"test": "jest --config jest.config.js",我看到以下错误。

 FAIL  src/configuration/notifications.test.js
  ? Test suite failed to run

    /var/www/management/node/src/configuration/notifications.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import notifications from './notifications';
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs create-react-app

13
推荐指数
2
解决办法
4432
查看次数

开玩笑:语法错误:无法在 React/Typescript 项目的模块外部使用 import 语句

在使用 Babel、Typescript、React 和 Jest 测试某些组件时,我遇到了 Jest 问题。

在测试 React/Typescript 组件时,我得到了SyntaxError: Cannot use import statement outside a module.

我尝试过的事情

  1. 使用 babel 插件(参见下面的 babel 配置)
  2. 使用ts-jest
  3. 尝试了文档ts-jest中的 ESM 支持
  4. 阅读这篇这篇这篇有类似问题的帖子

这里是jest.config.js

const ignores = ['/node_modules/'];

module.exports = {
    preset: 'ts-jest',
    roots: ['<rootDir>'],
    modulePaths: [
        "<rootDir>/src"
    ],
    moduleDirectories: [
        "node_modules",
    ],
    transformIgnorePatterns: [...ignores],
    transform: {
        '^.+\\.(ts|tsx)?$': 'ts-jest',
        '^.+\\.(gif|svg|ico)$': '<rootDir>/svgTransform.js',
    },
    testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.js?$',
    moduleFileExtensions: ['tsx', 'js', 'ts'],
    moduleNameMapper: { …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js jestjs babeljs ts-jest

5
推荐指数
1
解决办法
1847
查看次数