Skip to content

Commit b820a43

Browse files
9aoyCopilot
andauthored
chore: check dependency installed (#543)
Co-authored-by: Copilot <[email protected]>
1 parent f1ce8ff commit b820a43

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/core/src/runtime/util.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createRequire } from 'node:module';
12
import { format } from 'node:util';
23
import { diff } from 'jest-diff';
34
import type { FormattedError, Test } from '../types';
@@ -108,3 +109,23 @@ function getValue(source: any, path: string, defaultValue = undefined): any {
108109
}
109110

110111
export class TestRegisterError extends Error {}
112+
113+
export class RstestError extends Error {
114+
public fullStack?: boolean;
115+
}
116+
117+
export function checkPkgInstalled(name: string): void {
118+
const require = createRequire(import.meta.url);
119+
try {
120+
require.resolve(name);
121+
} catch (error: any) {
122+
if (error.code === 'MODULE_NOT_FOUND') {
123+
const error = new RstestError(
124+
`Missing dependency "${name}". Please install it first.`,
125+
);
126+
error.fullStack = true;
127+
throw error;
128+
}
129+
throw error;
130+
}
131+
}

packages/core/src/runtime/worker/env/happyDom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Window as HappyDOMWindow } from 'happy-dom';
22
import type { TestEnvironment } from '../../../types';
3+
import { checkPkgInstalled } from '../../util';
34
import { addDefaultErrorHandler, installGlobal } from './utils';
45

56
type HappyDOMOptions = ConstructorParameters<typeof HappyDOMWindow>[0];
@@ -9,8 +10,9 @@ export const environment = <
910
>{
1011
name: 'happy-dom',
1112
async setup(global, { happyDom = {} }) {
12-
const { Window } = await import('happy-dom');
13+
checkPkgInstalled('happy-dom');
1314

15+
const { Window } = await import('happy-dom');
1416
const win = new Window({
1517
...happyDom,
1618
url: happyDom.url || 'http://localhost:3000',

packages/core/src/runtime/worker/env/jsdom.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ConstructorOptions } from 'jsdom';
22
import type { TestEnvironment } from '../../../types';
3+
import { checkPkgInstalled } from '../../util';
34
import { addDefaultErrorHandler, installGlobal } from './utils';
45

56
type JSDOMOptions = ConstructorOptions & {
@@ -10,6 +11,8 @@ type JSDOMOptions = ConstructorOptions & {
1011
export const environment = <TestEnvironment<typeof globalThis>>{
1112
name: 'jsdom',
1213
async setup(global, { jsdom = {} }) {
14+
checkPkgInstalled('jsdom');
15+
1316
const { CookieJar, JSDOM, ResourceLoader, VirtualConsole } = await import(
1417
'jsdom'
1518
);

0 commit comments

Comments
 (0)