因为 jest 运行环境没有 chrome 对象,我使用全剧对象。
// package.json
"jest": {
"globals": {
"chrome": {
"extension": {}
}
}
}
然后使用 Object.defineProperty
Object.definePropertise
设置 chrome 中属性。
it('test load css', () => {
Object.defineProperty(chrome.extension, 'getURL', {
value: jest.fn().mockImplementation((name) => `https://${name}`),
})
loadCSS()
expect(document.head.lastChild.href).toEqual('https://gist.css/')
})
Example