SyntaxError: неожиданный токен {- во время тестирования Jest - PullRequest
0 голосов
/ 27 июня 2019

У меня есть файл шутливого теста hello.test.js .Это импортирует класс Robot из другого файла, robot.js .

hello.test.js

import { Robot } from './robot'
 .
 .

robot.js импортирует другой класс x из другого файла.

robot.js

import { x } from 'file'
 .
 .

Jest не может импортировать x.Я получаю следующую ошибку.

  ● 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, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

 import {
           ^

    SyntaxError: Unexpected token {

В моем package.json у меня есть

"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"regenerator-runtime": "^0.13.2",
"@babel/plugin-proposal-class-properties": "^7.4.4"

Мой jest.config.js имеет:

  transform: {
    '^.+\\.js$': 'babel-jest'
  },

Это мойbabel.config.js

const presets = [
  "@babel/preset-env"
]

const plugins = [
  "@babel/plugin-proposal-class-properties"
]

module.exports = {
  presets,
  plugins
}

import работает найти, когда я просто запускаю веб-пакет, используя ту же конфигурацию babel, но как мне заставить его работать для шутливых тестов.

Спасибо за помощь!

...