From ab04d0e332174d1df80af0446c955dc1abb56c6c Mon Sep 17 00:00:00 2001 From: Pascal Sthamer Date: Mon, 25 May 2020 19:22:46 +0200 Subject: Add test for window being undefined --- packages/browser/src/helpers/supportsWebauthn.test.ts | 15 ++++++++++++++- packages/browser/src/setupTests.ts | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'packages/browser/src') diff --git a/packages/browser/src/helpers/supportsWebauthn.test.ts b/packages/browser/src/helpers/supportsWebauthn.test.ts index 078c27d..f6b8a8e 100644 --- a/packages/browser/src/helpers/supportsWebauthn.test.ts +++ b/packages/browser/src/helpers/supportsWebauthn.test.ts @@ -1,4 +1,4 @@ -import supportsWebauthn from './supportsWebauthn'; +import supportsWebauthn from './supportsWebauthn' beforeEach(() => { // @ts-ignore 2741 @@ -13,3 +13,16 @@ test('should return false when browser does not support WebAuthn', () => { delete window.PublicKeyCredential; expect(supportsWebauthn()).toBe(false); }); + +test('should return false when window is undefined', () => { + // Make window undefined as it is in node environments. + // @ts-expect-error + const windowSpy = jest.spyOn(global, "window", "get"); + windowSpy.mockImplementation(() => undefined); + + expect(window).toBe(undefined) + expect(supportsWebauthn()).toBe(false); + + // Restore original window value. + windowSpy.mockRestore() +}) diff --git a/packages/browser/src/setupTests.ts b/packages/browser/src/setupTests.ts index 019ba42..75833a8 100644 --- a/packages/browser/src/setupTests.ts +++ b/packages/browser/src/setupTests.ts @@ -3,6 +3,9 @@ // jest.spyOn(console, 'debug').mockImplementation(); // jest.spyOn(console, 'error').mockImplementation(); +// @ts-expect-error +if (global.window) { + /** * JSDom doesn't seem to support `credentials`, so let's define them here so we can mock their * implementations in specific tests. @@ -14,3 +17,4 @@ window.navigator.credentials = { // assertion get: jest.fn(), }; +} \ No newline at end of file -- cgit v1.2.3