We use Jest rather than vitest, but hopefully something similar will work.
We wrote a file which explicitly declares mocks for some of the Chrome API functions we use most often, and injects them into the global namespace:
const chrome = {
alarms: {
onAlarm: { addListener: jest.fn },
get: jest.fn(),
create: jest.fn(),
},
// ...
};
globalThis.chrome = chrome;
In our Jest configuration, that file is specified in the `setupFilesAfterEnv` property. That means it gets included automatically when running tests.
Each test or test suite which actually uses a mock has to set up its own expectations on each mock, and then the mocks typically have to be reset or cleared afterwards. It can be quite cumbersome, but it's been pretty effective so far.