我正在使用 Jest 在 Node.js 应用程序上执行单元测试,其中代码源是用 TypeScript 编写的,然后编译成 JavaScript。
在我希望测试的一个类中,导入了一个外部模块并使用了该模块中的一个方法。我想模拟对这个方法的调用,以便只测试我的代码。
但是,当我运行测试时,出现以下错误:TypeError: Cannot redefine property: methodName
问题是此方法具有以下对象属性:
{ value: [Function],
writable: false,
enumerable: true,
configurable: false }
configurable: false
是什么使它成为一个大问题。我无法在模拟调用之前重新定义属性以使其可写。以下是相关代码的样子:
测试类
import externalType from 'external-module-name';
export class ClassName {
public propertyName: externalType;
public method(param: string): Promise<any> {
return new Promise((resolve, reject) => {
this.propertyName.externalMethod(param)
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
}
单元测试
import { ClassName } from 'path/to/class';
describe('class', () => {
const class = new ClassName;
it("Blahblah", (done) => {
Object.defineProperty(class['propertyName'], 'externalMethod', {writable: true});
const spy = jest.spyOn(class['propertyName'], 'externalMethod').mockReturnValue(Promise.resolve());
class.method('string')
.then((result) => {
// Various expect()
done();
});
});
});
到目前为止我尝试过的
Object.defineProperty(class['module'], 'methodName', {writable: true});
jest.spyOn(class['module'], 'methodName').mockReturnValue(Promise.resolve());
class.propertyName.externalMethod = jest.fn().mockImplementation((query) => {
return Promise.resolve();
});
class.propertyName = <any> {
externalMethod = (param: any) => {
return Promise.resolve();
}
}
对于这个我得到错误
TypeError: Cannot assign to read only property externalMethod of object class
,这是有道理的,因为 readable 设置为 false。但是属性
configurable
似乎阻止了一切。 .我确信可以做一些事情,因为我可能不是唯一一个想要对导入外部模块的类执行单元测试的人,尽管它是安全的。所以我的问题是:模拟外部方法的干净且有效的方法是什么? 如果绝对不可能,在不调用该外部方法的情况下测试我的类(class)的方法是什么?
提前致谢!
最佳答案
我发现这样做:
jest.mock('path/to/class');
我能够解决这个问题。
关于javascript - 覆盖对象属性以进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755086/