Skip to content

Commit 23e966a

Browse files
committed
add tests
1 parent e200dff commit 23e966a

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

packages/browser/test/unit/sdk.test.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,16 @@ describe('init', () => {
135135
new MockIntegration('MockIntegration 0.2'),
136136
];
137137

138+
const originalLocation = WINDOW.location || {};
139+
138140
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS });
139141

140142
afterEach(() => {
141143
Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true });
142144
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true });
143145
});
144146

145-
it('should log a browser extension error if executed inside a Chrome extension', () => {
147+
it('logs a browser extension error if executed inside a Chrome extension', () => {
146148
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
147149

148150
Object.defineProperty(WINDOW, 'chrome', {
@@ -160,7 +162,7 @@ describe('init', () => {
160162
consoleErrorSpy.mockRestore();
161163
});
162164

163-
it('should log a browser extension error if executed inside a Firefox/Safari extension', () => {
165+
it('logs a browser extension error if executed inside a Firefox/Safari extension', () => {
164166
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
165167

166168
Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true });
@@ -175,7 +177,30 @@ describe('init', () => {
175177
consoleErrorSpy.mockRestore();
176178
});
177179

178-
it('should not log a browser extension error if executed inside regular browser environment', () => {
180+
it.each(['chrome-extension', 'moz-extension', 'ms-browser-extension'])(
181+
"doesn't log a browser extension error if executed inside an extension running in a dedicated page (%s)",
182+
extensionProtocol => {
183+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
184+
185+
// @ts-expect-error - this is a hack to simulate a dedicated page in a browser extension
186+
delete WINDOW.location;
187+
// @ts-expect-error - this is a hack to simulate a dedicated page in a browser extension
188+
WINDOW.location = {
189+
href: `${extensionProtocol}://mock-extension-id/dedicated-page.html`,
190+
};
191+
192+
Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true });
193+
194+
init(options);
195+
196+
expect(consoleErrorSpy).toBeCalledTimes(0);
197+
198+
consoleErrorSpy.mockRestore();
199+
WINDOW.location = originalLocation;
200+
},
201+
);
202+
203+
it("doesn't log a browser extension error if executed inside regular browser environment", () => {
179204
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
180205

181206
init(options);

0 commit comments

Comments
 (0)