【发布时间】:2022-01-05 00:02:28
【问题描述】:
我无法从我拥有的这个模块模式设置 (MyConnection.js) 中进行独特的搜索实现 - 这与我所拥有的类似:
// MyConnection.js
const MyConnection = () => {
const search = async (q) => {
//...some functionality
};
return {
search,
};
};
//JEST TESTS
const MyConnection = require('../MyConnection')
// this works - but it sets the search implementation
// for the whole test file I believe
jest.mock('../MyConnection', () => {
return jest.fn(()=>{
search: ()=> ['mocked', 'fn', 'results']
})
})
//the jest tests - I want different
// implementations of search in each test
describe('connection tests', ()=>{
it('test one', ()=>{
//Not sure if its something like this to set 'search' for each test? This doesn't work as is
MyConnection.search.mockImplementation((q)=>{`You searched ${q}`})
})
it('test two', ()=>{
MyConnection.search.mockImplementation((q)={q.length>32? 'a': 'b' }})
})
})
如何为每个测试获得该搜索功能的唯一 Jest 模拟实现?
【问题讨论】:
标签: javascript unit-testing jestjs