@pokujs/dom 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ import type { RuntimeOptions } from './types.ts';
2
+ type SetupDomEnvironmentOptions = {
3
+ runtimeOptions: RuntimeOptions;
4
+ packageTag: string;
5
+ enableReactActEnvironment?: boolean;
6
+ };
7
+ export declare const setupHappyDomEnvironment: (options: SetupDomEnvironmentOptions) => Promise<void>;
8
+ export declare const setupJsdomEnvironment: (options: SetupDomEnvironmentOptions) => Promise<void>;
9
+ export {};
@@ -0,0 +1,81 @@
1
+ import { GlobalWindow } from 'happy-dom';
2
+ const applyReactActEnvironment = (enabled) => {
3
+ if (!enabled)
4
+ return;
5
+ const reactGlobal = globalThis;
6
+ if (typeof reactGlobal.IS_REACT_ACT_ENVIRONMENT === 'undefined') {
7
+ reactGlobal.IS_REACT_ACT_ENVIRONMENT = true;
8
+ }
9
+ };
10
+ const defineGlobal = (key, value) => {
11
+ Object.defineProperty(globalThis, key, {
12
+ configurable: true,
13
+ writable: true,
14
+ value,
15
+ });
16
+ };
17
+ export const setupHappyDomEnvironment = async (options) => {
18
+ if (!globalThis.window || !globalThis.document) {
19
+ // Save native event primitives before installing Happy DOM globals.
20
+ // Deno's runtime calls globalThis.dispatchEvent('beforeunload') on exit
21
+ // using its own native Event constructor — if Happy DOM's dispatchEvent
22
+ // is installed instead, the type check inside it throws a TypeError.
23
+ const existingDispatchEvent = globalThis.dispatchEvent;
24
+ const existingEvent = globalThis.Event;
25
+ const existingCustomEvent = globalThis.CustomEvent;
26
+ const happyWindow = new GlobalWindow({ url: options.runtimeOptions.domUrl });
27
+ defineGlobal('window', happyWindow);
28
+ defineGlobal('document', happyWindow.document);
29
+ defineGlobal('navigator', happyWindow.navigator);
30
+ defineGlobal('HTMLElement', happyWindow.HTMLElement);
31
+ defineGlobal('Element', happyWindow.Element);
32
+ defineGlobal('Node', happyWindow.Node);
33
+ defineGlobal('Text', happyWindow.Text);
34
+ defineGlobal('SVGElement', happyWindow.SVGElement);
35
+ defineGlobal('MutationObserver', happyWindow.MutationObserver);
36
+ defineGlobal('requestAnimationFrame', happyWindow.requestAnimationFrame);
37
+ defineGlobal('cancelAnimationFrame', happyWindow.cancelAnimationFrame);
38
+ // Prefer the runtime's native Event constructors so that Deno's internal
39
+ // event dispatch (e.g. beforeunload) continues to work correctly.
40
+ defineGlobal('Event', typeof existingEvent === 'function' ? existingEvent : happyWindow.Event);
41
+ defineGlobal('CustomEvent', typeof existingCustomEvent === 'function' ? existingCustomEvent : happyWindow.CustomEvent);
42
+ if (typeof existingDispatchEvent === 'function') {
43
+ globalThis.dispatchEvent = existingDispatchEvent;
44
+ }
45
+ else {
46
+ globalThis.dispatchEvent = happyWindow.dispatchEvent.bind(happyWindow);
47
+ }
48
+ }
49
+ applyReactActEnvironment(Boolean(options.enableReactActEnvironment));
50
+ };
51
+ export const setupJsdomEnvironment = async (options) => {
52
+ if (!globalThis.window || !globalThis.document) {
53
+ let mod;
54
+ try {
55
+ mod = await import('jsdom');
56
+ }
57
+ catch {
58
+ throw new Error(`[${options.packageTag}] DOM adapter "jsdom" requires the "jsdom" package. Install it with "npm install --save-dev jsdom".`);
59
+ }
60
+ const { JSDOM } = mod;
61
+ const dom = new JSDOM('', {
62
+ url: options.runtimeOptions.domUrl,
63
+ pretendToBeVisual: true,
64
+ });
65
+ defineGlobal('window', dom.window);
66
+ defineGlobal('document', dom.window.document);
67
+ defineGlobal('navigator', dom.window.navigator);
68
+ defineGlobal('HTMLElement', dom.window.HTMLElement);
69
+ defineGlobal('Element', dom.window.Element);
70
+ defineGlobal('Node', dom.window.Node);
71
+ defineGlobal('Text', dom.window.Text);
72
+ defineGlobal('SVGElement', dom.window.SVGElement);
73
+ defineGlobal('Event', dom.window.Event);
74
+ defineGlobal('CustomEvent', dom.window.CustomEvent);
75
+ defineGlobal('MutationObserver', dom.window.MutationObserver);
76
+ defineGlobal('requestAnimationFrame', dom.window.requestAnimationFrame);
77
+ defineGlobal('cancelAnimationFrame', dom.window.cancelAnimationFrame);
78
+ }
79
+ applyReactActEnvironment(Boolean(options.enableReactActEnvironment));
80
+ };
81
+ //# sourceMappingURL=dom-env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom-env.js","sourceRoot":"","sources":["../src/dom-env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAazC,MAAM,wBAAwB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACpD,IAAI,CAAC,OAAO;QAAE,OAAO;IACrB,MAAM,WAAW,GAAG,UAA4B,CAAC;IACjD,IAAI,OAAO,WAAW,CAAC,wBAAwB,KAAK,WAAW,EAAE,CAAC;QAChE,WAAW,CAAC,wBAAwB,GAAG,IAAI,CAAC;IAC9C,CAAC;AACH,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,GAA4B,EAAE,KAAc,EAAE,EAAE;IACpE,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,GAAG,EAAE;QACrC,YAAY,EAAE,IAAI;QAClB,QAAQ,EAAE,IAAI;QACd,KAAK;KACN,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,EAC3C,OAAmC,EACnC,EAAE;IACF,IAAI,CAAC,UAAU,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;QAC/C,oEAAoE;QACpE,wEAAwE;QACxE,wEAAwE;QACxE,qEAAqE;QACrE,MAAM,qBAAqB,GAAG,UAAU,CAAC,aAAa,CAAC;QACvD,MAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC;QACvC,MAAM,mBAAmB,GAAG,UAAU,CAAC,WAAW,CAAC;QAEnD,MAAM,WAAW,GAAG,IAAI,YAAY,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,cAAc,CAAC,MAAM,EAAE,CAAC,CAAC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CAAC,CAAC;QACxC,YAAY,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QACpD,YAAY,CAAC,kBAAkB,EAAE,GAAG,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;QAC9D,YAAY,CAAC,uBAAuB,EAAE,GAAG,CAAC,MAAM,CAAC,qBAAqB,CAAC,CAAC;QACxE,YAAY,CAAC,sBAAsB,EAAE,GAAG,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IACxE,CAAC;IAED,wBAAwB,CAAC,OAAO,CAAC,OAAO,CAAC,yBAAyB,CAAC,CAAC,CAAC;AACvE,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,149 +1,8 @@
1
- import { Screen } from '@testing-library/dom';
2
-
3
- type DomAdapter = 'happy-dom' | 'jsdom' | {
4
- setupModule: string;
5
- };
6
- type RenderMetric = {
7
- file: string;
8
- componentName: string;
9
- durationMs: number;
10
- };
11
- type MetricsSummary = {
12
- totalCaptured: number;
13
- totalReported: number;
14
- topSlowest: RenderMetric[];
15
- };
16
- type MetricsOptions = {
17
- enabled?: boolean;
18
- topN?: number;
19
- minDurationMs?: number;
20
- reporter?: (summary: MetricsSummary) => void;
21
- };
22
- type TestingPluginOptions = {
23
- dom?: DomAdapter;
24
- domUrl?: string;
25
- metrics?: boolean | MetricsOptions;
26
- };
27
- type NormalizedMetricsOptions = {
28
- enabled: boolean;
29
- topN: number;
30
- minDurationMs: number;
31
- reporter?: (summary: MetricsSummary) => void;
32
- };
33
- type RuntimeOptionArgPrefixes = {
34
- metrics: string;
35
- minMetricMs: string;
36
- domUrl: string;
37
- metricBatchSize: string;
38
- metricFlushMs: string;
39
- };
40
- type RuntimeOptions = {
41
- domUrl: string;
42
- metricsEnabled: boolean;
43
- minMetricMs: number;
44
- metricBatchSize: number;
45
- metricFlushMs: number;
46
- };
47
- type FrameworkDescriptor = {
48
- pluginName: string;
49
- packageTag: string;
50
- runtimeArgBase: string;
51
- metricMessageType: string;
52
- metricBatchMessageType: string;
53
- testFileExtensions?: string[];
54
- };
55
-
56
- declare const createRuntimeOptionArgPrefixes: (base: string) => RuntimeOptionArgPrefixes;
57
- declare const parseRuntimeOptions: (prefixes: RuntimeOptionArgPrefixes, argv?: string[]) => RuntimeOptions;
58
-
59
- type RuntimeSupport = {
60
- supportsNodeLikeImport: boolean;
61
- supportsDenoPreload: boolean;
62
- };
63
- type BuildRunnerCommandInput = {
64
- runtime: string;
65
- command: string[];
66
- file: string;
67
- domSetupPath: string;
68
- runtimeOptionArgs: string[];
69
- extensions: Set<string>;
70
- };
71
- type BuildRunnerCommandOutput = {
72
- shouldHandle: boolean;
73
- command: string[];
74
- };
75
- declare const isNodeRuntime: (runtime: string) => runtime is "node";
76
- declare const getRuntimeSupport: (runtime: string) => RuntimeSupport;
77
- declare const canHandleRuntime: (runtime: string) => boolean;
78
- declare const createDomSetupPathResolver: (packageTag: string, happyDomSetupPath: string, jsdomSetupPath: string) => (adapter: DomAdapter | undefined) => string;
79
- declare const buildRunnerCommand: ({ runtime, command, file, domSetupPath, runtimeOptionArgs, extensions, }: BuildRunnerCommandInput) => BuildRunnerCommandOutput;
80
-
81
- type InProcessSetupOptions = {
82
- isolation: string | undefined;
83
- runtime: string;
84
- runtimeOptionArgs: string[];
85
- domSetupPath: string;
86
- packageTag: string;
87
- };
88
- declare const setupInProcessEnvironment: (options: InProcessSetupOptions) => Promise<(() => void) | undefined>;
89
-
90
- declare const isRenderMetricMessage: (message: unknown, metricMessageType: string) => message is {
91
- type: string;
92
- componentName?: string;
93
- durationMs?: number;
94
- };
95
- declare const isRenderMetricBatchMessage: (message: unknown, metricBatchMessageType: string) => message is {
96
- type: string;
97
- metrics: Array<{
98
- componentName?: string;
99
- durationMs?: number;
100
- }>;
101
- };
102
- declare const getComponentName: (componentName: unknown) => string;
103
- declare const buildRuntimeOptionArgs: (options: TestingPluginOptions, metricsOptions: NormalizedMetricsOptions, prefixes: RuntimeOptionArgPrefixes) => string[];
104
- declare const normalizeMetricsOptions: (metrics: boolean | MetricsOptions | undefined) => NormalizedMetricsOptions;
105
- declare const selectTopSlowestMetrics: (metrics: RenderMetric[], options: NormalizedMetricsOptions) => RenderMetric[];
106
- declare const createMetricsSummary: (metrics: RenderMetric[], options: NormalizedMetricsOptions) => MetricsSummary | null;
107
- declare const printMetricsSummary: (summary: MetricsSummary, packageTag: string) => void;
108
-
109
- type FrameworkPluginInternals = {
110
- buildRunnerCommand: typeof buildRunnerCommand;
111
- canHandleRuntime: typeof canHandleRuntime;
112
- buildRuntimeOptionArgs: typeof buildRuntimeOptionArgs;
113
- normalizeMetricsOptions: typeof normalizeMetricsOptions;
114
- selectTopSlowestMetrics: typeof selectTopSlowestMetrics;
115
- createMetricsSummary: typeof createMetricsSummary;
116
- getComponentName: typeof getComponentName;
117
- isRenderMetricMessage: typeof isRenderMetricMessage;
118
- isRenderMetricBatchMessage: typeof isRenderMetricBatchMessage;
119
- resolveDomSetupPath: (adapter: DomAdapter | undefined) => string;
120
- };
121
- declare const createFrameworkTestingPluginFactory: (descriptor: FrameworkDescriptor, moduleUrl: string) => {
122
- createTestingPlugin: (options?: TestingPluginOptions) => any;
123
- resolveDomSetupPath: (adapter: DomAdapter | undefined) => string;
124
- runtimeOptionArgPrefixes: RuntimeOptionArgPrefixes;
125
- internals: FrameworkPluginInternals;
126
- };
127
-
128
- type SetupDomEnvironmentOptions = {
129
- runtimeOptions: RuntimeOptions;
130
- packageTag: string;
131
- enableReactActEnvironment?: boolean;
132
- };
133
- declare const setupHappyDomEnvironment: (options: SetupDomEnvironmentOptions) => Promise<void>;
134
- declare const setupJsdomEnvironment: (options: SetupDomEnvironmentOptions) => Promise<void>;
135
-
136
- declare const getNow: () => number;
137
- type RenderMetricsEmitterOptions = {
138
- runtimeOptions: RuntimeOptions;
139
- metricsStateKey: symbol;
140
- metricsBatchMessageType: string;
141
- };
142
- declare const createRenderMetricsEmitter: (options: RenderMetricsEmitterOptions) => {
143
- emitRenderMetric: (componentName: string, durationMs: number) => void;
144
- flushMetricBuffer: () => void;
145
- clearMetricFlushTimer: () => void;
146
- };
147
- declare const createScreen: () => Screen;
148
-
149
- export { type BuildRunnerCommandInput, type BuildRunnerCommandOutput, type DomAdapter, type FrameworkDescriptor, type FrameworkPluginInternals, type InProcessSetupOptions, type MetricsOptions, type MetricsSummary, type NormalizedMetricsOptions, type RenderMetric, type RenderMetricsEmitterOptions, type RuntimeOptionArgPrefixes, type RuntimeOptions, type RuntimeSupport, type TestingPluginOptions, buildRunnerCommand, buildRuntimeOptionArgs, canHandleRuntime, createDomSetupPathResolver, createFrameworkTestingPluginFactory, createMetricsSummary, createRenderMetricsEmitter, createRuntimeOptionArgPrefixes, createScreen, getComponentName, getNow, getRuntimeSupport, isNodeRuntime, isRenderMetricBatchMessage, isRenderMetricMessage, normalizeMetricsOptions, parseRuntimeOptions, printMetricsSummary, selectTopSlowestMetrics, setupHappyDomEnvironment, setupInProcessEnvironment, setupJsdomEnvironment };
1
+ export * from './types.ts';
2
+ export * from './runtime-options.ts';
3
+ export * from './plugin-command.ts';
4
+ export * from './plugin-setup.ts';
5
+ export * from './plugin-metrics.ts';
6
+ export * from './plugin-factory.ts';
7
+ export * from './dom-env.ts';
8
+ export * from './testing-core.ts';