NestJS+jestのテストでHTTP通信部分をモックする
7763 ワード
NestJSで実装したコントローラで、HttpServiceを使ってHTTP通信をしている処理があったのだが、テストを書く際にHTTP通信部分をどうにかする必要があった。
Dockerでローカルにサーバーを立てることも考えたが、jestでモックできたのでメモとして残しておく。
import { HttpService, INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AxiosResponse } from 'axios';
import { of } from 'rxjs';
import * as request from 'supertest';
describe('controller test', () => {
let app: INestApplication;
let module: TestingModule;
let httpService: HttpService;
describe('hoge endpoint', () => {
beforeAll(async () => {
module = await Test.createTestingModule({}).compile();
app = module.createNestApplication();
await app.init();
httpService = module.get<HttpService>(HttpService);
});
afterAll(async () => {
await app.close();
await module.close();
});
it('hoge', async () => {
const params = {
hoge: 'hoge',
fuga: 'fuga',
};
const generatePostRequest = () =>
request(app.getHttpServer())
.post('/hoge/endpoint');
jest.spyOn(httpService, 'post').mockImplementationOnce(() =>
of({
data: {
hoge: '1',
fuga: 2,
},
} as AxiosResponse),
);
await generatePostRequest()
.send(params)
.then((response: any) => {
expect(response.body.success).toBe(1);
expect(response.body.data).toBeTruthy();
});
});
});
});
mockImplementationOnceで期待している戻り値を返却するようにモックすればOK.
Author And Source
この問題について(NestJS+jestのテストでHTTP通信部分をモックする), 我々は、より多くの情報をここで見つけました https://qiita.com/dosukoi_kdm/items/0967b32ce8b32b92c66e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .