next-page-tester
Version:
Enable DOM integration testing on Next.js pages
48 lines (47 loc) • 1.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanup = exports.initTestHelpers = void 0;
const makeRenderMethods_1 = require("./makeRenderMethods");
const setEnvVars_1 = require("./setEnvVars");
const utils_1 = require("./utils");
function isJSDOMEnvironment() {
return navigator && navigator.userAgent.includes('jsdom');
}
class IntersectionObserver {
observe() { }
unobserve() { }
disconnect() { }
}
function initTestHelpers() {
if (isJSDOMEnvironment()) {
// Mock IntersectionObserver (Link component relies on it)
if (!global.IntersectionObserver) {
//@ts-expect-error missing DOM types
global.IntersectionObserver = IntersectionObserver;
}
// Mock window.scrollTo (Link component triggers it)
global.scrollTo = () => { };
}
if (typeof document !== 'undefined' && typeof afterEach === 'function') {
afterEach(cleanup);
// Disable testing library auto cleanup
// https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup
process.env.RTL_SKIP_AUTO_CLEANUP = 'true';
}
// We are intentionally only targeting jest here for it to work with jest.isolatedModules
// If user has a different test runner we handle it in src/utils where we fallback to stealthy-require
if (typeof jest !== 'undefined') {
(0, utils_1.preservePredefinedSharedModulesIdentity)();
// Enable a Next.js workaround to avoid mixed commonJs/Es Module imports that Jes can't handle
// @LINK: https://github.com/vercel/next.js/blob/v12.1.0/packages/next/server/config.ts#L639
beforeEach(() => {
process.env.__NEXT_TEST_MODE = 'jest';
});
}
}
exports.initTestHelpers = initTestHelpers;
function cleanup() {
(0, makeRenderMethods_1.cleanupDOM)();
(0, setEnvVars_1.cleanupEnvVars)();
}
exports.cleanup = cleanup;