Skip to content

Commit

Permalink
fix: correctly connect to vite server from clients (worker, browser)
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Apr 17, 2024
1 parent ee7e71b commit 9833520
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/runner/browser-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ViteServer } from "./vite/server";
import { MainRunner as NodejsEnvRunner } from "..";
import { TestCollection } from "../../test-collection";
import { Config } from "../../config";
import RuntimeConfig from "../../config/runtime-config";
import { Interceptor } from "../../events";
import type { Stats as RunnerStats } from "../../stats";

Expand All @@ -17,6 +18,7 @@ export class MainRunner extends NodejsEnvRunner {
async run(testCollection: TestCollection, stats: RunnerStats): Promise<void> {
try {
await this._viteServer.start();
RuntimeConfig.getInstance().extend({ viteBaseUrl: this._viteServer.baseUrl });
} catch (err) {
throw new Error(`Vite server failed to start: ${(err as Error).message}`);
}
Expand Down
1 change: 0 additions & 1 deletion src/runner/browser-env/vite/browser-modules/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const connectToSocket = (): BrowserViteSocket => {
runUuid: window.__testplane__.runUuid,
type: BROWSER_EVENT_PREFIX,
},
transports: ["websocket"],
}) as BrowserViteSocket;

socket.on("connect_error", err => {
Expand Down
3 changes: 2 additions & 1 deletion src/worker/browser-env/runner/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { wrapExecutionThread } from "./execution-thread";
import { WorkerEventNames } from "./types";
import { WORKER_EVENT_PREFIX, SUPPORTED_ASYMMETRIC_MATCHER } from "./constants";
import logger from "../../../../utils/logger";
import RuntimeConfig from "../../../../config/runtime-config";

import { BrowserEventNames } from "../../../../runner/browser-env/vite/types";
import type { Selector } from "webdriverio";
Expand Down Expand Up @@ -37,7 +38,7 @@ export class TestRunner extends NodejsEnvTestRunner {
constructor(opts: WorkerTestRunnerCtorOpts) {
super(opts);

this._socket = io(this._config.baseUrl, {
this._socket = io(RuntimeConfig.getInstance().viteBaseUrl, {
transports: ["websocket"],
auth: {
runUuid: this._runUuid,
Expand Down
14 changes: 14 additions & 0 deletions test/src/runner/browser-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sinon, { SinonStub } from "sinon";
import { MainRunner as BrowserEnvRunner } from "../../../../src/runner/browser-env";
import { MainRunner as NodejsEnvRunner } from "../../../../src/runner";
import { ViteServer } from "../../../../src/runner/browser-env/vite/server";
import RuntimeConfig from "../../../../src/config/runtime-config";
import { TestCollection } from "../../../../src/test-collection";
import { Stats as RunnerStats } from "../../../../src/stats";

Expand All @@ -28,6 +29,8 @@ describe("BrowserEnvRunner", () => {
sandbox.stub(NodejsEnvRunner.prototype, "run").resolves();
sandbox.stub(NodejsEnvRunner.prototype, "cancel");

sandbox.stub(RuntimeConfig, "getInstance").returns({ extend: sandbox.stub() });

sandbox.stub(ViteServer, "create").returns(Object.create(ViteServer.prototype));
sandbox.stub(ViteServer.prototype, "start").resolves();
sandbox.stub(ViteServer.prototype, "close").resolves();
Expand All @@ -53,6 +56,17 @@ describe("BrowserEnvRunner", () => {
assert.calledOnceWith(ViteServer.prototype.start);
});

it("should save vite base url to runtime config", async () => {
const viteBaseUrl = "http://localhost:4000";
sandbox.stub(ViteServer.prototype, "baseUrl").get(() => viteBaseUrl);

await run_();

assert.calledWith((RuntimeConfig.getInstance as SinonStub).lastCall.returnValue.extend, {
viteBaseUrl,
});
});

it("should throw error if vite server failed", async () => {
(ViteServer.prototype.start as SinonStub).rejects(new Error("o.O"));

Expand Down
13 changes: 8 additions & 5 deletions test/src/worker/browser-env/runner/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import BrowserAgent from "../../../../../../src/worker/runner/browser-agent";
import history from "../../../../../../src/browser/history";
import logger from "../../../../../../src/utils/logger";
import OneTimeScreenshooter from "../../../../../../src/worker/runner/test-runner/one-time-screenshooter";
import RuntimeConfig from "../../../../../../src/config/runtime-config";

import ExpectWebdriverIO from "expect-webdriverio";
import { BrowserEventNames } from "../../../../../../src/runner/browser-env/vite/types";
Expand Down Expand Up @@ -165,6 +166,8 @@ describe("worker/browser-env/runner/test-runner", () => {
sandbox.stub(process, "pid").value(11111);
sandbox.stub(logger, "warn");

sandbox.stub(RuntimeConfig, "getInstance").returns({ viteBaseUrl: "http://default" });

socketClientStub = sandbox.stub().returns(mkSocket_());
wrapExecutionThreadStub = sandbox.stub().callsFake(socket => wrapExecutionThread(socket));

Expand All @@ -175,13 +178,13 @@ describe("worker/browser-env/runner/test-runner", () => {

describe("constructor", () => {
describe("socket", () => {
it("should connect to the baseUrl", () => {
const baseUrl = "http://localhost:3333";
const config = makeBrowserConfigStub({ baseUrl }) as BrowserConfig;
it("should connect to the vite baseUrl from runtime config", () => {
const viteBaseUrl = "http://localhost:3333";
(RuntimeConfig.getInstance as SinonStub).returns({ viteBaseUrl });

mkRunner_({ config });
mkRunner_();

assert.calledOnceWith(socketClientStub, baseUrl);
assert.calledOnceWith(socketClientStub, viteBaseUrl);
});

it("should use websocket protocol when connecting", () => {
Expand Down

0 comments on commit 9833520

Please sign in to comment.