类型“JestMatchers〈Mock〈any,any>>”上不存在属性“toHaveBeenCalledOnceWith”

rjee0c15  于 2022-12-20  发布在  Jest
关注(0)|答案(1)|浏览(185)

我已经添加了3个JSON文件作为动态配置,因此这些文件将在应用程序初始化时加载。
在将Jasmine-Karma代码迁移到Jest之后,我遇到了这个问题:

Property 'toHaveBeenCalledOnceWith' does not exist on type 'JestMatchers<Mock<any, any>>'.

在应用程序模块ts中

export function configLoader(injector: Injector) : () => Promise<any>
{
    return () => injector.get(ConfigurationService).loadConfiguration();
}
export function configProdLoader(injector: Injector) : () => Promise<any> {
    return () => injector.get(ConfigurationService).loadProdConfig();
}

export function configEnvironmentLoader(injector: Injector) : () => Promise<any>
{
    return () => injector.get(ConfigurationService).loadEnvironmentConfig();
}

应用程序模块提供

{provide: APP_INITIALIZER, useFactory: configLoader, deps: [Injector], multi: true},
        {provide: APP_INITIALIZER, useFactory: configProdLoader, deps: [Injector], multi: true},
        {provide: APP_INITIALIZER, useFactory: configEnvironmentLoader, deps: [Injector], multi: true},

我的测试规范ts

describe("ConfigurationService", () => {

    const returnValue = {};

    let httpMock: {get: jest.Mock};

    let service: ConfigurationService;

    beforeEach(() => {
        httpMock = {
            get: jest.fn(() => of(returnValue)),
        };

        service = new ConfigurationService(<any>httpMock);
    });

    test('Should call the endpoint and retrieve the config', (done) => {
        service.loadConfiguration().then(() => {
            expect(httpMock.get)
                .toHaveBeenCalledOnceWith(service['configPath']);
            expect(service['configData']).toBe(returnValue);
            done();
        });
    });

    test('Should call the endpoint and retrieve the configProd', (done) => {
        service.loadProdConfig().then(() => {
            expect(httpMock.get)
                .toHaveBeenCalledOnceWith(service['configProdPath']);
            expect(service['configProdData']).toBe(returnValue);
            done();
        });
    });

    test('Should call the endpoint and retrieve the configEnvironment', (done) => {
        service.loadEnvironmentConfig().then(() => {
            expect(httpMock.get)
                .toHaveBeenCalledOnceWith(service['configEnvironmentPath']);
            expect(service['configEnvironmentData']).toBe(returnValue);
            done();
        });
    });

});

我的服务.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ConfigurationService {
    private configData: any | undefined;
    private configProdData: any | undefined;
    private configEnvironmentData: any | undefined;
    private readonly configPath: string = '../../assets/config/aws-config.json';
    private readonly configProdPath: string = '../../assets/config/prod-config.json';
    private readonly configEnvironmentPath: string = '../../assets/config/environment-config.json';

  constructor(private httpClient: HttpClient) { }

    async loadConfiguration(): Promise<any> {
        try {
            const response = await this.httpClient.get(`${this.configPath}`)
                .toPromise().then(res => this.configData = res);
            return this.configData;
        } catch (err) {
            return Promise.reject(err);
        }
    }

    get config(): any | undefined {
        return this.configData;
    }

    async loadProdConfig(): Promise<any> {
        try {
            const response = await this.httpClient.get(`${this.configProdPath}`)
                .toPromise().then(res => this.configProdData = res);
            return this.configProdData;
        } catch (err) {
            return Promise.reject(err);
        }
    }

    get configProd(): any | undefined {
        return this.configProdData;
    }

    async loadEnvironmentConfig(): Promise<any> {
        try {
            const response = await this.httpClient.get(`${this.configEnvironmentPath}`)
                .toPromise().then(res => this.configEnvironmentData = res);
            return this.configEnvironmentData;
        } catch (err) {
            return Promise.reject(err);
        }
    }

    get configEnvironmentProd(): any | undefined {
        return this.configEnvironmentData;
    }
}

我在考试中做错了什么?

tvmytwxo

tvmytwxo1#

对于任何发现这个的人,因为他们正在使用aws-sdk-client-mock ...
您需要导入aws-sdk-client-mock [1]附带的笑话匹配器。
import 'aws-sdk-client-mock-jest';
我怀疑您还缺少这些定制匹配器定义的导入。

  1. https://github.com/m-radzikowski/aws-sdk-client-mock#jest-matchers

相关问题