配置Webpack执行TypeScript + Mocha测试

时间:2019-03-07 21:25:08

标签: javascript typescript webpack mocha

我想将webpack配置为将以打字稿编写的测试(mocha + chai)转换为javascript,然后执行它们。为此,我在下面有 webpack.test.config.js 文件。注意:我目前已将webpack配置为使用 ts-loader mocha-loader

不幸的是,当我执行webpack --config webpack.test.config.js --env.dev时,收到错误消息:

  

模块解析失败:意外令牌(2:10)您可能需要一个   适当的加载程序来处理此文件类型。

如何解决此错误并实现我的上述目标?

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const outputPath = './bin/test';

module.exports = env => {
  return {
    mode: env && env.pro ? 'production' : 'development',
    context: path.resolve('src'),
    entry: {
      core: './test/typescript/core.spec.ts'
    },
    output: {
      filename: '[name].js',
      path: path.join(__dirname, outputPath)
    },
    devtool: 'source-map',
    plugins: [
      new CleanWebpackPlugin({
        dry: true,
        cleanOnceBeforeBuildPatterns: ['./bin/test/**/*']
      })
    ],
    module: {
      rules: [
        {
          test: /\.spec\.tsx?$/,
          use: ['mocha-loader', 'ts-loader'],
          exclude: /node_modules/
        }
      ]
    },
    resolve: {
      extensions: ['.tsx', '.ts', '.js']
    }
  };
};

0 个答案:

没有答案