lightsoutAlpha

import-time-side-effects

a module that acts at import time tested without a fresh instance or its own environment

Agent checkadvisory by defaultbasetests

The argument

Import-Time Side Effects

  • Use jest.isolateModules when the module acts at import time (reads document.currentScript, checks globals): each call gets a fresh module instance, so per-test state changes take effect on the next require inside the isolate block.
  • Branches unreachable in the default jsdom environment (e.g., SSR guards on typeof window) get a separate test file with a /** @jest-environment node */ docblock, named to distinguish it (autoInitInBrowser.ssr.unit.test.ts).

The proof

failsrc/boot/autoInit.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';
import { isInitialized } from './autoInit';

describe('autoInit', () => {
	test('reads the current script the module was loaded from', () => {
		// the module already ran at import time, so this change arrives too late
		// and every later test shares the one instance
		jest.replaceProperty(document, 'currentScript', document.createElement('script'));

		expect(isInitialized).toBe(true);
	});
});
passsrc/boot/autoInit.unit.test.ts
import { expect, describe, test, jest } from '@jest/globals';

const setupBoot = ({ hasScript = true }: { hasScript?: boolean } = {}) => {
	jest.replaceProperty(document, 'currentScript', hasScript ? document.createElement('script') : null);

	return { hasScript };
};

describe('autoInit', () => {
	test('reads the current script the module was loaded from', () => {
		setupBoot({ hasScript: true });

		let loaded: { isInitialized: boolean } | undefined;
		jest.isolateModules(() => {
			loaded = require('./autoInit') as { isInitialized: boolean };
		});

		expect(loaded?.isInitialized).toBe(true);
	});
});

Turn it down

Both lines go in your lightsout.config.json.

"standards-checks": { "import-time-side-effects": "advisory" }
"standards-checks": { "import-time-side-effects": "off" }