React Jest Unexected token Import

时间:2017-05-23 14:05:11

标签: reactjs jestjs babel-jest

尝试运行简单测试时出现导入错误: 错误:

 FAIL  Root\wwwroot\Scripts\__tests__\list_tests.js
  ● Test suite failed to run

    Root\wwwroot\Scripts\__tests__\list_tests.js:3
    import List from '../General/list';
    ^^^^^^
    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
      at process._tickCallback (internal/process/next_tick.js:109:7)

List_tests.js:

jest.dontMock();

import List from '../General/list';
...  //(the rest is irrelevant at this point).

我不熟悉react / jest中的单元测试,并希望有人可以提供帮助。我的印象是它与package.json有关?与实际答案相关,我怎么知道要添加什么package.json文件?人们总是发布配置,我真的不明白他们是如何到达那里的。请参阅下面的package.json和webpack。

Package.json中的批量信息(很多开玩笑,我从其他在线实现中提取):

  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-jest": "^20.0.3",
    "babel-loader": "^7.0.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.1",
    "jest": "^20.0.3",
    "jest-cli": "^20.0.3",
    "moment": "^2.18.1",
    "node-gyp": "^3.6.1",
    "npm": "^4.5.0",
    "office-ui-fabric-core": "^7.0.0",
    "react-calendar-timeline": "^0.11.1",
    "react-icons": "^2.2.4",
    "react-test-renderer": "^15.5.4",
    "style-loader": "^0.17.0",
    "webpack": "^2.5.1"
  },
  "dependencies": {
    "office-ui-fabric-react": "^2.27.1",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-fontawesome": "^1.6.1"
  },
    "jest": {
        "transform": "./node_modules/babel-jest",
        "testFileExtensions": [
            "es6",
            "js"
        ],
        "moduleFileExtensions": [
            "js",
            "jsx",
            "json",
            "es6"
        ]
    }

的WebPack:

var path = require('path');
var webpack = require('webpack');

module.exports = {
    context: __dirname + "/wwwroot/Scripts",
    entry: {
        entry1: './Layout/entry1.jsx',
        entry2: './Layout/entry2.jsx',
        //Should I have an entry for my tests?
    },
    output: {
        path: __dirname + '/wwwroot/Scripts/Build',
        filename: '[name].js'
    },
    module: {
        loaders: [
            {
                test: /\.(js|jsx)$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react'],
                    plugins: ['transform-class-properties']
                }
            },
            { test: /\.css$/, loader: 'style-loader!css-loader' },
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx'] 
    }
};

0 个答案:

没有答案