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

问题描述 投票:0回答:2

我添加了3个JSON文件作为动态配置,因此这些文件将在应用程序初始化时加载。

将我的 Jasmine-Karma 代码迁移到 Jest 后,我遇到了这个问题:

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

在app.module.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();
}

App模块提供

 {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},

我的测试.spec.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;
    }
}

我在测试中做错了什么?

javascript angular jestjs
2个回答
1
投票

对于因使用

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

0
投票

您的类型中可能缺少

jest-extended
。将其添加到您的
global.d.ts
文件或失败的特定测试文件中:

/// <reference types="jest-extended" />
© www.soinside.com 2019 - 2024. All rights reserved.