Я новичок и в Typescript, и в Javascript.
Я пытаюсь смоделировать метод foo
из ServiceA
класса при выполнении метода bar
из ServiceB
класса в модульный тест с использованием JEST с Typescript :
import { ServiceB } from '../../src/services/ServiceB';
import { ServiceA } from '../../src/services/ServiceA'
jest.mock('../../src/services/ServiceA');
describe("ServiceB test suite", () => {
beforeAll(() => {
jest.clearAllMocks();
});
afterAll(() => {
jest.clearAllMocks();
});
it("successfully executes ServiceB bar method", async () => {
(ServiceA as jest.Mocked<typeof ServiceA>).foo.mockResolvedValue(30);
const serviceB = new ServiceB();
const argument = 15;
let result = await serviceB.bar(argument);
expect(result).toBe(150);
});
});
Однако в моей среде IDE указывается ошибка Property 'foo' does not exist on type 'Mocked<typeof ServiceA>'
при попытке смоделировать возвращаемое значение в строка:
(ServiceA as jest.Mocked<typeof ServiceA>).foo.mockResolvedValue(30);
Реализация ServiceB
выглядит следующим образом.
import { ServiceA } from "./ServiceA";
export class ServiceB {
bar = async (argument) => {
const serviceA = new ServiceA();
const result = await serviceA.foo(argument);
return result * 10;
}
}
Реализация ServiceB
ниже:
import { ServiceA } from "./ServiceA";
export class ServiceB {
bar = async (argument) => {
const serviceA = new ServiceA();
const result = await serviceA.foo(argument);
return result * 10;
}
}
Реализация из ServiceA
выглядит следующим образом:
export class ServiceA {
food = async (argument) => {
return (argument + 112)/22;
}
}
Что можно исправить в моей реализации, чтобы объект Mocked
нашел метод foo
в моем тесте ?