如何在Fastify中使用Jest和Supertest?

huangapple go评论95阅读模式
英文:

How to use Jest and Supertest with Fastify?

问题

我正在尝试在Fastify中使用Jest和Supertest。我有这个测试文件。

  1. // @ts-nocheck
  2. import { FastifyInstance } from 'fastify'
  3. import request from 'supertest'
  4. import app from '../app'
  5. let server: FastifyInstance
  6. beforeAll(async () => {
  7. server = app.server;
  8. })
  9. afterAll(() => {
  10. server.close()
  11. })
  12. test('应该返回200 OK', async () => {
  13. const response = await request(server).get('/')
  14. expect(response.status).toBe(200)
  15. })

我的 app.ts

  1. import fastify from 'fastify'
  2. import dotenv from 'dotenv'
  3. dotenv.config()
  4. import appRoute from './routes/app'
  5. const app = fastify({ logger: true })
  6. app.register(appRoute)
  7. export default app

但我总是超时... 有人能提供如何在Fastify中使用Jest和Supertest的示例吗?

英文:

I'm trying to use Jest and Supertest with Fastify. I have this test file.

  1. // @ts-nocheck
  2. import { FastifyInstance } from 'fastify'
  3. import request from 'supertest'
  4. import app from '../app'
  5. let server: FastifyInstance
  6. beforeAll(async () => {
  7. server = app.server;
  8. })
  9. afterAll(() => {
  10. server.close()
  11. })
  12. test('Should get a 200 OK', async () => {
  13. const response = await request(server).get('/')
  14. expect(response.status).toBe(200)
  15. })

My app.ts:

  1. import fastify from 'fastify'
  2. import dotenv from 'dotenv'
  3. dotenv.config()
  4. import appRoute from './routes/app'
  5. const app = fastify({ logger: true })
  6. app.register(appRoute)
  7. export default app

But I always get a timeout... Could anyone provide an example of how to use Jest and Supertest with Fastify?

答案1

得分: 1

以下是翻译好的内容:

在官方文档底部有一个关于使用 supertest 编写测试的示例。请查看 testing

确保调用 fastify.register() API 的 done 回调函数。

index.js:

  1. const Fastify = require('fastify');
  2. const app = Fastify({ logger: true });
  3. app.register((instance, opts, done) => {
  4. instance.get('/', (request, reply) => {
  5. reply.send({ hello: 'world' });
  6. });
  7. done();
  8. });
  9. module.exports = app;

index.test.js:

  1. const supertest = require('supertest');
  2. const app = require('./');
  3. afterAll(() => app.close());
  4. test('GET `/` route', async () => {
  5. await app.ready();
  6. const response = await supertest(app.server)
  7. .get('/')
  8. .expect(200)
  9. .expect('Content-Type', 'application/json; charset=utf-8');
  10. expect(response.body).toEqual({ hello: 'world' });
  11. });

测试结果:

  1. npm run test
  2. $ jest
  3. {"level":30,"time":1673234788774,"pid":70,"hostname":"noderfv2fs-ky0o","reqId":"req-1","req":{"method":"GET","url":"/","hostname":"127.0.0.1:62766"},"msg":"incoming request"}
  4. {"level":30,"time":1673234788777,"pid":70,"hostname":"noderfv2fs-ky0o","reqId":"req-1","res":{"statusCode":200},"responseTime":2.5850000000000364,"msg":"request completed"}
  5. PASS ./index.test.js
  6. GET `/` route (489 ms)
  7. Test Suites: 1 passed, 1 total
  8. Tests: 1 passed, 1 total
  9. Snapshots: 0 total
  10. Time: 2.269 s, estimated 3 s
  11. Ran all test suites.

stackblitz(请使用Chrome打开,在Microsoft Edge上运行 npm run test 时可能会出现错误)。

英文:

There is an example in the official document at the bottom about writing testings with supertest. See testing

Make sure you call the done callback of the fasity.register() API.

index.js:

  1. const Fastify = require('fastify');
  2. const app = Fastify({ logger: true });
  3. app.register((instance, opts, done) => {
  4. instance.get('/', (request, reply) => {
  5. reply.send({ hello: 'world' });
  6. });
  7. done();
  8. });
  9. module.exports = app;

index.test.js:

  1. const supertest = require('supertest');
  2. const app = require('./');
  3. afterAll(() => app.close());
  4. test('GET `/` route', async () => {
  5. await app.ready();
  6. const response = await supertest(app.server)
  7. .get('/')
  8. .expect(200)
  9. .expect('Content-Type', 'application/json; charset=utf-8');
  10. expect(response.body).toEqual({ hello: 'world' });
  11. });

Test result:

  1. npm run test
  2. $ jest
  3. {"level":30,"time":1673234788774,"pid":70,"hostname":"noderfv2fs-ky0o","reqId":"req-1","req":{"method":"GET","url":"/","hostname":"127.0.0.1:62766"},"msg":"incoming request"}
  4. {"level":30,"time":1673234788777,"pid":70,"hostname":"noderfv2fs-ky0o","reqId":"req-1","res":{"statusCode":200},"responseTime":2.5850000000000364,"msg":"request completed"}
  5. PASS ./index.test.js
  6. GET `/` route (489 ms)
  7. Test Suites: 1 passed, 1 total
  8. Tests: 1 passed, 1 total
  9. Snapshots: 0 total
  10. Time: 2.269 s, estimated 3 s
  11. Ran all test suites.

stackblitz(Open using Chrome, there is a bug when run npm run test using Microsoft Edge)

huangapple
  • 本文由 发表于 2023年1月5日 04:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75011078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定