【问题标题】:Testem receiving imports compiled to `require` when using ES2015/React presets to Babel当使用 ES2015/React 预设到 Babel 时,Testem 接收编译为“require”的导入
【发布时间】:2017-11-14 21:27:11
【问题描述】:

我有一个项目,它设置为使用 构建。使用node_modules/webpack/bin/webpack.js --config webpack.config.js 构建它可以正常工作,我可以在浏览器中毫无问题地运行结果。但是,当我运行 npm test 并在浏览器中查看测试运行程序时,我收到错误:"Uncaught ReferenceError: require is not defined at dashboard-tests.js:3"。似乎 webpack 正在将我的测试(但不是我的普通代码)编译成无法在浏览器中运行的 版本。

以下是相关文件:

.babelrc:

{
   "presets": ["es2015", "react"]
}

testem.json:

{
  "framework": [
    "jasmine"
  ],
  "launch_in_dev": [
    "PhantomJS"
  ],
  "launch_in_ci": [
    "PhantomJS"
  ],
  "serve_files": [
    "tmp/**/*-test{,s}.js"
  ],
  "on_start": "babel static_source --out-dir tmp",
  "on_exit": "yes | rm -r tmp"
}

package.json:

{
  "main": "index.js",
  "scripts": {
    "test": "testem"
  },
  "dependencies": {
    "jquery": "^3.2.1",
    "prop-types": "^15.6.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-feather": "^1.0.7"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "jasmine-es6": "^0.4.3",
    "testem": "^1.18.4",
    "webpack": "^3.6.0"
  }
}

webpack.config.js:

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

module.exports = {
  context: __dirname,
  entry: './static_source/js/index.js', 
  output: {
    path: path.resolve('./app/static/js/'),
    filename: "compiled.js"
  },
  plugins: [],
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
    ],
  }
}

仪表板测试:

import React from 'react';
import ReactDOM from "react-dom";
import TestUtils from "react-dom/test-utils";

import Dashboard from './dashboard';

describe('Dashboard', function() {
  it('exists', function() {
    expect(Dashboard).not.toBe(undefined);
  });

  it('renders arrows', function() {
    var dashboard = <Dashboard />;
    var renderedDashboard = TestUtils.renderIntoDocument(dashboard);

    var arrowsDiv = TestUtils.findRenderedComponentWithClass('arrows');

    expect(arrowsDiv).not.toBe(undefined);
  });
});

【问题讨论】:

标签: react jsx javascript javascript babeljs testem


【解决方案1】:

你正在使用 Babel 编译你的测试源文件。 Babel 只是一个转译器,它接收 ES+ 文件并将它们转换为 ES5 文件。因为 import 在 ES5 中不存在,所以 Babel 将它们替换为 require()

要解决此问题,您需要使用 Webpack 将源文件捆绑为一个。

您将需要一个单独的 Webpack 配置文件,我们称之为webpack.config.test.js。在这个文件中,我们需要指示 Webpack 获取你所有的 static_source/**/*-test{,s}.js 文件作为入口点。 您将需要glob 模块:npm install glob --save-dev。 然后添加具有此内容的webpack.config.test.js 文件:

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

module.exports = {
  context: __dirname,
  // we look for all file ending with -test(s?).js and return the absolute path
  entry: glob
      .sync('./static_source/js/**/*-test{,s}.js')
      .map(function(file) { return path.resolve(__dirname, file) }),
  output: {
    // save the resulting bundle in the ./tmp/ directory
    path: path.resolve('./tmp/'),
    filename: "compiled.js"
  },
  plugins: [],
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
    ],
  }
}

testem.json 文件中,将on_start 替换为before_test 以将Webpack 替换为serve_files 以映射到捆绑文件。

"before_tests": "webpack --config webpack.config.test.js ",
"serve_files": [
    "tmp/compiled.js"
],

testem 启动时会触发 Webpack 测试编译。 现在您应该有一个tmp/compiled.js 文件,其中捆绑了所有测试文件。您可能需要进行一些调整,但它应该基本上可以工作。

【讨论】:

  • 如果我没记错的话,这会编译测试文件并将它们全部混合到一个文件中 (compiled.js)。有没有办法让它包含源映射并在 testem 中显示源文件/行号?
  • 您可以将devtool: 'sourcemap' 添加到您的Webpack 配置中。如果堆栈跟踪未映射到源映射,请使用 source-map-support(例如,Webpack 配置中的 entry: ['source-map-support/register'].concat(glob.sync(...).map(...)))。
猜你喜欢
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 2016-07-19
  • 2016-05-03
  • 2019-03-10
  • 2017-06-23
相关资源
最近更新 更多