如果在一个项目中,有一个js,并没有使用export导出函数,那 Jest 该怎么写测试文件?又或者只导出了函数 a,但是函数 b 没有导出,那我不想导出 b 给外部,那怎么写测试函数 b 的测试文件?
// test.js function test(a,b){ return a + b } (function(){ test(1,2) })()
// test.test.js ???
听起来 rewire 应该符合你的需求
https://github.com/jhnns/rewire
// test.test.js const rewire = require('rewire') const testjs = rewire('test.js') const testFunction = testjs.__get__('test') // 这是你要测试的函数
1.4m articles
1.4m replys
5 comments
56.7k users