我有一个nw.js本机应用程序,内部有angular.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);
我的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;
因此,每个依赖项都包含Node.js本机模块,例如fs
,path
,child_process
,这些模块捆绑在一个包含在html中的大文件bundle.js
中,然后打包我的< strong> nw.js 应用。所以我的应用程序结构如下:
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
我正在尝试使用此结构运行测试。我尝试了业力+茉莉花,业力+摩卡茶,尝试了不同的配置,我的最后一个看起来像:
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
}
}
});
};
但是我的测试仍然没有看到角度应用。
describe('Welcome page', function() {
beforeEach(angular.mock.module('WelcomePageCtrl'));
});
PS 我不需要完全karma
和jasminne
,所以任何解决方案将不胜感激。我只想用测试覆盖我的项目
答案 0 :(得分:3)
我自己经历了类似的事情。我认为您不需要bundle.js
进行测试。
这是怎么做:
我假设您的控制器/服务/等实施如下:
/* app/controller/welcome.js */
module.exports = function(app) {
return app.controller("MyCtrl", function($scope) {
$scope.name = "lebouf";
});
};
我希望测试代码位于我正在测试的代码旁边(Welcome.spec.js
与Welcome.js
在同一目录中)。该测试代码如下所示:
/* app/controller/welcome.spec.js */
beforeEach(function() {
var app = angular.module("myApp", []); //module definition
require("./welcome")(app);
});
beforeEach(mockModule("myApp"));
describe("MyCtrl", () => {
var scope = {};
beforeEach(mockInject(function($controller) {
$controller("MyCtrl", {
$scope: scope
});
}));
it("has name in its scope", function() {
expect(scope.name).to.equal("not lebouf"); //the test will fail
});
});
除了,这是我们正在测试的角度控制器,并不是那么简单。我们需要angular
对象本身。因此,让我们进行设置。我将解释为什么要完成它,接下来要怎么做:
/* test-setup.js */
const jsdom = require("jsdom").jsdom; //v5.6.1
const chai = require("chai");
global.document = jsdom("<html><head></head><body></body></html>", {});
global.window = document.defaultView;
global.window.mocha = true;
global.window.beforeEach = beforeEach;
global.window.afterEach = afterEach;
require("angular/angular");
require("angular-mocks");
global.angular = window.angular;
global.mockInject = angular.mock.inject;
global.mockModule = angular.mock.module;
global.expect = chai.expect;
console.log("ALL SET");
我们将以以下方式运行测试:
node_modules/.bin/mocha ./init.js app/**/*.spec.js
#or preferably as `npm test` by copying the abev statement into package.json
以下是如何设置init.js
的信息:
jsdom
:如果您require("angular/angular")
,您会发现它需要一个window
实例。 jsdom
可以创建document
和window
等等,而无需使用网络浏览器!window.mocha
:我们需要angular-mocks
来用必要的实用程序填充angular
。但是if you look at the code您会注意到window.mocha || window.jasmine
必须是true
。那s why
window.mocha = true` window.beforeEach
,window.afterEach
:与上述原因相同;因为angular-mocks.js
要求这样做。angular
,expect
,mockInject
,mockModule
。这些还可能提供一些其他信息: