Я пытаюсь добавить несколько шутливых тестов в свой проект node typecipt. Я хотел бы использовать supertest для вызова моего маршрутизатора koa, но также использовать fetch-mock для имитации запросов, сделанных с помощью node-fetch.
Мое решение до сих пор ниже, но выборка в моем маршрутизаторе не использует имитацию настройки выборки с помощью fetch-mock. Модульный тест терпит неудачу, потому что ожидаемый ответ, который я высмеивал, не возвращается. Я пытался следовать документация для насмешки над глобальной выборкой, но безуспешно, а машинописный текст затрудняет отслеживание решений, не связанных с машинописным текстом, которые я нашел. Я хотел бы избежать использования неглобальной песочницы, если это возможно, так как мне пришлось бы переписать много кода, чтобы передать выборку.
server.spec.ts
import * as fetchMock from 'fetch-mock';
import * as request from 'supertest';
import server from './server';
afterEach(() => {
server.close();
fetchMock.reset();
});
describe('router', () => {
test('GET: should return data', async () => {
const expectedResponse = { test: 'TEST' };
fetchMock.get('https://myapi.com/test', expectedResponse);
const response = await request(server).get('/test');
expect(response.status).toEqual(200);
expect(response.body).toMatchObject(expectedResponse);
});
});
server.ts
import * as Koa from 'koa';
import * as Router from 'koa-router';
import fetch from 'node-fetch';
const app = new Koa();
const router = new Router();
router.get('/test', async ctx => {
const options = { method: 'GET' };
try {
const response = await fetch('https://myapi.com/test', options);
ctx.body = await response.json();
} catch (error) {
error.fetchUrl = url;
throw error;
}
});
app.use(router.routes());
const server = app.listen(3000);
export default server;
Вы можете издеваться над модулем node-fetch
самостоятельно. Вот решение:
server.ts
:
import Koa from 'koa';
import Router from 'koa-router';
import fetch from 'node-fetch';
const app = new Koa();
const router = new Router();
router.get('/test', async ctx => {
const options = { method: 'GET' };
const url = 'https://myapi.com/test';
try {
const response = await fetch(url, options);
ctx.body = await response.json();
} catch (error) {
error.fetchUrl = url;
throw error;
}
});
app.use(router.routes());
function createHttpServer() {
return app.listen(3000);
}
if (require.main === module) {
createHttpServer();
}
export default createHttpServer;
server.spec.ts
:
import request from 'supertest';
import createHttpServer from './server';
import fetch from 'node-fetch';
const { Response } = jest.requireActual('node-fetch');
const server = createHttpServer();
jest.mock('node-fetch', () => jest.fn());
afterAll(done => {
server.close(done);
});
describe('router', () => {
test('GET: should return data', async () => {
const expectedResponse = { test: 'TEST' };
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(new Response(JSON.stringify(expectedResponse)));
const response = await request(server).get('/test');
expect(response.status).toEqual(200);
expect(response.body).toEqual(expectedResponse);
});
test('GET: should throw error', async () => {
const mockedFetchError = new Error('some error');
(fetch as jest.MockedFunction<typeof fetch>).mockRejectedValueOnce(mockedFetchError);
const response = await request(server).get('/test');
expect(response.status).toEqual(500);
});
});
Результат модульного теста с отчетом о покрытии:
PASS src/stackoverflow/56735795/server.spec.ts (8.487s)
router
✓ GET: should return data (51ms)
✓ GET: should throw error (15ms)
console.error node_modules/koa/lib/application.js:200
undefined
console.error node_modules/koa/lib/application.js:201
Error: some error
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:26:30)
at step (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:32:23)
at Object.next (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:13:53)
at /Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:7:71
at new Promise (<anonymous>)
at Object.<anonymous>.__awaiter (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:3:12)
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/src/stackoverflow/56735795/server.spec.ts:25:35)
at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:102:37)
at resolve (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
at new Promise (<anonymous>)
at mapper (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
at promise.then (/Users/ldu020/workspace/github.com/mrdulin/jest-codelab/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
at process._tickCallback (internal/process/next_tick.js:68:7)
console.error node_modules/koa/lib/application.js:202
undefined
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 95.24 | 50 | 100 | 94.12 | |
server.ts | 95.24 | 50 | 100 | 94.12 | 28 |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 10.36s
Вот завершенная демонстрация: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56735795
эта строка ;(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValueOnce(new Response(JSON.stringify(expectedResponse)))
выдает мне эту ошибку TypeError: _nodeFetch.default.mockResolvedValueOnce is not a function
- идеи?
Я попытался заставить node-fetch
издевается работать с jest
и машинопись, используя руководствоГлобальныйнасмешливый и набор разныепакеты, как это предлагается в других ответах. Вот единственное, что сработало для меня;
install 'jest-fetch-mock'
Создайте тесты;
// To mock fetch we need to import and enable before all other imports
import { enableFetchMocks } from 'jest-fetch-mock';
enableFetchMocks();
import ...
import ...
import ...
import fetch from 'node-fetch';
describe("I lost too many hours on this", () => {
let spyFetch;
beforeAll(() => {
spyFetch = (fetch as jest.MockedFunction<typeof fetch>);
spyFetch.mockImplementation(() => Promise.resolve({json: () => 'YMMV'}));
});
afterEach(() => {
spyFetch.mockClear();
});
it("Call method which contains fetch", async () => {
// Setup
...
await exampleClass.methodContainingFetch('TestUrl');
expect(spyFetch.mock.calls[0][0]).toBe('TestUrl');
});
});
Если у вас возникли проблемы с подсказками машинописного текста, какое-то время я использовал
spyFetch = (fetch as any)
Вышеупомянутый супертест отсутствует, но я сомневаюсь, что у людей, сталкивающихся с этим вопросом, есть проблемы с этой частью.
Спасибо за это решение. Мне пришлось добавить
"esModuleInterop": true
в мой tsconfig.json, но после этого все заработало.