Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"yallist": "3.1.1"
},
"devDependencies": {
"@babel/parser": "7.28.5",
"@commitlint/cli": "^19.0.3",
"@commitlint/config-conventional": "^19.0.3",
"@cspotcode/source-map-support": "0.8.0",
Expand Down Expand Up @@ -179,11 +180,15 @@
"uglifyify": "3.0.4"
},
"peerDependencies": {
"@babel/parser": ">=7.0.0",
"@cspotcode/source-map-support": ">=0.7.0",
"@swc/core": ">=1.3.96",
"ts-node": ">=10.5.0"
},
"peerDependenciesMeta": {
"@babel/parser": {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use @babel/parser because I can't implement a plugin for esbuild when use transformSync in order to remove vite-style imports. For swc I can implement it in Rust, but it won't be a complete solution.

In next major we should move to use only swc parser.

"optional": true
},
"ts-node": {
"optional": true
},
Expand Down
5 changes: 3 additions & 2 deletions src/base-testplane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Interceptor,
} from "./events";
import Errors from "./errors";
import { registerTransformHook } from "./utils/typescript";
import { registerTransformHook, updateTransformHook } from "./utils/typescript";
import { ConfigInput } from "./config/types";

export abstract class BaseTestplane extends AsyncEmitter {
Expand All @@ -31,8 +31,9 @@ export abstract class BaseTestplane extends AsyncEmitter {
this._interceptors = [];

registerTransformHook(this.isWorker());

this._config = Config.create(config);
updateTransformHook(this._config);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need config in order to check if tests run in node env or in browser env


this._setLogLevel();
this._loadPlugins();
}
Expand Down
20 changes: 18 additions & 2 deletions src/test-reader/mocha-reader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@ const { TreeBuilderDecorator } = require("./tree-builder-decorator");
const { TestReaderEvents } = require("../../events");
const { MasterEvents } = require("../../events");
const { getMethodsByInterface } = require("./utils");
const logger = require("../../utils/logger");
const { enableSourceMaps } = require("../../utils/typescript");

async function readFiles(files, { esmDecorator, config, eventBus, runnableOpts }) {
async function readFiles(files, { esmDecorator, config, eventBus, runnableOpts, isBrowserEnv = false }) {
const mocha = new Mocha(config);
mocha.fullTrace();

initBuildContext(eventBus);
initEventListeners({ rootSuite: mocha.suite, outBus: eventBus, config, runnableOpts });

files.forEach(f => mocha.addFile(f));
await mocha.loadFilesAsync({ esmDecorator });

try {
await mocha.loadFilesAsync({ esmDecorator });
} catch (err) {
const errorMessage = (err.message || "").split("\n")[0].trim();

if (isBrowserEnv && err.code === "MODULE_NOT_FOUND" && errorMessage.includes("?")) {
logger.warn(
`Failed to resolve module with query parameter: ${errorMessage}. ` +
`This is likely a Vite-style import (e.g., './file.svg?react'). ` +
`Please install @babel/parser version 7 or higher to fix this issue.`,
);
}

throw err;
}

applyOnly(mocha.suite, eventBus);
}
Expand Down
5 changes: 4 additions & 1 deletion src/test-reader/test-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as logger from "../utils/logger";
import { getShortMD5 } from "../utils/crypto";
import { Test } from "./test-object";
import { Config } from "../config";
import { isRunInBrowserEnv } from "../utils/config";
import { BrowserConfig } from "../config/browser-config";
import type { ReadTestsOpts } from "../testplane";

Expand Down Expand Up @@ -79,7 +80,9 @@ export class TestParser extends EventEmitter {

const rand = Math.random();
const esmDecorator = (f: string): string => f + `?rand=${rand}`;
await readFiles(files, { esmDecorator, config: mochaOpts, eventBus, runnableOpts });
const isBrowserEnv = isRunInBrowserEnv(config);

await readFiles(files, { esmDecorator, config: mochaOpts, eventBus, runnableOpts, isBrowserEnv });

if (config.lastFailed.only) {
try {
Expand Down
9 changes: 8 additions & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { NODEJS_TEST_RUN_ENV } from "../constants/config";
import { BROWSER_TEST_RUN_ENV, NODEJS_TEST_RUN_ENV } from "../constants/config";
import type { CommonConfig } from "../config/types";

export const isRunInNodeJsEnv = (config: CommonConfig): boolean => {
return config.system.testRunEnv === NODEJS_TEST_RUN_ENV;
};

export const isRunInBrowserEnv = (config: CommonConfig): boolean => {
return (
(typeof config.system.testRunEnv === "string" && config.system.testRunEnv === BROWSER_TEST_RUN_ENV) ||
(Array.isArray(config.system.testRunEnv) && config.system.testRunEnv[0] === BROWSER_TEST_RUN_ENV)
);
};
Loading
Loading