Angular.js + nw.js + webpack + karma + jasmine如何测试

Art*_*rti 5 javascript node.js angularjs karma-jasmine

我有一个带有angular.jsnw.js本机应用程序.我的应用程序与webpack捆绑在一起,包含本机node.js模块.我的入口点是我组织的index.js文件:

var gui = require('nw.gui');
var angular = require('angular');
require('./app.css');

// other modules

var myApp = angular.module('myApp', [
    'ngRaven',
    'ngMaterial',
    'ngMessages'
]).constant(
    'fs', require('fs')
)

require('./services')(myApp);
require('./directives')(myApp);
require('./factories')(myApp);
require('./filters')(myApp);
require('./controllers')(myApp);
require('./app.js')(myApp);
Run Code Online (Sandbox Code Playgroud)

我的webpack配置如下所示:

const path = require('path');

const config = {
    entry: [
        './app/index.js'
    ],
    output: {
        path: path.resolve(__dirname, 'app'),
        filename: 'bundle.js'
    },
    devtool: "source-map",
    target: 'node-webkit',
    module:{
        // css, html loaders
    },
    node: {
        os: true,
        fs: true,
        child_process: true,
        __dirname: true,
        __filename: true
    }
};

module.exports = config;
Run Code Online (Sandbox Code Playgroud)

所以每依赖性包括Node.js的本机模块一样fs,path,child_process在一个大的文件中捆绑的bundle.js,我在HTML中包括再包我nw.js应用程序.所以我的app结构如下:

my_project:
--app
----controllers
------welcome
--------welcome.js // Page controller
--------welcome.html // Page HTML
------index.js // here I include each page controller
----app.js // My angular app initialization
----index.js // here I include all dependencies 
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用这种结构运行测试.我尝试了业力+茉莉,业力+摩卡,尝试了不同的配置,我的最后一个看起来像:

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            'app/bundle.js',
            'app/**/*spec.js'
        ],
        exclude: [],
        preprocessors: {
            'app/bundle.js': ['webpack']
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox']
            }
        },
        singleRun: true,

        webpack: {
            // you don't need to specify the entry option because
            // karma watches the test entry points
            // webpack watches dependencies

            // ... remainder of webpack configuration (or import)
        },

        webpackMiddleware: {
            // webpack-dev-middleware configuration
            // i.e.
            noInfo: true,
            // and use stats to turn off verbose output
            stats: {
                // options i.e.
                chunks: false
            }
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

但我的测试仍然没有看到角度应用.

describe('Welcome page', function() {
    beforeEach(angular.mock.module('WelcomePageCtrl'));
});
Run Code Online (Sandbox Code Playgroud)

PS我不需要完全karmajasminne,所以任何解决方案将不胜感激.我只想用测试来覆盖我的项目