diff --git a/src/runner/browser-env/index.ts b/src/runner/browser-env/index.ts deleted file mode 100644 index b6490d4e0..000000000 --- a/src/runner/browser-env/index.ts +++ /dev/null @@ -1,51 +0,0 @@ -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"; -import defaults from "../../config/defaults"; - -export class MainRunner extends NodejsEnvRunner { - private _viteServer: ViteServer; - - constructor(config: Config, interceptors: Interceptor[]) { - super(config, interceptors); - - this._viteServer = ViteServer.create(config); - } - - async run(testCollection: TestCollection, stats: RunnerStats): Promise { - 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}`); - } - - this._useBaseUrlFromVite(); - await super.run(testCollection, stats); - } - - private _useBaseUrlFromVite(): void { - const viteBaseUrl = this._viteServer.baseUrl!; - const defaultBaseUrl = defaults.baseUrl; - - if (this.config.baseUrl === defaultBaseUrl) { - this.config.baseUrl = viteBaseUrl; - } - - for (const broConfig of Object.values(this.config.browsers)) { - if (broConfig.baseUrl === defaultBaseUrl) { - broConfig.baseUrl = viteBaseUrl; - } - } - } - - cancel(): void { - super.cancel(); - - this._viteServer.close(); - } -} diff --git a/src/runner/browser-env/vite/server.ts b/src/runner/browser-env/vite/server.ts index 562830d58..51c6d3797 100644 --- a/src/runner/browser-env/vite/server.ts +++ b/src/runner/browser-env/vite/server.ts @@ -13,7 +13,9 @@ import { plugin as generateIndexHtml } from "./plugins/generate-index-html"; import { plugin as mockPlugin } from "./plugins/mock"; import { ManualMock } from "./manual-mock"; import { Config } from "../../../config"; +import RuntimeConfig from "../../../config/runtime-config"; import { VITE_DEFAULT_CONFIG_ENV } from "./constants"; +import defaults from "../../../config/defaults"; import type { ViteDevServer, UserConfig, InlineConfig } from "vite"; import type { BrowserTestRunEnvOptions } from "./types"; @@ -76,6 +78,8 @@ export class ViteServer { await this._server.listen(); + this._useBaseUrlFromVite(); + logger.log(chalk.green(`Vite server started on ${this.baseUrl}`)); } @@ -115,4 +119,21 @@ export class ViteServer { get baseUrl(): string | undefined { return this._server?.resolvedUrls!.local[0]; } + + private _useBaseUrlFromVite(): void { + const viteBaseUrl = this.baseUrl!; + const defaultBaseUrl = defaults.baseUrl; + + RuntimeConfig.getInstance().extend({ viteBaseUrl }); + + if (this._testplaneConfig.baseUrl === defaultBaseUrl) { + this._testplaneConfig.baseUrl = viteBaseUrl; + } + + for (const broConfig of Object.values(this._testplaneConfig.browsers)) { + if (broConfig.baseUrl === defaultBaseUrl) { + broConfig.baseUrl = viteBaseUrl; + } + } + } } diff --git a/src/testplane.ts b/src/testplane.ts index c8d95f531..fb5cfbb1e 100644 --- a/src/testplane.ts +++ b/src/testplane.ts @@ -3,8 +3,7 @@ import _ from "lodash"; import fs from "fs-extra"; import { Stats as RunnerStats } from "./stats"; import { BaseTestplane } from "./base-testplane"; -import type { MainRunner as NodejsEnvRunner } from "./runner"; -import type { MainRunner as BrowserEnvRunner } from "./runner/browser-env"; +import type { MainRunner } from "./runner"; import RuntimeConfig from "./config/runtime-config"; import { MasterAsyncEvents, MasterEvents, MasterSyncEvents } from "./events"; import eventsUtils from "./events/utils"; @@ -19,6 +18,7 @@ import { initDevServer } from "./dev-server"; import { ConfigInput } from "./config/types"; import { MasterEventHandler, Test, TestResult } from "./types"; import { preloadWebdriverIO } from "./utils/preload-utils"; +import { ViteServer } from "./runner/browser-env/vite/server"; interface RunOpts { browsers: string[]; @@ -70,7 +70,8 @@ export interface Testplane { export class Testplane extends BaseTestplane { protected failed: boolean; protected failedList: FailedListItem[]; - protected runner: NodejsEnvRunner | BrowserEnvRunner | null; + protected runner: MainRunner | null; + protected viteServer: ViteServer | null; constructor(config?: string | ConfigInput) { super(config); @@ -78,6 +79,7 @@ export class Testplane extends BaseTestplane { this.failed = false; this.failedList = []; this.runner = null; + this.viteServer = null; } extendCli(parser: Command): void { @@ -91,6 +93,15 @@ export class Testplane extends BaseTestplane { configPath: this._config.configPath, }); + if (!isRunInNodeJsEnv(this._config)) { + try { + this.viteServer = ViteServer.create(this._config); + await this.viteServer.start(); + } catch (err) { + throw new Error(`Vite server failed to start: ${(err as Error).message}`); + } + } + return super._init(); } @@ -126,9 +137,7 @@ export class Testplane extends BaseTestplane { this._config.system.mochaOpts.timeout = 0; } - const RunnerClass = isRunInNodeJsEnv(this._config) - ? await import("./runner").then(m => m.MainRunner) - : await import("./runner/browser-env").then(m => m.MainRunner); + const RunnerClass = await import("./runner").then(m => m.MainRunner); const runner = RunnerClass.create(this._config, this._interceptors); this.runner = runner; @@ -257,6 +266,10 @@ export class Testplane extends BaseTestplane { }, timeout).unref(); } + if (this.viteServer) { + this.viteServer.close(); + } + if (this.runner) { this.runner.cancel(); } diff --git a/src/types/index.ts b/src/types/index.ts index 2053d81c0..9b5ca3795 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,6 +1,5 @@ import type { Events } from "../events"; -import type { MainRunner as NodejsEnvRunner } from "../runner"; -import type { MainRunner as BrowserEnvRunner } from "../runner/browser-env/index"; +import type { MainRunner } from "../runner"; import type { TestCollection } from "../test-collection"; import type { Test } from "../test-reader/test-object/test"; import type { Suite } from "../test-reader/test-object/suite"; @@ -219,7 +218,7 @@ export interface SnapshotsData { export type MasterEventHandler = { (event: Events["INIT"], callback: () => Promise | void): T; - (event: Events["RUNNER_START"], callback: (runner: NodejsEnvRunner | BrowserEnvRunner) => Promise | void): T; + (event: Events["RUNNER_START"], callback: (runner: MainRunner) => Promise | void): T; (event: Events["RUNNER_END"], callback: (result: StatsResult) => Promise | void): T; (event: Events["SESSION_START"], callback: AsyncSessionEventCallback): T; (event: Events["SESSION_END"], callback: AsyncSessionEventCallback): T; diff --git a/test/src/runner/browser-env/index.ts b/test/src/runner/browser-env/index.ts deleted file mode 100644 index ebcb61869..000000000 --- a/test/src/runner/browser-env/index.ts +++ /dev/null @@ -1,112 +0,0 @@ -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"; - -import { makeConfigStub } from "../../../utils"; -import type { Config } from "../../../../src/config"; - -describe("BrowserEnvRunner", () => { - const sandbox = sinon.createSandbox(); - - const run_ = async ( - opts: { config?: Config; testCollection?: TestCollection; stats?: RunnerStats } = {}, - ): Promise => { - const config = opts.config || makeConfigStub(); - const testCollection = opts.testCollection || TestCollection.create({}); - const stats = opts.stats || sinon.createStubInstance(RunnerStats); - - const runner = BrowserEnvRunner.create(config); - runner.init(); - - return runner.run(testCollection, stats); - }; - - beforeEach(() => { - 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(); - sandbox.stub(ViteServer.prototype, "baseUrl").get(() => "http://vite-default.com"); - }); - - afterEach(() => sandbox.restore()); - - describe("constructor", () => { - it("should create vite server", () => { - const config = makeConfigStub(); - - BrowserEnvRunner.create(config); - - assert.calledOnceWith(ViteServer.create, config); - }); - }); - - describe("run", () => { - it("should start vite server", async () => { - await run_(); - - 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")); - - await assert.isRejected(run_(), "Vite server failed to start: o.O"); - }); - - it("should use base url from vite", async () => { - const viteUrl = "http://localhost:4000"; - sandbox.stub(ViteServer.prototype, "baseUrl").get(() => viteUrl); - - const config = makeConfigStub({ - baseUrl: "http://default.com", - browsers: ["b1", "b2"], - }) as Config; - - await run_({ config }); - - assert.equal(config.baseUrl, viteUrl); - assert.equal(config.browsers.b1.baseUrl, viteUrl); - assert.equal(config.browsers.b2.baseUrl, viteUrl); - }); - - it('should call "run" command of base runner at the end', async () => { - await run_(); - - assert.callOrder(ViteServer.prototype.start as SinonStub, NodejsEnvRunner.prototype.run as SinonStub); - }); - }); - - describe("cancel", () => { - it('should call "cancel" command of base runner', () => { - BrowserEnvRunner.create(makeConfigStub()).cancel(); - - assert.calledOnce(NodejsEnvRunner.prototype.cancel as SinonStub); - }); - - it("should close vite server", () => { - BrowserEnvRunner.create(makeConfigStub()).cancel(); - - assert.calledOnce(ViteServer.prototype.close as SinonStub); - }); - }); -}); diff --git a/test/src/runner/browser-env/vite/server.ts b/test/src/runner/browser-env/vite/server.ts index 506eeef1c..4745b7c77 100644 --- a/test/src/runner/browser-env/vite/server.ts +++ b/test/src/runner/browser-env/vite/server.ts @@ -7,10 +7,12 @@ import { ViteServer } from "../../../../../src/runner/browser-env/vite/server"; import { ManualMock } from "../../../../../src/runner/browser-env/vite/manual-mock"; import { makeConfigStub } from "../../../../utils"; import { BROWSER_TEST_RUN_ENV } from "../../../../../src/constants/config"; +import defaults from "../../../../../src/config/defaults"; +import { promiseDelay } from "../../../../../src/utils/promise"; +import RuntimeConfig from "../../../../../src/config/runtime-config"; import type { Config } from "../../../../../src/config"; import type { BrowserTestRunEnvOptions } from "../../../../../src/runner/browser-env/vite/types"; -import { promiseDelay } from "../../../../../src/utils/promise"; describe("runner/browser-env/vite/server", () => { const sandbox = sinon.createSandbox(); @@ -54,6 +56,8 @@ describe("runner/browser-env/vite/server", () => { mockPlugin = sandbox.stub().returns([{ name: "default-plugin-2" }]); sandbox.stub(ManualMock, "create").resolves(sinon.stub() as unknown as ManualMock); + sandbox.stub(RuntimeConfig, "getInstance").returns({ extend: sandbox.stub() }); + ({ ViteServer: ViteServerStub } = proxyquire("../../../../../src/runner/browser-env/vite/server", { "get-port": getPortStub, "./socket": { createSocketServer }, @@ -222,6 +226,45 @@ describe("runner/browser-env/vite/server", () => { assert.callOrder(createSocketServer, viteServer.listen as SinonStub); }); + it("should save vite base url to runtime config", async () => { + const viteBaseUrl = "http://localhost:4444"; + const viteServer = mkViteServer_({ + resolvedUrls: { + local: [viteBaseUrl], + network: [], + }, + }); + (Vite.createServer as SinonStub).resolves(viteServer); + + await ViteServerStub.create(mkConfig_()).start(); + + assert.calledWith((RuntimeConfig.getInstance as SinonStub).lastCall.returnValue.extend, { + viteBaseUrl, + }); + }); + + it("should use base url from vite", async () => { + const viteBaseUrl = "http://localhost:4444"; + const viteServer = mkViteServer_({ + resolvedUrls: { + local: [viteBaseUrl], + network: [], + }, + }); + (Vite.createServer as SinonStub).resolves(viteServer); + + const config = makeConfigStub({ + baseUrl: defaults.baseUrl, + browsers: ["b1", "b2"], + }) as Config; + + await ViteServerStub.create(config).start(); + + assert.equal(config.baseUrl, viteBaseUrl); + assert.equal(config.browsers.b1.baseUrl, viteBaseUrl); + assert.equal(config.browsers.b2.baseUrl, viteBaseUrl); + }); + it("should inform on which address vite server started", async () => { const viteServer = mkViteServer_({ resolvedUrls: { diff --git a/test/src/testplane.js b/test/src/testplane.js index 3ee07fa65..8a06c63bd 100644 --- a/test/src/testplane.js +++ b/test/src/testplane.js @@ -15,9 +15,10 @@ const { Stats: RunnerStats } = require("src/stats"); const { TestReader } = require("src/test-reader"); const { TestCollection } = require("src/test-collection"); const { MasterEvents: RunnerEvents, CommonSyncEvents, MasterAsyncEvents, MasterSyncEvents } = require("src/events"); -const { MainRunner: NodejsEnvRunner } = require("src/runner"); -const { MainRunner: BrowserEnvRunner } = require("src/runner/browser-env"); +const { MainRunner } = require("src/runner"); +const { ViteServer } = require("src/runner/browser-env/vite/server"); const { makeConfigStub } = require("../utils"); +const { BROWSER_TEST_RUN_ENV, NODEJS_TEST_RUN_ENV } = require("src/constants/config"); const { promiseDelay } = require("../../src/utils/promise"); describe("testplane", () => { @@ -29,19 +30,17 @@ describe("testplane", () => { return Testplane.create(); }; - const mkRunnerStubHelper_ = (RunnerCls, runFn) => { + const mkRunner_ = runFn => { const runner = new AsyncEmitter(); - runner.run = sandbox.stub(RunnerCls.prototype, "run").callsFake(runFn && runFn.bind(null, runner)); - runner.addTestToRun = sandbox.stub(RunnerCls.prototype, "addTestToRun"); - runner.init = sandbox.stub(RunnerCls.prototype, "init").named("RunnerInit"); + runner.run = sandbox.stub(MainRunner.prototype, "run").callsFake(runFn && runFn.bind(null, runner)); + runner.addTestToRun = sandbox.stub(MainRunner.prototype, "addTestToRun"); + runner.init = sandbox.stub(MainRunner.prototype, "init").named("RunnerInit"); - sandbox.stub(RunnerCls, "create").returns(runner); + sandbox.stub(MainRunner, "create").returns(runner); return runner; }; - const mkNodejsEnvRunner_ = runFn => mkRunnerStubHelper_(NodejsEnvRunner, runFn); - beforeEach(() => { sandbox.stub(Config, "create").returns(makeConfigStub()); sandbox.stub(pluginsLoader, "load").returns([]); @@ -57,6 +56,10 @@ describe("testplane", () => { loggerWarnStub = sandbox.stub(); loggerErrorStub = sandbox.stub(); + sandbox.stub(ViteServer, "create").returns(Object.create(ViteServer.prototype)); + sandbox.stub(ViteServer.prototype, "start").resolves(); + sandbox.stub(ViteServer.prototype, "close").resolves(); + Testplane = proxyquire("src/testplane", { "./reporters": { initReporters }, "./signal-handler": signalHandler, @@ -77,7 +80,7 @@ describe("testplane", () => { describe("constructor", () => { beforeEach(() => { - sandbox.stub(NodejsEnvRunner, "create").returns(new EventEmitter()); + sandbox.stub(MainRunner, "create").returns(new EventEmitter()); }); describe("logLevel", () => { @@ -155,47 +158,40 @@ describe("testplane", () => { sandbox.stub(Testplane.prototype, "halt"); }); - [ - { name: "nodejs", mkRunner_: mkNodejsEnvRunner_, RunnerCls: NodejsEnvRunner }, - { name: "browser", mkRunner_: mkNodejsEnvRunner_, RunnerCls: BrowserEnvRunner }, - ].forEach(({ name, mkRunner_, RunnerCls }) => { - describe(`${name} environment runner`, () => { - it("should create runner", () => { - mkRunner_(); + it("should create runner", () => { + mkRunner_(); - return runTestplane().then(() => assert.calledOnce(RunnerCls.create)); - }); + return runTestplane().then(() => assert.calledOnce(MainRunner.create)); + }); - it("should create runner with config", () => { - mkRunner_(); + it("should create runner with config", () => { + mkRunner_(); - const config = makeConfigStub(); - Config.create.returns(config); + const config = makeConfigStub(); + Config.create.returns(config); - return mkTestplane_(config).run(() => assert.calledWith(RunnerCls.create, config)); - }); + return mkTestplane_(config).run(() => assert.calledWith(MainRunner.create, config)); + }); - it("should create runner with interceptors", async () => { - mkRunner_(); + it("should create runner with interceptors", async () => { + mkRunner_(); - const testplane = mkTestplane_(); - const fooHandler = () => {}; - const barHandler = () => {}; + const testplane = mkTestplane_(); + const fooHandler = () => {}; + const barHandler = () => {}; - testplane.intercept("foo", fooHandler).intercept("bar", barHandler); + testplane.intercept("foo", fooHandler).intercept("bar", barHandler); - await testplane.run(); + await testplane.run(); - assert.calledWith(RunnerCls.create, sinon.match.any, [ - { event: "foo", handler: fooHandler }, - { event: "bar", handler: barHandler }, - ]); - }); - }); + assert.calledWith(MainRunner.create, sinon.match.any, [ + { event: "foo", handler: fooHandler }, + { event: "bar", handler: barHandler }, + ]); }); it("should warn about unknown browsers from cli", () => { - mkNodejsEnvRunner_(); + mkRunner_(); return runTestplane([], { browsers: ["bro3"] }).then(() => assert.calledWithMatch(loggerWarnStub, /Unknown browser ids: bro3/), @@ -203,7 +199,7 @@ describe("testplane", () => { }); it("should init runtime config", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); await runTestplane([], { updateRefs: true, @@ -232,12 +228,12 @@ describe("testplane", () => { local: false, keepBrowserMode: { enabled: false, onFail: false }, }); - assert.callOrder(RuntimeConfig.getInstance, NodejsEnvRunner.create); + assert.callOrder(RuntimeConfig.getInstance, MainRunner.create); }); describe("repl mode", () => { it("should not reset test timeout to 0 if run not in repl", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testplane = mkTestplane_({ lastFailed: { only: false }, system: { mochaOpts: { timeout: 100500 } }, @@ -249,7 +245,7 @@ describe("testplane", () => { }); it("should reset test timeout to 0 if run in repl", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testplane = mkTestplane_({ lastFailed: { only: false }, system: { mochaOpts: { timeout: 100500 } }, @@ -262,7 +258,7 @@ describe("testplane", () => { }); describe("INIT", () => { - beforeEach(() => mkNodejsEnvRunner_()); + beforeEach(() => mkRunner_()); it("should emit INIT on run", () => { const onInit = sinon.spy(); @@ -281,14 +277,14 @@ describe("testplane", () => { const afterInit = sinon.spy(); const testplane = mkTestplane_().on(RunnerEvents.INIT, () => promiseDelay(20).then(afterInit)); - return testplane.run().then(() => assert.callOrder(afterInit, NodejsEnvRunner.prototype.run)); + return testplane.run().then(() => assert.callOrder(afterInit, MainRunner.prototype.run)); }); it("should init runner after emit INIT", () => { const onInit = sinon.spy(); const testplane = mkTestplane_().on(RunnerEvents.INIT, onInit); - return testplane.run().then(() => assert.callOrder(onInit, NodejsEnvRunner.prototype.init)); + return testplane.run().then(() => assert.callOrder(onInit, MainRunner.prototype.init)); }); it("should send INIT event only once", () => { @@ -301,13 +297,51 @@ describe("testplane", () => { .then(() => testplane.run()) .then(() => assert.calledOnce(onInit)); }); + + describe("vite server", () => { + it(`should do nothing if testRunEnv is ${NODEJS_TEST_RUN_ENV}`, async () => { + const testplane = mkTestplane_({ system: { testRunEnv: NODEJS_TEST_RUN_ENV } }); + + await testplane.run(); + + assert.notCalled(ViteServer.create); + assert.notCalled(ViteServer.prototype.start); + }); + + it("should create vite server", async () => { + const config = makeConfigStub({ system: { testRunEnv: BROWSER_TEST_RUN_ENV } }); + const testplane = mkTestplane_(config); + + await testplane.run(); + + assert.calledOnceWith(ViteServer.create, config); + }); + + it("should start vite server", async () => { + const testplane = mkTestplane_({ system: { testRunEnv: BROWSER_TEST_RUN_ENV } }); + + await testplane.run(); + + assert.calledOnceWith(ViteServer.prototype.start); + }); + + it("should throw error if vite server failed", async () => { + ViteServer.prototype.start.rejects(new Error("o.O")); + + const testplane = mkTestplane_({ + system: { testRunEnv: BROWSER_TEST_RUN_ENV }, + }); + + await assert.isRejected(testplane.run(), "Vite server failed to start: o.O"); + }); + }); }); describe("reporters", () => { let runner; beforeEach(() => { - runner = mkNodejsEnvRunner_(); + runner = mkRunner_(); }); it("should initialize passed reporters", async () => { @@ -332,7 +366,7 @@ describe("testplane", () => { }); describe("reading the tests", () => { - beforeEach(() => mkNodejsEnvRunner_()); + beforeEach(() => mkRunner_()); it("should read tests", async () => { const testPaths = ["foo/bar"]; @@ -366,7 +400,7 @@ describe("testplane", () => { await runTestplane(testCollection); - assert.calledOnceWith(NodejsEnvRunner.prototype.run, testCollection); + assert.calledOnceWith(MainRunner.prototype.run, testCollection); }); it("should not read tests if test collection passed instead of paths", async () => { @@ -381,24 +415,24 @@ describe("testplane", () => { describe("running of tests", () => { it("should run tests", () => { - mkNodejsEnvRunner_(); + mkRunner_(); - return runTestplane().then(() => assert.calledOnce(NodejsEnvRunner.prototype.run)); + return runTestplane().then(() => assert.calledOnce(MainRunner.prototype.run)); }); it("should use read tests", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testCollection = TestCollection.create(); sandbox.stub(Testplane.prototype, "readTests").resolves(testCollection); await runTestplane(); - assert.calledWith(NodejsEnvRunner.prototype.run, testCollection); + assert.calledWith(MainRunner.prototype.run, testCollection); }); it("should create runner stats", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testplane = mkTestplane_(); @@ -408,17 +442,17 @@ describe("testplane", () => { }); it("should use created runner stats ", async () => { - mkNodejsEnvRunner_(); + mkRunner_(); RunnerStats.create.returns("foo bar"); await runTestplane(); - assert.calledWith(NodejsEnvRunner.prototype.run, sinon.match.any, "foo bar"); + assert.calledWith(MainRunner.prototype.run, sinon.match.any, "foo bar"); }); it('should return "true" if there are no failed tests', () => { - mkNodejsEnvRunner_(); + mkRunner_(); return runTestplane().then(success => assert.isTrue(success)); }); @@ -429,7 +463,7 @@ describe("testplane", () => { browserId: "chrome", browserVersion: "1", }; - mkNodejsEnvRunner_(runner => runner.emit(RunnerEvents.TEST_FAIL, results)); + mkRunner_(runner => runner.emit(RunnerEvents.TEST_FAIL, results)); return runTestplane().then(success => assert.isFalse(success)); }); @@ -438,7 +472,7 @@ describe("testplane", () => { const testplane = mkTestplane_(); const err = new Error(); - mkNodejsEnvRunner_(runner => runner.emit(RunnerEvents.ERROR, err)); + mkRunner_(runner => runner.emit(RunnerEvents.ERROR, err)); return testplane.run().then(() => assert.calledOnceWith(testplane.halt, err)); }); @@ -449,7 +483,7 @@ describe("testplane", () => { browserId: "chrome", browserVersion: "1", }; - mkNodejsEnvRunner_(runner => { + mkRunner_(runner => { runner.emit(RunnerEvents.TEST_FAIL, results), runner.emit(RunnerEvents.RUNNER_END); }); @@ -467,7 +501,7 @@ describe("testplane", () => { describe("should passthrough", () => { it("all synchronous runner events", () => { - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); return testplane.run().then(() => { @@ -484,7 +518,7 @@ describe("testplane", () => { it('synchronous runner events before "Runner.run" called', () => { sandbox.stub(eventsUtils, "passthroughEvent"); - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); return testplane.run().then(() => { @@ -499,7 +533,7 @@ describe("testplane", () => { }); it("all asynchronous runner events", () => { - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); return testplane.run().then(() => { @@ -516,7 +550,7 @@ describe("testplane", () => { it('asynchronous runner events before "Runner.run" called', () => { sandbox.stub(eventsUtils, "passthroughEventAsync"); - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); return testplane.run().then(() => { @@ -531,7 +565,7 @@ describe("testplane", () => { }); it("all runner events with passed event data", () => { - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); const results = { fullTitle: () => "Title", @@ -553,7 +587,7 @@ describe("testplane", () => { }); it("exit event from signalHandler", () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testplane = mkTestplane_(); const onExit = sinon.spy().named("onExit"); @@ -570,7 +604,7 @@ describe("testplane", () => { it('exit event before "Runner.run" called', () => { sandbox.stub(eventsUtils, "passthroughEventAsync"); - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); return testplane.run().then(() => { @@ -588,7 +622,7 @@ describe("testplane", () => { describe("addTestToRun", () => { it("should pass test to the existing runner", async () => { - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); const test = {}; @@ -599,7 +633,7 @@ describe("testplane", () => { }); it("should return false when testplane is not running", () => { - const runner = mkNodejsEnvRunner_(); + const runner = mkRunner_(); const testplane = mkTestplane_(); const added = testplane.addTestToRun({}); @@ -830,7 +864,7 @@ describe("testplane", () => { }); it('should return "false" if there are no failed tests or errors', () => { - mkNodejsEnvRunner_(); + mkRunner_(); const testplane = mkTestplane_(); @@ -846,7 +880,7 @@ describe("testplane", () => { browserVersion: "1", }; - mkNodejsEnvRunner_(runner => { + mkRunner_(runner => { runner.emit(RunnerEvents.TEST_FAIL, results); assert.isTrue(testplane.isFailed()); @@ -871,10 +905,8 @@ describe("testplane", () => { testplane = mkTestplane_(); sandbox.stub(process, "exit"); - sandbox - .stub(NodejsEnvRunner.prototype, "run") - .callsFake(() => testplane.emitAndWait(RunnerEvents.RUNNER_START)); - sandbox.stub(NodejsEnvRunner.prototype, "cancel"); + sandbox.stub(MainRunner.prototype, "run").callsFake(() => testplane.emitAndWait(RunnerEvents.RUNNER_START)); + sandbox.stub(MainRunner.prototype, "cancel"); }); it("should log provided error", () => { @@ -889,10 +921,20 @@ describe("testplane", () => { }); }); + it("should close vite server", async () => { + testplane = mkTestplane_(makeConfigStub({ system: { testRunEnv: BROWSER_TEST_RUN_ENV } })); + + await testplane.run(); + + testplane.halt(new Error("test error")); + + assert.calledOnce(ViteServer.prototype.close); + }); + it("should not cancel test runner if runner is not inited", () => { testplane.halt(new Error("test error")); - assert.notCalled(NodejsEnvRunner.prototype.cancel); + assert.notCalled(MainRunner.prototype.cancel); }); it("should cancel test runner", () => { @@ -901,7 +943,7 @@ describe("testplane", () => { }); return testplane.run().finally(() => { - assert.calledOnce(NodejsEnvRunner.prototype.cancel); + assert.calledOnce(MainRunner.prototype.cancel); }); }); @@ -924,7 +966,7 @@ describe("testplane", () => { await testplane.run(); - assert.callOrder(global.setTimeout, NodejsEnvRunner.prototype.cancel); + assert.callOrder(global.setTimeout, MainRunner.prototype.cancel); }); it("should force exit if timeout is reached", () => { diff --git a/test/utils.js b/test/utils.js index 96c8c036c..cf3be2951 100644 --- a/test/utils.js +++ b/test/utils.js @@ -40,6 +40,7 @@ function makeConfigStub(opts = {}) { }); const config = { + baseUrl: opts.baseUrl, browsers: {}, plugins: opts.plugins, system: opts.system,