-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to run unit tests in browser
- Loading branch information
Showing
55 changed files
with
4,160 additions
and
432 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ViteServer } from "./vite/server"; | ||
import { MainRunner as NodejsEnvRunner } from ".."; | ||
import { TestCollection } from "../../test-collection"; | ||
import { Config } from "../../config"; | ||
import { Interceptor } from "../../events"; | ||
import type { Stats as RunnerStats } from "../../stats"; | ||
|
||
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<void> { | ||
try { | ||
await this._viteServer.start(); | ||
} 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!; | ||
|
||
this.config.baseUrl = viteBaseUrl; | ||
for (const broConfig of Object.values(this.config.browsers)) { | ||
broConfig.baseUrl = viteBaseUrl; | ||
} | ||
} | ||
|
||
cancel(): void { | ||
super.cancel(); | ||
|
||
this._viteServer.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const DOCUMENT_TITLE = "Testplane Browser Test"; | ||
export const VITE_OVERLAY_SELECTOR = "vite-error-overlay"; | ||
|
||
export const VITE_SELECTORS = { | ||
overlay: "vite-error-overlay", | ||
overlayMessage: ".message", | ||
overlayStack: ".stack", | ||
overlayFile: ".file", | ||
overlayFrame: ".frame", | ||
overlayTip: ".tip", | ||
}; | ||
|
||
export const BROWSER_EVENT_SUFFIX = "browser"; | ||
export const WORKER_EVENT_SUFFIX = "worker"; |
11 changes: 11 additions & 0 deletions
11
src/runner/browser-env/vite/browser-modules/errors/base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export class BaseError extends Error { | ||
constructor({ message, stack }: { message: string; stack?: string }) { | ||
super(message); | ||
|
||
this.name = this.constructor.name; | ||
|
||
if (stack) { | ||
this.stack = stack; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/runner/browser-env/vite/browser-modules/errors/browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { BaseError } from "./base.js"; | ||
|
||
interface BrowserErrorData { | ||
message: string; | ||
stack?: string; | ||
file?: string; | ||
} | ||
|
||
export class BrowserError extends BaseError { | ||
file?: string; | ||
|
||
static create<T extends BrowserError>(this: new (opts: BrowserErrorData) => T, opts: BrowserErrorData): T { | ||
return new this(opts); | ||
} | ||
|
||
constructor({ message, stack, file }: BrowserErrorData) { | ||
super({ message, stack }); | ||
|
||
if (file) { | ||
this.file = file; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/runner/browser-env/vite/browser-modules/errors/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { BrowserError } from "./browser.js"; | ||
import { LoadPageError } from "./load-page.js"; | ||
import { ViteRuntimeError } from "./vite-runtime.js"; | ||
import { getSelectorTextFromShadowRoot } from "../utils/index.js"; | ||
import { DOCUMENT_TITLE, VITE_SELECTORS } from "../constants.js"; | ||
|
||
export type ErrorOnRunRunnable = ViteRuntimeError | BrowserError | Error; | ||
export type ErrorOnPageLoad = LoadPageError | ErrorOnRunRunnable; | ||
export type ViteError = ErrorOnPageLoad | ErrorOnRunRunnable; | ||
|
||
const getLoadPageErrors = (): LoadPageError[] => { | ||
if (document.title === DOCUMENT_TITLE && window.__testplane__) { | ||
return []; | ||
} | ||
|
||
return [LoadPageError.create()]; | ||
}; | ||
|
||
// TODO: use API from vite to get error in runtime (not existing right now) | ||
const getViteRuntimeErrors = (): ViteRuntimeError[] => { | ||
const viteErrorElem = document.querySelector(VITE_SELECTORS.overlay); | ||
|
||
if (!viteErrorElem || !viteErrorElem.shadowRoot) { | ||
return []; | ||
} | ||
|
||
const shadowRoot = viteErrorElem.shadowRoot; | ||
|
||
const message = getSelectorTextFromShadowRoot(VITE_SELECTORS.overlayMessage, shadowRoot); | ||
const stack = getSelectorTextFromShadowRoot(VITE_SELECTORS.overlayStack, shadowRoot); | ||
const file = getSelectorTextFromShadowRoot(VITE_SELECTORS.overlayFile, shadowRoot); | ||
const frame = getSelectorTextFromShadowRoot(VITE_SELECTORS.overlayFrame, shadowRoot); | ||
const tip = getSelectorTextFromShadowRoot(VITE_SELECTORS.overlayTip, shadowRoot); | ||
|
||
return [ViteRuntimeError.create({ message, stack, file, frame, tip })]; | ||
}; | ||
|
||
const getBrowserErrors = (): BrowserError[] => { | ||
return window.__testplane__.errors; | ||
}; | ||
|
||
export const prepareError = (error: Error): Error => { | ||
// in order to correctly pass errors through websocket | ||
return JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))); | ||
}; | ||
|
||
const getErrors = (errors: ViteError | ViteError[] = []): ViteError[] => { | ||
return [errors, getViteRuntimeErrors(), getBrowserErrors()].flat().filter(Boolean).map(prepareError); | ||
}; | ||
|
||
export const getErrorsOnPageLoad = (initError?: Error): ErrorOnPageLoad[] => { | ||
const errors = new Array<ViteError>().concat(initError || [], getLoadPageErrors()); | ||
|
||
return getErrors(errors); | ||
}; | ||
|
||
export const getErrorsOnRunRunnable = (runnableError?: Error): ViteError[] => { | ||
return getErrors(runnableError); | ||
}; | ||
|
||
export { BrowserError, LoadPageError, ViteRuntimeError }; |
17 changes: 17 additions & 0 deletions
17
src/runner/browser-env/vite/browser-modules/errors/load-page.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BaseError } from "./base.js"; | ||
|
||
interface LoadPageErrorData { | ||
message?: string; | ||
} | ||
|
||
type BrowserErrorCtor<T> = new (opts?: LoadPageErrorData) => T; | ||
|
||
export class LoadPageError extends BaseError { | ||
static create<T extends LoadPageError>(this: BrowserErrorCtor<T>, opts?: LoadPageErrorData): T { | ||
return new this(opts); | ||
} | ||
|
||
constructor({ message = "failed to load Vite test page" }: LoadPageErrorData = {}) { | ||
super({ message }); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/runner/browser-env/vite/browser-modules/errors/vite-runtime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { BaseError } from "./base.js"; | ||
|
||
interface ViteRuntimeErrorData { | ||
message: string; | ||
stack: string; | ||
file: string; | ||
frame: string; | ||
tip: string; | ||
} | ||
|
||
type ViteRuntimeErrorCtor<T> = new (opts: ViteRuntimeErrorData) => T; | ||
|
||
export class ViteRuntimeError extends BaseError { | ||
file: string; | ||
frame: string; | ||
tip: string; | ||
|
||
static create<T extends ViteRuntimeError>(this: ViteRuntimeErrorCtor<T>, opts: ViteRuntimeErrorData): T { | ||
return new this(opts); | ||
} | ||
|
||
constructor({ message, stack, file, frame, tip }: ViteRuntimeErrorData) { | ||
super({ message }); | ||
|
||
this.stack = `${this.constructor.name}: ${this.message}\n${stack}`; | ||
this.file = file; | ||
this.frame = frame; | ||
this.tip = tip; | ||
} | ||
} |
Oops, something went wrong.