【发布时间】:2021-10-23 02:31:01
【问题描述】:
我想在所有测试之间共享一个服务器,为此我创建了文件 server-environment.js
const NodeEnvironment = require('jest-environment-node')
const supertest = require('supertest')
//A koa server
const { app, init } = require('../src/server')
class ServerEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context)
this.testPath = context.testPath
}
async setup() {
await init
this.server = app.listen()
this.api = supertest(this.server)
this.global.api = this.api
}
async teardown() {
this.server.close()
}
}
module.exports = ServerEnvironment
问题是我想模拟一些服务器路由使用的中间件,但我真的找不到办法做到这一点。如果我尝试在文件中的任何位置声明 jest.mock ,则会收到未定义 jest 的错误。如果我在实际测试文件中模拟该函数,全局将不会使用它。不确定 Jest 是否可以做类似的事情?
【问题讨论】:
标签: node.js unit-testing jestjs