Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and Add Comprehensive Test Coverage [#71] #86

Merged
merged 17 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
/docs/authenticators
/site
/authenticators
/misc/authenticators
/misc/authenticators
/coverage
11 changes: 11 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// global.d.ts
import "jest";

declare global {
var mockCreate: jest.Mock<any, any>;
var mockGet: jest.Mock<any, any>;
var mockIsUserVerifyingPlatformAuthenticatorAvailable: jest.Mock<any, any>;
var mockIsConditionalMediationAvailable: jest.Mock<any, any>;
}

export {};
23 changes: 13 additions & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
//import { TextDecoder } from 'util';

export default {
preset: 'ts-jest',
testEnvironment: 'node', // 'jsdom', //'node',
preset: "ts-jest",
testEnvironment: "node", // 'jsdom', //'node',
moduleDirectories: ["node_modules", "src"],
resolver: "jest-ts-webcompat-resolver"
/*
globals: {
crypto,
TextDecoder
}
*/
};
resolver: "jest-ts-webcompat-resolver",
setupFilesAfterEnv: ["<rootDir>/jest_config/jest.setup.js"],
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
coverageThreshold: {
global: {
lines: 90,
},
},
coveragePathIgnorePatterns: ["/node_modules/", "<rootDir>/src/authenticators.ts"],
};
51 changes: 51 additions & 0 deletions jest_config/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// jest.setup.js

if (typeof globalThis.btoa === "undefined") {
globalThis.btoa = (data) => Buffer.from(data, "binary").toString("base64");
}

if (typeof globalThis.atob === "undefined") {
globalThis.atob = (data) => Buffer.from(data, "base64").toString("binary");
}

if (typeof globalThis.crypto === "undefined") {
const { webcrypto } = require("crypto");
globalThis.crypto = webcrypto;
}

if (typeof window !== "undefined" && typeof document !== "undefined") {
if (!crypto.randomUUID) {
crypto.randomUUID = () => "mock-uuid-1234";
}

// Mock the PublicKeyCredential and its static methods:
const mockIsUserVerifyingPlatformAuthenticatorAvailable = jest.fn();
const mockIsConditionalMediationAvailable = jest.fn();

Object.defineProperty(window, "PublicKeyCredential", {
writable: true,
value: class {
static isUserVerifyingPlatformAuthenticatorAvailable =
mockIsUserVerifyingPlatformAuthenticatorAvailable;
static isConditionalMediationAvailable = mockIsConditionalMediationAvailable;
},
});

// Mock navigator.credentials.create and .get:
const mockCreate = jest.fn();
const mockGet = jest.fn();

Object.defineProperty(navigator, "credentials", {
writable: true,
value: {
create: mockCreate,
get: mockGet,
},
});

global.mockCreate = mockCreate;
global.mockGet = mockGet;
global.mockIsUserVerifyingPlatformAuthenticatorAvailable =
mockIsUserVerifyingPlatformAuthenticatorAvailable;
global.mockIsConditionalMediationAvailable = mockIsConditionalMediationAvailable;
}
Loading