javascript - 覆盖对象属性以进行单元测试

标签 javascript typescript jestjs object-properties

我正在使用 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/

    相关文章:

    reactjs - Jest 和 React Native : How to mock AccessibilityInfo

    javascript - 在保留图像的同时在 HTML5 Canvas 中绘制图像

    javascript - 不同ID的链接设置值的onclick

    javascript - 正则表达式:获取捕获组的位置列表,而不是匹配项

    Angular 在后台显示推送通知

    javascript - 在 TypeScript 中将类转换为模块

    javascript - knockout 移动js刷新

    javascript - 在顶层将 Redux 连接到我的应用程序不起作用

    typescript - 是什么导致 Jest 在此代码中显示 75% 的分支覆盖率?

    javascript - 如何在 Jest 测试中模拟影子元素