如何在所有其他人之前执行karma index.html文件?

时间:2018-04-12 07:01:17

标签: javascript webpack jasmine karma-runner karma-jasmine

我有以下业力配置文件(相关属性):

 files: [
      { pattern: './src/*.html', watched: true, served: true, included: false },
      // polyfill for es6 Promise
      'node_modules/es6-promise/dist/es6-promise.auto.js',
      // polyfill for fetch.js
      'node_modules/whatwg-fetch/fetch.js',
      './src/**/*.ts'
    ],
    preprocessors: {
      '**/*.html': ['html2js'],
      '**/*.ts': 'karma-typescript',
      'karma-babel-preprocessor': ['babel']
    },

我正在使用ES6模块项目(没有React,Angular2 +设置)并尝试设置我的测试以便正常运行。

主要问题是在我的.ts文件中(不是spec.ts - >目标仅用于测试的文件)我有一个doccument.getElementById('').someAttribute在测试运行时断开,因为它是空的。之所以发生这种情况,是因为尚没有将业力设置为index.html。

如何解决index.html首先构建的问题,然后解决所有其他.ts文件......

运行 npm test 时出错:*

  

" message":" Uncaught TypeError:无法设置属性< innerHTML'的   空\ NAT

1 个答案:

答案 0 :(得分:0)

您是否尝试使用'karma-babel-preprocessor'和'karma-typescript'来使用'webpack'进行翻译Babel或Typescript?

以下是https://www.npmjs.com/package/awesome-typescript-loader,可用于使用webpack进行TypeScript处理。

使用Babel和Typescript可能会有一些冲突,因为Typescript可以在没有babel的情况下进行转换,但是你可以在webpack配置中处理它。以下是“karma.conf”与Babel的相似之处:

const webpack = require('webpack');

module.exports = function (config) {
var configuration = {
    browsers: ['Chrome'],
    files: [
        { pattern: 'test/**/*.js', watched: true },
    ],
    frameworks: ['jasmine'],
    preprocessors: {
        'test/**/*.js': ['webpack'],
    },
    webpack: {
        module: {
            loaders: [
                { test: /\.js/, exclude: /node_modules/, loader: 'babel-loader' }
            ]
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: JSON.stringify('test')
                }
            })
        ],
        watch: true
    },
    webpackServer: {
        noInfo: true
    }
};
config.set(configuration);
};