【发布时间】:2017-08-28 08:57:42
【问题描述】:
伙计们!
我正在使用 Jest 和 Supertest 来测试我的节点服务器代码。
这是我的server.js
// server.js
const config = require('./lib/config')
...
const app = new koa()
...
module.exports = app
我想在使用 Supertest 时模拟 config.js,这是我的 server.test.js
// server.test.js
const supertest = require('supertest-as-promised')
describe('xxxxxx', ()=>{
let app,server
beforeEach(()=>{
jest.mock('lib/config',()=>({
uri: '/path',
apiPrefix: '/prefix'
}))
app = require('server')
})
afterEach(()=>{
server && server.close()
app=null
server=null
})
it('should success', async ()=>{
server || (server = app.listen(0))
const request = supertest(server)
request().get('path/prefix_home').expect(200)
})
})
我在运行测试时在server.js中打印了config,但是打印信息显示jest.mock不起作用(lib/config的路径是正确的)。
任何人都知道在这种情况下使用 Supertest 来模拟 config.js 吗?
【问题讨论】:
-
在
server.js中,您需要./lib/config,在您的测试中需要lib/config,这不是严格相等的字符串。问题可能在这里
标签: javascript node.js unit-testing