Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/wxt/src/utils/__tests__/content-script-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ describe('Content Script Context', () => {
expect(isValid).toBe(false);
});

it('should not throw when browser.runtime is undefined (extension context fully invalidated)', () => {
const ctx = new ContentScriptContext('test');
const onInvalidated = vi.fn();

ctx.onInvalidated(onInvalidated);
// Simulate complete extension context invalidation where browser.runtime becomes undefined
const originalRuntime = fakeBrowser.runtime;
// @ts-ignore
fakeBrowser.runtime = undefined;

// Should not throw, and should mark as invalid
expect(() => ctx.isInvalid).not.toThrow();
expect(ctx.isInvalid).toBe(true);
expect(onInvalidated).toBeCalled();

// Restore for other tests
fakeBrowser.runtime = originalRuntime;
});

it('should invalidate the current content script when a new context is created', async () => {
const name = 'test';
const onInvalidated = vi.fn();
Expand Down
2 changes: 1 addition & 1 deletion packages/wxt/src/utils/content-script-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class ContentScriptContext implements AbortController {
}

get isInvalid(): boolean {
if (browser.runtime.id == null) {
if (browser.runtime?.id == null) {
this.notifyInvalidated(); // Sets `signal.aborted` to true
}
return this.signal.aborted;
Expand Down