0

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

 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"
        ]
    }

网页包:

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'] 
    }
};
4

0 回答 0