Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit bfa7ad0

Browse files
authored
fix: ensure console.log event context is defined (#3421)
And add tests to make sure the event's `context` is as expected.
1 parent 165db66 commit bfa7ad0

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/chains/ethereum/console.log/tests/index.test.ts

+48-6
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,40 @@ describe("@ganache/console.log", () => {
151151
}
152152
}
153153

154-
async function watchLogs() {
154+
/**
155+
* Collects all logs emitted via the `ganache:vm:tx:console.log` event as well
156+
* as logs sent to `logger.log`. If the logs from each source do not match
157+
* each other this function throws.
158+
*
159+
* This function also validates that the `context` object emitted by the
160+
* `before`, `after`, and `log` are strictly equal to each other. If they do
161+
* not match this function throws (this could be a bug in the test -- two
162+
* transactions/calls could be running at the same time, or a bug in Ganache's
163+
* event system).
164+
*
165+
* @returns all logs collected over the very next transaction event lifecycle
166+
* for the `provider` (contextual)
167+
*/
168+
async function watchForLogs() {
155169
// collection all logs during the transaction's execution
156170
const allLogs: any[] = [];
171+
const eventLogs: any[] = [];
172+
173+
const { context: beforeContext } = await provider.once(
174+
"ganache:vm:tx:before"
175+
);
157176

158-
await provider.once("ganache:vm:tx:before");
177+
const unsubLogs = provider.on(
178+
"ganache:vm:tx:console.log",
179+
({ context: logContext, logs }) => {
180+
assert.strictEqual(
181+
logContext,
182+
beforeContext,
183+
"`console.log` event context did not match transaction `before` context"
184+
);
185+
eventLogs.push(logs);
186+
}
187+
);
159188

160189
// start listening for logs
161190
logger.log = (...logs: any[]) => {
@@ -164,10 +193,23 @@ describe("@ganache/console.log", () => {
164193

165194
// we're done listening to logs once the transaction completes
166195
try {
167-
await provider.once("ganache:vm:tx:after");
196+
const { context: afterContext } = await provider.once(
197+
"ganache:vm:tx:after"
198+
);
199+
assert.strictEqual(
200+
afterContext,
201+
beforeContext,
202+
"`after` event context did not match `before` context"
203+
);
168204
} finally {
169205
logger.log = () => {};
170206
}
207+
unsubLogs();
208+
assert.deepStrictEqual(
209+
eventLogs,
210+
allLogs,
211+
"logs emitted by `console.log` event didn't match logs captured via `logger.log`"
212+
);
171213
return allLogs;
172214
}
173215

@@ -176,7 +218,7 @@ describe("@ganache/console.log", () => {
176218
method: string,
177219
contractAddress: string
178220
) {
179-
const logsProm = watchLogs();
221+
const logsProm = watchForLogs();
180222

181223
// send our logging transaction
182224
const transactionPromise = sendLoggingTransaction(
@@ -358,7 +400,7 @@ describe("@ganache/console.log", () => {
358400
});
359401

360402
it("logs when `console.log` is called within an `eth_call`", async () => {
361-
const logsProm = watchLogs();
403+
const logsProm = watchForLogs();
362404
await provider.send("eth_call", [
363405
{
364406
from,
@@ -398,7 +440,7 @@ describe("@ganache/console.log", () => {
398440
contractAddress
399441
);
400442

401-
const logsProm = watchLogs();
443+
const logsProm = watchForLogs();
402444
// execute our logging tx via `debug_traceTransaction`
403445
await provider.send("debug_traceTransaction", [txHash]);
404446

src/chains/ethereum/ethereum/src/blockchain.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,10 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
10911091
const logs = maybeGetLogs(event);
10921092
if (logs) {
10931093
options.logging.logger.log(...logs);
1094-
this.emit("ganache:vm:tx:console.log", { context, logs });
1094+
this.emit("ganache:vm:tx:console.log", {
1095+
context: transactionContext,
1096+
logs
1097+
});
10951098
}
10961099

10971100
if (!this.#emitStepEvent) return;

0 commit comments

Comments
 (0)