我有karma的默认配置:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'./app/*.js',
'./app/*.spec.js'
],
exclude: [],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
我的package.json:
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/karma/bin/karma start karma.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jasmine-core": "^2.4.1",
"karma": "^0.13.21",
"karma-chrome-launcher": "^0.2.2",
"karma-jasmine": "^0.3.7",
"requirejs": "^2.1.22"
}
在文件夹应用程序中,我有两个文件,calculator.js:
'use strict';
window.calculator = window.calculator || {};
(function() {
var getIntById = function(id) {
return parseInt(document.getElementById(id).value, 10);
};
var calculate = function() {
var sum = getIntById('x') + getIntById('y');
document.getElementById('result').innerHTML = isNaN(sum) ? 0 : sum;
};
window.calculator.init = function() {
document.getElementById('add').addEventListener('click', calculate);
};
})();
和calculator.spec.js:
/*
* Unit tests for lib/calculator.js
*/
describe('Calculator', function() {
// inject the HTML fixture for the tests
beforeEach(function() {
var fixture = '<div id="fixture"><input id="x" type="text">' +
'<input id="y" type="text">' +
'<input id="add" type="button" value="Add Numbers">' +
'Result: <span id="result" /></div>';
document.body.insertAdjacentHTML(
'afterbegin',
fixture);
});
// remove the html fixture from the DOM
afterEach(function() {
document.body.removeChild(document.getElementById('fixture'));
});
// call the init function of calculator to register DOM elements
beforeEach(function() {
window.calculator.init();
});
it('should return 3 for 1 + 2', function() {
document.getElementById('x').value = 1;
document.getElementById('y').value = 2;
document.getElementById('add').click();
expect(document.getElementById('result').innerHTML).toBe('3');
});
it('should calculate zero for invalid x value', function() {
document.getElementById('x').value = 'hello';
document.getElementById('y').value = 2;
document.getElementById('add').click();
expect(document.getElementById('result').innerHTML).toBe('0');
});
it('should calculate zero for invalid y value', function() {
document.getElementById('x').value = 1;
document.getElementById('y').value = 'goodbye';
document.getElementById('add').click();
expect(document.getElementById('result').innerHTML).toBe('0');
});
});
现在,当我尝试使用npm test karma打开连接并使用chrome和打开选项卡进行测试时,但是当我查看devTools并转到源代码时,我找不到我的calculator.js和spec文件。
为什么业力没有执行我的spec文件?