@@ -151,11 +151,40 @@ describe("@ganache/console.log", () => {
151
151
}
152
152
}
153
153
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 ( ) {
155
169
// collection all logs during the transaction's execution
156
170
const allLogs : any [ ] = [ ] ;
171
+ const eventLogs : any [ ] = [ ] ;
172
+
173
+ const { context : beforeContext } = await provider . once (
174
+ "ganache:vm:tx:before"
175
+ ) ;
157
176
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
+ ) ;
159
188
160
189
// start listening for logs
161
190
logger . log = ( ...logs : any [ ] ) => {
@@ -164,10 +193,23 @@ describe("@ganache/console.log", () => {
164
193
165
194
// we're done listening to logs once the transaction completes
166
195
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
+ ) ;
168
204
} finally {
169
205
logger . log = ( ) => { } ;
170
206
}
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
+ ) ;
171
213
return allLogs ;
172
214
}
173
215
@@ -176,7 +218,7 @@ describe("@ganache/console.log", () => {
176
218
method : string ,
177
219
contractAddress : string
178
220
) {
179
- const logsProm = watchLogs ( ) ;
221
+ const logsProm = watchForLogs ( ) ;
180
222
181
223
// send our logging transaction
182
224
const transactionPromise = sendLoggingTransaction (
@@ -358,7 +400,7 @@ describe("@ganache/console.log", () => {
358
400
} ) ;
359
401
360
402
it ( "logs when `console.log` is called within an `eth_call`" , async ( ) => {
361
- const logsProm = watchLogs ( ) ;
403
+ const logsProm = watchForLogs ( ) ;
362
404
await provider . send ( "eth_call" , [
363
405
{
364
406
from,
@@ -398,7 +440,7 @@ describe("@ganache/console.log", () => {
398
440
contractAddress
399
441
) ;
400
442
401
- const logsProm = watchLogs ( ) ;
443
+ const logsProm = watchForLogs ( ) ;
402
444
// execute our logging tx via `debug_traceTransaction`
403
445
await provider . send ( "debug_traceTransaction" , [ txHash ] ) ;
404
446
0 commit comments