我正在尝试根据tti是null还是已定义的调用sendLogs时测试分支,Chrome浏览器tti polyfill用于测量应用程序性能,仅在Chrome中有效,在其他浏览器中将解析为null。
App.vue
import ttiPolyfill from 'tti-polyfill'
export default {
name: 'App',
mounted () {
ttiPolyfill.getFirstConsistentlyInteractive().then(tti => {
if (tti) {
sendLogs(tti)
}
})
}
}
我不确定如何将单元测试之间的getFirstConsistentlyInteractive返回的值重置为当前
在App spec.js中,我有这样的东西(简化)
const createWrapper = ({ getters, state, methods } = {}) => {
return shallowMount(App, {
mocks,
localVue,
sync: false,
store: new Vuex.Store({
actions,
state: merge({}, baseState, state),
getters: merge({}, defaultGetters, getters)
}),
methods
})
}
beforeEach(() => {
wrapper = createWrapper()
})
describe('tti Polyfill', () => {
it('should send metrics when tti is defined', async () => {
await expect(sendLogs).toHaveBeenCalled()
})
it('should NOT send metrics when tti is null', async () => {
/// How to test this?
await expect(sendLogs).not.toHaveBeenCalled()
})
})
和__模拟__
tti-polyfill.js
export default {
getFirstConsistentlyInteractive: () => Promise.resolve(123)
}
如果要测试两种情况,我不确定是否可以使用自动模拟,但是不确定替代方法是什么