如何通过使用笑话嘲笑超级模块?
伙计们!
我正在使用Jest和Supertest来testing我的节点服务器代码。
这是我的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) }) })
我在运行testing时在server.js
打印了config
,但打印信息显示jest.mock
不起作用( lib/config
path正确)。
任何人有任何想法嘲笑config.js
在这种情况下使用Supertest?
Jest似乎解决了path,所以你在./lib/config
时提供的path必须是一致的:如果server.test.js
位于server.js
旁边,它应该是./lib/config
。 如果文件位于不同的位置,则需要相应地构buildpath,例如../src/lib/config
如果server.js位于src文件夹中,并且server.test.js位于src旁边的test文件夹中
- SequelizeJS:如何在不使用原始查询的情况下在多个数据库中包含关联(join)
- 如何在Sails中使用摩卡testing控制器?
- Node + Express + Passport + Mongoose:req.user未定义
- `Node.js`和/或其他Javascript分支的相对于非JS框架的性能,稳定性和速度(Rails,Django …)
- 如何开发一个没有服务器端语言的dynamic网站?
- 使用NodeJS提供的shell
- 如何使用并行subprocess在大型数组上执行“工作”?
- 在heroku的节点上查找CERT_UNTRUSTED错误的来源
- node.js – 带有base64的http