Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to attach anvil logs to playwright report #273

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/test/src/anvil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ export interface AnvilArgs {
transactionBlockKeeper?: number | undefined;
}

export interface AnvilProcessCallbacks {
/**
* Called when a message is received from the Anvil process.
*/
onMessage?: (message: string) => void;
/**
* Called when an error is received from the Anvil process.
*/
onError?: (message: string) => void;
/**
* Called when the Anvil process is closed.
*/
onClose?: (message: string) => void;
}

/**
* Converts an object of options to an array of command line arguments.
*
Expand Down Expand Up @@ -302,6 +317,7 @@ let workerInstances = 0;
export const spawnAnvil = async (
args: AnvilArgs,
index = workerInstances++,
callbacks?: AnvilProcessCallbacks,
): Promise<{
rpcUrl: `http://localhost:${number}`;
stop: () => boolean;
Expand All @@ -316,6 +332,7 @@ export const spawnAnvil = async (
subprocess.stdout.on("data", (data) => {
const message = `[port ${port}] ${data.toString()}`;

callbacks?.onMessage?.(message);
// console.debug(message);

if (message.includes("Listening on")) {
Expand All @@ -327,9 +344,17 @@ export const spawnAnvil = async (
subprocess.stderr.on("data", (data) => {
const message = `[port ${port}] ${data.toString()}`;

callbacks?.onError?.(message);

if (!started) reject(message);
else console.warn(message);
});

subprocess.on("close", (code) => {
const message = `[port ${port}] Anvil exited with code ${code}`;

callbacks?.onClose?.(message);
});
});

return {
Expand Down
36 changes: 34 additions & 2 deletions packages/test/src/playwright.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from "@playwright/test";
import { type TestInfo, test } from "@playwright/test";
import { http, type Chain, formatUnits } from "viem";
import { type AnvilArgs, spawnAnvil } from "./anvil";
import { type AnvilTestClient, createAnvilTestClient } from "./client";
Expand All @@ -7,9 +7,40 @@ export interface PlaywrightTestContext<chain extends Chain = Chain> {
client: AnvilTestClient<chain>;
}

const attachLogToPlaywright = (testInfo: TestInfo) => {
const anvilLogs: string[] = [];

const attachLog = (message: string) => {
anvilLogs.push(message);
};

const onClose = (message: string) => {
attachLog(message);

testInfo.attach("Anvil Logs", {
body: anvilLogs.join("\n"),
contentType: "text/plain",
});
};

return {
onMessage: attachLog,
onError: attachLog,
onClose: onClose,
};
};
Comment on lines +10 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this should be end user code because we're preventing devs from passing their own callbacks to createViemTest

I would simply expect the same optional argument callbacks?: AnvilProcessCallbacks

And for its vitest counterpart too! (also named createViemTest)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think that making it native to your playwright wrapper is a good use case?
But happy to move it wherever you want or even outside of the repo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can export the resulting object (the 3 callbacks) as a constant attachLogs from this file if you want! So you just have to pass it as the third optional parameter, in place of callbacks


/**
* Creates a Playwright test that spawns an Anvil instance and injects a test client.
*
* @param chain - The chain to use.
* @param parameters - The parameters to pass to the Anvil instance.
* @param attachedAnvilLogs - Whether to attach the Anvil logs to the test that can be seen in the Playwright report (attachments tabs).
*/
export const createViemTest = <chain extends Chain>(
chain: chain,
parameters: AnvilArgs = {},
attachAnvilLogs = false,
) => {
parameters.forkChainId ??= chain?.id;
parameters.forkUrl ??= chain?.rpcUrls.default.http[0];
Expand All @@ -22,10 +53,11 @@ export const createViemTest = <chain extends Chain>(

return test.extend<PlaywrightTestContext<chain>>({
// biome-ignore lint/correctness/noEmptyPattern: required by playwright at runtime
client: async ({}, use) => {
client: async ({}, use, testInfo) => {
const { rpcUrl, stop } = await spawnAnvil(
parameters,
test.info().workerIndex,
attachAnvilLogs ? attachLogToPlaywright(testInfo) : undefined,
);

const client = createAnvilTestClient(http(rpcUrl), chain);
Expand Down