Jest - 模拟导入的依赖项或服务实例

时间:2021-03-19 11:34:29

标签: javascript angular ecmascript-6 jestjs

我有一个像 ES6 Angular JS 的服务

//in services config file
app.service('ServiceName', ['dep1', 'dep2', 'ServiceName']);

//in service file
import { importeddep } from '../path'

export default class ServiceName{
  //and inside service i assign the depenndencies as 
  constructor('dep1', 'dep2') {
   this.dep1 = dep1;
   this.dep2 = dep2;

   // now i have a 3rd dep which is actually an import 
   this.dep3 = importeddep.someMethod('param')
  }
  
}


//now in test file servicename.test.js
beforeEach(() => {
 // I mock dep1 and dep2
})

// then I inject the dependencies mock as below
const service = function() {
 return new ServiceName(dep1mock, dep2mock);
}

这里的问题是我应该如何模拟 dep3,它是我从其他文件导入的对象实例。感谢您的帮助。

0 个答案:

没有答案