Skip to content

Commit

Permalink
feat: add ability to show user log in component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Apr 16, 2024
1 parent 202af7d commit 772cf09
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/runner/browser-env/vite/browser-modules/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const MAX_ARGS_LENGTH = 50;
// used from - https://github.com/jestjs/jest/blob/726ca20752e38c18e20aa21740cec7aba7891946/packages/pretty-format/src/plugins/AsymmetricMatcher.ts#L11-L14
export const ASYMMETRIC_MATCHER =
typeof Symbol === "function" && Symbol.for ? Symbol.for("jest.asymmetricMatcher") : 0x13_57_a5;

export const CONSOLE_METHODS = ["log", "info", "warn", "error", "debug", "table"] as const;
3 changes: 3 additions & 0 deletions src/runner/browser-env/vite/browser-modules/mocha/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TestParser } from "./parser.js";
import { wrapConsoleMethods } from "../utils/index.js";
import { getErrorsOnPageLoad, getErrorsOnRunRunnable, BrowserError } from "../errors/index.js";
import { BrowserEventNames, WorkerEventNames, type BrowserViteSocket, type RunnableFn } from "../types.js";

Expand Down Expand Up @@ -33,6 +34,8 @@ export class MochaWrapper {
}

this._socket.emit(BrowserEventNames.initialize, getErrorsOnPageLoad(error));

wrapConsoleMethods();
}

private _validate(): never | void {
Expand Down
9 changes: 8 additions & 1 deletion src/runner/browser-env/vite/browser-modules/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BROWSER_EVENT_PREFIX, WORKER_EVENT_PREFIX } from "./constants.js";
import { BROWSER_EVENT_PREFIX, WORKER_EVENT_PREFIX, CONSOLE_METHODS } from "./constants.js";
import { BrowserError, type ViteError } from "./errors/index.js";
import type { Socket } from "socket.io-client";
import type { Expect, MatcherState } from "expect";
Expand All @@ -13,6 +13,7 @@ export enum BrowserEventNames {
initialize = `${BROWSER_EVENT_PREFIX}:initialize`,
runBrowserCommand = `${BROWSER_EVENT_PREFIX}:runBrowserCommand`,
runExpectMatcher = `${BROWSER_EVENT_PREFIX}:runExpectMatcher`,
callConsoleMethod = `${BROWSER_EVENT_PREFIX}:callConsoleMethod`,
}

export interface BrowserRunBrowserCommandPayload {
Expand All @@ -28,6 +29,11 @@ export interface BrowserRunExpectMatcherPayload {
context?: WebdriverIO.Browser | WebdriverIO.Element | ElementArray | ChainablePromiseElement<WebdriverIO.Element>;
}

export interface BrowserCallConsoleMethodPayload {
method: (typeof CONSOLE_METHODS)[number];
args: unknown[];
}

export interface BrowserViteEvents {
[BrowserEventNames.initialize]: (payload: ViteError[]) => void;
[BrowserEventNames.runBrowserCommand]: (
Expand All @@ -38,6 +44,7 @@ export interface BrowserViteEvents {
payload: BrowserRunExpectMatcherPayload,
cb: (args: [{ pass: boolean; message: string }]) => void,
) => void;
[BrowserEventNames.callConsoleMethod]: (payload: BrowserCallConsoleMethodPayload) => void;
}

// TODO: use from nodejs code when migrate to esm
Expand Down
16 changes: 16 additions & 0 deletions src/runner/browser-env/vite/browser-modules/utils/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CONSOLE_METHODS } from "../constants.js";
import { BrowserEventNames } from "../types.js";

export const wrapConsoleMethods = (): void => {
const { socket } = window.__testplane__;

for (const method of CONSOLE_METHODS) {
const origCommand = console[method].bind(console);

console[method] = (...args: unknown[]): void => {
socket.emit(BrowserEventNames.callConsoleMethod, { method, args });

origCommand(...args);
};
}
};
1 change: 1 addition & 0 deletions src/runner/browser-env/vite/browser-modules/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./selector.js";
export * from "./console.js";
6 changes: 6 additions & 0 deletions src/runner/browser-env/vite/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ function handleBrowserEvents(
io.to(runUuid).except(socket.id).emit(BrowserEventNames.initialize, payload);
});

socket.on(BrowserEventNames.callConsoleMethod, payload => {
const { runUuid } = socket.handshake.auth;

io.to(runUuid).except(socket.id).emit(BrowserEventNames.callConsoleMethod, payload);
});

socket.on(BrowserEventNames.runBrowserCommand, async (payload, cb) => {
const { runUuid } = socket.handshake.auth;

Expand Down
1 change: 1 addition & 0 deletions src/runner/browser-env/vite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export enum BrowserEventNames {
initialize = `${BROWSER_EVENT_PREFIX}:initialize`,
runBrowserCommand = `${BROWSER_EVENT_PREFIX}:runBrowserCommand`,
runExpectMatcher = `${BROWSER_EVENT_PREFIX}:runExpectMatcher`,
callConsoleMethod = `${BROWSER_EVENT_PREFIX}:callConsoleMethod`,
}
4 changes: 4 additions & 0 deletions src/worker/browser-env/runner/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class TestRunner extends NodejsEnvTestRunner {
async (): Promise<void> => {
const { default: expectMatchers } = await import("expect-webdriverio/lib/matchers");

this._socket.on(BrowserEventNames.callConsoleMethod, payload => {
console[payload.method](...(payload.args || []));
});

this._socket.on(BrowserEventNames.runBrowserCommand, this._handleRunBrowserCommand(browser));
this._socket.on(
BrowserEventNames.runExpectMatcher,
Expand Down
14 changes: 14 additions & 0 deletions test/src/worker/browser-env/runner/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ describe("worker/browser-env/runner/test-runner", () => {
assert.calledOnceWith(NodejsEnvRunner.prototype.run as SinonStub, { ...runOpts, ExecutionThreadCls });
});

describe(`"${BrowserEventNames.callConsoleMethod}" event`, () => {
it("should call console method with passed args", async () => {
sandbox.stub(console, "log");

const socket = mkSocket_() as BrowserViteSocket;
socketClientStub.returns(socket);

await runWithEmitBrowserInit(socket);
socket.emit(BrowserEventNames.callConsoleMethod, { method: "log", args: ["foo", "bar"] });

assert.calledOnceWith(console.log, "foo", "bar");
});
});

describe(`"${WorkerEventNames.initialize}" event`, () => {
it("should emit with correct args", async () => {
const expectMatchers = { foo: sinon.stub(), bar: sinon.stub() };
Expand Down

0 comments on commit 772cf09

Please sign in to comment.