Skip to content

Commit 5bc3021

Browse files
committed
WIP
1 parent 397a961 commit 5bc3021

File tree

4 files changed

+36
-60
lines changed

4 files changed

+36
-60
lines changed

src/runner/browser-env/index.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/runner/browser-env/vite/server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { plugin as mockPlugin } from "./plugins/mock";
1414
import { ManualMock } from "./manual-mock";
1515
import { Config } from "../../../config";
1616
import { VITE_DEFAULT_CONFIG_ENV } from "./constants";
17+
import defaults from "../../../config/defaults";
1718

1819
import type { ViteDevServer, UserConfig, InlineConfig } from "vite";
1920
import type { BrowserTestRunEnvOptions } from "./types";
@@ -77,6 +78,8 @@ export class ViteServer {
7778
await this._server.listen();
7879

7980
logger.log(chalk.green(`Vite server started on ${this.baseUrl}`));
81+
82+
this._useBaseUrlFromVite();
8083
}
8184

8285
async close(): Promise<void> {
@@ -115,4 +118,19 @@ export class ViteServer {
115118
get baseUrl(): string | undefined {
116119
return this._server?.resolvedUrls!.local[0];
117120
}
121+
122+
private _useBaseUrlFromVite(): void {
123+
const viteBaseUrl = this.baseUrl!;
124+
const defaultBaseUrl = defaults.baseUrl;
125+
126+
if (this._testplaneConfig.baseUrl === defaultBaseUrl) {
127+
this._testplaneConfig.baseUrl = viteBaseUrl;
128+
}
129+
130+
for (const broConfig of Object.values(this._testplaneConfig.browsers)) {
131+
if (broConfig.baseUrl === defaultBaseUrl) {
132+
broConfig.baseUrl = viteBaseUrl;
133+
}
134+
}
135+
}
118136
}

src/testplane.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import _ from "lodash";
33
import fs from "fs-extra";
44
import { Stats as RunnerStats } from "./stats";
55
import { BaseTestplane } from "./base-testplane";
6-
import type { MainRunner as NodejsEnvRunner } from "./runner";
7-
import type { MainRunner as BrowserEnvRunner } from "./runner/browser-env";
6+
import type { MainRunner } from "./runner";
87
import RuntimeConfig from "./config/runtime-config";
98
import { MasterAsyncEvents, MasterEvents, MasterSyncEvents } from "./events";
109
import eventsUtils from "./events/utils";
@@ -19,6 +18,7 @@ import { initDevServer } from "./dev-server";
1918
import { ConfigInput } from "./config/types";
2019
import { MasterEventHandler, Test, TestResult } from "./types";
2120
import { preloadWebdriverIO } from "./utils/preload-utils";
21+
import { ViteServer } from "./runner/browser-env/vite/server";
2222

2323
interface RunOpts {
2424
browsers: string[];
@@ -70,14 +70,16 @@ export interface Testplane {
7070
export class Testplane extends BaseTestplane {
7171
protected failed: boolean;
7272
protected failedList: FailedListItem[];
73-
protected runner: NodejsEnvRunner | BrowserEnvRunner | null;
73+
protected runner: MainRunner | null;
74+
protected viteServer: ViteServer | null;
7475

7576
constructor(config?: string | ConfigInput) {
7677
super(config);
7778

7879
this.failed = false;
7980
this.failedList = [];
8081
this.runner = null;
82+
this.viteServer = null;
8183
}
8284

8385
extendCli(parser: Command): void {
@@ -91,6 +93,16 @@ export class Testplane extends BaseTestplane {
9193
configPath: this._config.configPath,
9294
});
9395

96+
if (!isRunInNodeJsEnv(this._config)) {
97+
try {
98+
this.viteServer = ViteServer.create(this._config);
99+
await this.viteServer.start();
100+
RuntimeConfig.getInstance().extend({ viteBaseUrl: this.viteServer.baseUrl });
101+
} catch (err) {
102+
throw new Error(`Vite server failed to start: ${(err as Error).message}`);
103+
}
104+
}
105+
94106
return super._init();
95107
}
96108

@@ -126,9 +138,7 @@ export class Testplane extends BaseTestplane {
126138
this._config.system.mochaOpts.timeout = 0;
127139
}
128140

129-
const RunnerClass = isRunInNodeJsEnv(this._config)
130-
? await import("./runner").then(m => m.MainRunner)
131-
: await import("./runner/browser-env").then(m => m.MainRunner);
141+
const RunnerClass = await import("./runner").then(m => m.MainRunner);
132142

133143
const runner = RunnerClass.create(this._config, this._interceptors);
134144
this.runner = runner;

src/types/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Events } from "../events";
2-
import type { MainRunner as NodejsEnvRunner } from "../runner";
3-
import type { MainRunner as BrowserEnvRunner } from "../runner/browser-env/index";
2+
import type { MainRunner } from "../runner";
43
import type { TestCollection } from "../test-collection";
54
import type { Test } from "../test-reader/test-object/test";
65
import type { Suite } from "../test-reader/test-object/suite";
@@ -219,7 +218,7 @@ export interface SnapshotsData {
219218

220219
export type MasterEventHandler<T extends BaseTestplane> = {
221220
(event: Events["INIT"], callback: () => Promise<void> | void): T;
222-
(event: Events["RUNNER_START"], callback: (runner: NodejsEnvRunner | BrowserEnvRunner) => Promise<void> | void): T;
221+
(event: Events["RUNNER_START"], callback: (runner: MainRunner) => Promise<void> | void): T;
223222
(event: Events["RUNNER_END"], callback: (result: StatsResult) => Promise<void> | void): T;
224223
(event: Events["SESSION_START"], callback: AsyncSessionEventCallback): T;
225224
(event: Events["SESSION_END"], callback: AsyncSessionEventCallback): T;

0 commit comments

Comments
 (0)