When possible, events are triggered based on other calls to other browser APIs. For example:
fakeBrowser.runtime.sendMessage() will trigger the fakeBrowser.runtime.onMessage listenersfakeBrowser.tabs.create() will trigger the fakeBrowser.tabs.onCreated listenersSome events, like runtime.onInstalled or alarms.onAlarm, can't be triggered as they would be in a real extension.
onInstalled, when is an extension "installed" during tests? Never? Or when the tests start? Either way, not useful for testing.onAlarm, alarms are meant to trigger in the far future, usually a much longer timespan than the duration of a unit test. Also, timers in tests are notoriously flakey and difficult to work with.Instead, the fakeBrowser provides a trigger method on every implemented event that you can call to trigger them manually. Pass in the arguments that the listeners are called with:
await fakeBrowser.runtime.onInstalled.trigger({ reason: 'install' });
await fakeBrowser.alarms.onAlarm.trigger({
name: 'alarm-name',
periodInMinutes: 5,
scheduledTime: Date.now(),
});
await fakeBrowser.tab.onCreated.trigger({ ... });
trigger, it will wait for all the listener to finish running.