下面有一个.ts模块
import client from './client';
export default class DefaultRequest implements IRequest {
make(req: Request): Promise<Response> {
return new Promise<Response>((resolve, reject) => {
client.post(req, (error: Error | null, res: Response) => {
if (error) {
return reject(error);
} else {
return resolve(res);
}
});
});
}
}我试图用ts-jest为这个类编写一个单元测试,以便对client进行模拟(并返回一些有效的Response)。
我就是这样做的:
import {mocked} from 'ts-jest/utils';
import client from './client';
import DefaultRequest from './request'
const mockedClient = mocked(client, true);
const testRequest = new DefaultRequest();
jest.mock('./client', () => {
return {
RestClient: jest.fn().mockImplementation(() => {
return {
post: () => {return someValidResponse();},
};
})
};
});
describe('My Tests', () => {
it('Unit Test 1', async () => {
let res: Response = await testRequest.make(buildReq());
});
});但mockedClient仍未受到嘲弄。这就是./client.ts的样子:
import { RestClient } from '@rest/client';
export default new RestClient();类DefaultRequest所使用的内部DefaultRequest模块可以以这种方式进行模拟吗?
编辑:我也尝试过jest.spyOn
const spiedMethod= jest.spyOn(client, 'post');
const call: Request = new Request();
const response: Response = await call.make(buildRequest());
expect(spiedReleaseMethod).toHaveBeenCalled();
expect(response.getResponsecode()).toBe(200);它仍然调用原始方法,而不是间谍方法。
发布于 2021-06-25 06:09:51
您正在测试依赖于request.ts模块的client.ts模块。因此,您需要模拟client.ts模块及其post方法,而不是@rest/client包。
例如。
request.ts
import client from './client';
interface IRequest {
make(req: Request): Promise<Response>;
}
export default class DefaultRequest implements IRequest {
make(req: Request): Promise<Response> {
return new Promise<Response>((resolve, reject) => {
client.post(req, (error: Error | null, res: Response) => {
if (error) {
return reject(error);
} else {
return resolve(res);
}
});
});
}
}client.ts:(无论客户端使用什么包实现,只要模块公开的接口相同)
export default {
post(req, callback) {
console.log('real implementation');
},
};request.test.ts
import { mocked } from 'ts-jest/utils';
import client from './client';
import DefaultRequest from './request';
jest.mock('./client');
const mockedClient = mocked(client);
describe('68115300', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', () => {
mockedClient.post.mockImplementationOnce((req, callback) => {
callback(null, 'mocked response');
});
const testRequest = new DefaultRequest();
testRequest.make(('req' as unknown) as Request);
expect(mockedClient.post).toBeCalledWith('req', expect.any(Function));
});
});测试结果:
PASS examples/68115300/request.test.ts (12.448 s)
68115300
✓ should pass (3 ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 77.78 | 50 | 75 | 77.78 |
client.ts | 50 | 100 | 0 | 50 | 3
request.ts | 85.71 | 50 | 100 | 85.71 | 12
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.61 shttps://stackoverflow.com/questions/68115300
复制相似问题