admin管理员组文章数量:1294048
I have generic service class which is abstract.
export default abstract class GenericService<Type> implements CrudService<Type> {
private readonly modifiedUrl: URL;
public constructor(url: string) {
this.modifiedUrl = new URL(url, window.location.href);
}
public async get(path?: string, filter?: URLSearchParams): Promise<Type> {
try {
if (path) {
this.modifiedUrl.href += `${path}`;
}
addQueryParams(this.modifiedUrl, filter);
const response = await handleRequest(`${this.modifiedUrl}`, getFetchOptions('GET'));
const data = await response.json();
return (await data.data) ? data.data : data;
} catch (error) {
throw new Error(`Runtime error: ${error}`);
}
}
}
export async function handleRequest(input: RequestInfo, init: RequestInit): Promise<Response> {
const response = await fetch(input, init);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response}`);
}
return response;
}
I need to cover this GenericService
's get
method with tests. I've tried this:
jest.mock('../../ponents/crudTable/service/GenericService');
const genericService = GenericService;
export class DummyClass {
public name: string = '';
}
export class DummyService extends GenericService<DummyClass> {}
describe('Generic Service', () => {
it('1- spy prototype function', async () => {
const spy = jest.spyOn(genericService.prototype, 'get');
await genericService.prototype.get();
expect(spy).toHaveBeenCalledTimes(1);
});
it('2- mock prototype function', async () => {
const mockFn = jest.fn(genericService.prototype.get);
await mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('3- mock subclass function', async () => {
const dummyService = new DummyService('test url');
const mockFn = jest.fn(dummyService.get);
await mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
This test works but the coverage stats says that it is still not covered.
So how can I covert the all get
method of GenericService
?
I have generic service class which is abstract.
export default abstract class GenericService<Type> implements CrudService<Type> {
private readonly modifiedUrl: URL;
public constructor(url: string) {
this.modifiedUrl = new URL(url, window.location.href);
}
public async get(path?: string, filter?: URLSearchParams): Promise<Type> {
try {
if (path) {
this.modifiedUrl.href += `${path}`;
}
addQueryParams(this.modifiedUrl, filter);
const response = await handleRequest(`${this.modifiedUrl}`, getFetchOptions('GET'));
const data = await response.json();
return (await data.data) ? data.data : data;
} catch (error) {
throw new Error(`Runtime error: ${error}`);
}
}
}
export async function handleRequest(input: RequestInfo, init: RequestInit): Promise<Response> {
const response = await fetch(input, init);
if (!response.ok) {
throw new Error(`Network response was not ok: ${response}`);
}
return response;
}
I need to cover this GenericService
's get
method with tests. I've tried this:
jest.mock('../../ponents/crudTable/service/GenericService');
const genericService = GenericService;
export class DummyClass {
public name: string = '';
}
export class DummyService extends GenericService<DummyClass> {}
describe('Generic Service', () => {
it('1- spy prototype function', async () => {
const spy = jest.spyOn(genericService.prototype, 'get');
await genericService.prototype.get();
expect(spy).toHaveBeenCalledTimes(1);
});
it('2- mock prototype function', async () => {
const mockFn = jest.fn(genericService.prototype.get);
await mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('3- mock subclass function', async () => {
const dummyService = new DummyService('test url');
const mockFn = jest.fn(dummyService.get);
await mockFn();
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
This test works but the coverage stats says that it is still not covered.
So how can I covert the all get
method of GenericService
?
1 Answer
Reset to default 6You can consider the following approach
GenericService.spec.js
import GenericSerice from "./GenericService";
class DummyService extends GenericSerice {}
describe("GenericSerice", () => {
beforeAll(() => {
global.fetch = jest.fn();
});
describe("extended by a class", () => {
let instance;
beforeAll(() => {
instance = new DummyService();
});
describe("method get", () => {
describe("with path given", () => {
const mockPath = "/pa/th";
describe("receiving successful response", () => {
let result;
const mockData = { key: "mock value" };
beforeAll(async () => {
global.fetch.mockClear();
global.fetch.mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockData)
});
result = await instance.get(mockPath);
});
it("should return data", () => {
expect(result).toEqual(mockData);
});
it("should request the correct URL", () => {
expect(global.fetch).toHaveBeenCalledWith(
"http://localhost/undefined/pa/th",
{
method: "GET"
}
);
});
});
});
});
});
});
check the full coverage example here
本文标签: javascriptCover abstract class method with tests in JestStack Overflow
版权声明:本文标题:javascript - Cover abstract class method with tests in Jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741581572a2386599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论