Skip to content

Commit

Permalink
fix: improve test lib detection
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder committed Jan 16, 2025
1 parent c720914 commit 1858e43
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,33 @@ type Item = {

const observers = new Map<IntersectionObserver, Item>();

// Store a reference to the original `IntersectionObserver` so we can restore it later.
// This can be relevant if testing in a browser environment, where you actually have a native `IntersectionObserver`.
const originalIntersectionObserver =
typeof window !== "undefined" ? window.IntersectionObserver : undefined;

/**
* Get the test utility object, depending on the environment. This could be either `vi` (Vitest) or `jest`.
* Type is mapped to Vitest, so we don't mix in Jest types when running in Vitest.
*/
function testLibraryUtil(): typeof vi | undefined {
if (typeof vi !== "undefined") return vi;
// @ts-expect-error We don't include the Jest types
if (typeof jest !== "undefined") return jest;
return undefined;
}

/**
* Check if the IntersectionObserver is currently being mocked.
* @return boolean
*/
const isMocking = () => {
// @ts-ignore
if (typeof jest !== "undefined") {
// @ts-ignore
return jest.isMockFunction(window.IntersectionObserver);
}
if (typeof vi !== "undefined") {
return vi.isMockFunction(window.IntersectionObserver);
function isMocking() {
const util = testLibraryUtil();
if (util && typeof util.isMockFunction === "function") {
return util.isMockFunction(window.IntersectionObserver);
}
};

// Store a reference to the original `IntersectionObserver` so we can restore it later.
// This can be relevant if testing in a browser environment, where you actually have a native `IntersectionObserver`.
const originalIntersectionObserver =
typeof window !== "undefined" ? window.IntersectionObserver : undefined;
return false;
}

/*
** If we are running in a valid testing environment, we can automate mocking the IntersectionObserver.
Expand All @@ -38,11 +46,9 @@ if (
typeof afterEach !== "undefined"
) {
beforeEach(() => {
// Use the exposed mock function. Currently, it supports Jest (`jest.fn`) and Vitest with globals (`vi.fn`).
// @ts-ignore
if (typeof jest !== "undefined") setupIntersectionMocking(jest.fn);
else if (typeof vi !== "undefined") {
setupIntersectionMocking(vi.fn);
const util = testLibraryUtil();
if (util) {
setupIntersectionMocking(util.fn);
}
// Ensure there's no observers from previous tests
observers.clear();
Expand Down

0 comments on commit 1858e43

Please sign in to comment.