我希望在所有测试之间共享一个服务器,为此,我创建了文件服务器-environment.js. do
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有关系?
发布于 2021-10-22 18:31:01
我有一个同样的问题,并通过添加setupFilesAfterEnv来解决它。
jest.config.js:
module.exports = {
...
setupFilesAfterEnv: [
'./test/setupMocks.js'
]
...
};测试/setupMocks.js
jest.mock('path/to/api', () => global.api);https://stackoverflow.com/questions/68287660
复制相似问题