我想测试一个具有角度(1.6)作为依赖项的 ng-redux 减速器。当我用 mocha 运行测试(npm test)时,我得到:
/data/work/mocha-angularjs/node_modules/angular/angular.js:33343
})(window);
^
ReferenceError: window is not defined
我尝试添加 jsdom 以提供假窗口。但是在导入角度时它仍然失败并出现此错误:
module.exports = angular;
^
ReferenceError: angular is not defined
有没有办法让 Angular 在 mocha/babel 世界中正常工作?
我在这里提供了一个小型 github 项目,它重现了该问题。
这是项目的内容:
包.json
{
"name": "mocha-angularjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"angular": "^1.6.3"
},
"devDependencies": {
"babel-core": "^6.24.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-latest": "^6.24.0",
"chai": "^3.5.0",
"jsdom": "9.12.0",
"jsdom-global": "2.1.1",
"mocha": "^3.2.0"
},
"scripts": {
"test": "NODE_ENV=test mocha src/index.test.js --compilers js:babel-register --require jsdom-global/register"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jtassin/mocha-angularjs.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/jtassin/mocha-angularjs/issues"
},
"homepage": "https://github.com/jtassin/mocha-angularjs#readme"
}
.babelrc
{
"plugins": [],
"presets": [
"latest",
]
}
要测试的代码
import angular from 'angular';
export default function getFive() {
return 5;
}
考试
import expect from 'chai';
import getFive from './index';
describe('desc', () => {
it('my test', () => {
expect(getFive()).to.equal(5);
});
});