Jest - 对命名空间文件进行单元测试
Jest - Unit Testing a Namespaced File
是否可以使用 Jest 为使用以下格式命名空间的 JavaScript 文件编写单元测试?
// utilities.js
var MyApp = MyApp || {};
MyApp.Common = MyApp.Common || {};
MyApp.Common.Utilities = new function(){
this.getSomething = function(){ // Need to unit test this function
return 'Something';
}
}
如果没有,是否有更好的方法来构建命名空间以允许进行单元测试?
如果您像下面的示例那样导出 MyApp,那么在 utilities.test.js 中您可以
const {MyApp} = require('./utilities');
it('should work', ()=>{
expect(MyApp.Common.Utilities.getSomething()).toEqual('something');
});
是否可以使用 Jest 为使用以下格式命名空间的 JavaScript 文件编写单元测试?
// utilities.js
var MyApp = MyApp || {};
MyApp.Common = MyApp.Common || {};
MyApp.Common.Utilities = new function(){
this.getSomething = function(){ // Need to unit test this function
return 'Something';
}
}
如果没有,是否有更好的方法来构建命名空间以允许进行单元测试?
如果您像下面的示例那样导出 MyApp,那么在 utilities.test.js 中您可以
const {MyApp} = require('./utilities');
it('should work', ()=>{
expect(MyApp.Common.Utilities.getSomething()).toEqual('something');
});