如何测试用 MikroORM 构建的 express rest api?
How can I test my express rest api built with MikroORM?
index.ts:
import 'reflect-metadata'
import express from 'express'
import { EntityManager, EntityRepository, MikroORM, RequestContext } from '@mikro-orm/core'
import options from '../mikro-orm.config'
import { Music } from './entities/music.entity'
import router from './routes'
const app = express()
export const DI = {} as {
orm: MikroORM,
em: EntityManager,
musicRepository: EntityRepository<Music>
}
async function bootstrap() {
DI.orm = await MikroORM.init(options)
DI.em = DI.orm.em
DI.musicRepository = DI.orm.em.getRepository(Music)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use((req, res, next) => RequestContext.create(DI.orm.em, next))
app.use(router)
return { app, DI }
}
bootstrap()
export default bootstrap
music.test.ts
import request from 'supertest'
import bootstrap from '../index'
describe('musics', () => {
it('should search the musics', async () => {
const { DI, app } = await bootstrap()
request(app)
.get('/musics')
.expect(200)
.end()
await DI.orm.close()
})
})
我的意思是,我想在所有测试结束后关闭连接,但我不能这样做,因为 ORM 连接是由函数 bootstrap 返回的,只能在“it "范围。
谁能帮我做这样的事情?
describe('musics', () => {
afterAll(async () => {
await DI.orm.close()
})
it('should search the musics', async () => {
const { DI, app } = await bootstrap()
request(app)
.get('/musics')
.expect(200)
.end()
})
})
存储库是:https://github.com/brenomacedo/g-track/tree/master/backend
使用beforeAll
钩子创建连接,并将结果存储在外部作用域中:
describe('musics', () => {
let context: any = {}; // TODO type this properly
beforeAll(async () => {
context = await bootstrap()
})
afterAll(async () => {
await context.DI.orm.close()
})
it('should search the musics', async () => {
request(context.app)
.get('/musics')
.expect(200)
.end()
})
})
index.ts:
import 'reflect-metadata'
import express from 'express'
import { EntityManager, EntityRepository, MikroORM, RequestContext } from '@mikro-orm/core'
import options from '../mikro-orm.config'
import { Music } from './entities/music.entity'
import router from './routes'
const app = express()
export const DI = {} as {
orm: MikroORM,
em: EntityManager,
musicRepository: EntityRepository<Music>
}
async function bootstrap() {
DI.orm = await MikroORM.init(options)
DI.em = DI.orm.em
DI.musicRepository = DI.orm.em.getRepository(Music)
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use((req, res, next) => RequestContext.create(DI.orm.em, next))
app.use(router)
return { app, DI }
}
bootstrap()
export default bootstrap
music.test.ts
import request from 'supertest'
import bootstrap from '../index'
describe('musics', () => {
it('should search the musics', async () => {
const { DI, app } = await bootstrap()
request(app)
.get('/musics')
.expect(200)
.end()
await DI.orm.close()
})
})
我的意思是,我想在所有测试结束后关闭连接,但我不能这样做,因为 ORM 连接是由函数 bootstrap 返回的,只能在“it "范围。
谁能帮我做这样的事情?
describe('musics', () => {
afterAll(async () => {
await DI.orm.close()
})
it('should search the musics', async () => {
const { DI, app } = await bootstrap()
request(app)
.get('/musics')
.expect(200)
.end()
})
})
存储库是:https://github.com/brenomacedo/g-track/tree/master/backend
使用beforeAll
钩子创建连接,并将结果存储在外部作用域中:
describe('musics', () => {
let context: any = {}; // TODO type this properly
beforeAll(async () => {
context = await bootstrap()
})
afterAll(async () => {
await context.DI.orm.close()
})
it('should search the musics', async () => {
request(context.app)
.get('/musics')
.expect(200)
.end()
})
})