【问题标题】:Syntax error when testing React component Jasmine and Webpack测试 React 组件 Jasmine 和 Webpack 时出现语法错误
【发布时间】:2016-04-21 14:01:30
【问题描述】:

尝试使用 React、Karma、Jasmine 和 Webpack 运行简单测试时,我不断收到错误消息。错误是' Uncaught SyntaxError: Unexpected token

这是我的文件

karma.conf.js

var webpack = require("webpack"),
  path = require("path");

// Karma configuration

module.exports = function(config) {
  config.set({
    basePath: "",
    frameworks: ["jasmine"],
    files: [
      "../test/!**!/!*.test.js"
    ],
    preprocessors: {
      "./test/!**!/!*.test.js": ["webpack"]
    },
    webpack: {
      module: {
          loaders: [
      {
        test: /\.jsx?$/i,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-1']
        }
      },
      { test: /\.less$/, loader: "style!css!less" }
    ]
  },
      },
      plugins: [
        new webpack.ResolverPlugin([
          new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
        ])
      ],
      resolve: {
        root: __dirname,
        extensions: [
          '',
          '.json',
          '.js',
          '.jsx'
        ]
      }
    },
    webpackMiddleware: {
      noInfo: true
    },
    plugins: [
      require("karma-webpack"),
      require("karma-jasmine"),
      require("karma-chrome-launcher")
    ],
    reporters: ["dots"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["Chrome"],
    singleRun: false
  });
};

我的测试文件 example.test.js

var Comp = require('../../../js/common/components/MyComp.jsx'),
  React = require('react'),
  TestUtils = React.addons.TestUtils;

describe("Component Test", function() {
  it("renders an h1", function () {
    var component = TestUtils.renderIntoDocument(
      <Comp/> // syntax error here
    );

    var h2 = TestUtils.findRenderedDOMComponentWithTag(
      component, 'h2'
    );

    expect(h1).toExist();
  });
});

所以语法错误发生在

【问题讨论】:

    标签: testing reactjs jasmine karma-runner webpack


    【解决方案1】:

    抱歉,设置测试文件的正确路径时出错。

    files: [
      "../test/**/*.test.js"
    ],
    preprocessors: {
      "../test/**/*.test.js": ["webpack"]
    },
    

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 2016-06-14
      • 2019-02-27
      • 1970-01-01
      • 2017-04-23
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多