Skip to content

Commit ec5e174

Browse files
authored
fix: fix missing steps when time travel is on (#1102)
1 parent 76099cc commit ec5e174

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/browser/history/index.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { eventWithTime } from "@rrweb/types";
1+
import { isPromise } from "util/types";
22
import { Callstack } from "./callstack";
33
import * as cmds from "./commands";
44
import { isGroup, normalizeCommandArgs, runWithHooks, shouldRecordSnapshots } from "./utils";
@@ -70,27 +70,40 @@ const runWithHistoryHooks = <T>({ session, callstack, nodeData, fn, config }: Ru
7070
return fn();
7171
}
7272

73+
let rrwebPromise: Promise<void> | null = null;
74+
75+
try {
76+
const timeTravelMode = config.timeTravel.mode;
77+
const isRetry = (session.executionContext?.ctx?.attempt ?? 0) > 0;
78+
const shouldRecord = shouldRecordSnapshots(timeTravelMode, isRetry);
79+
80+
if (shouldRecord && process.send && session.executionContext?.ctx?.currentTest) {
81+
rrwebPromise = installRrwebAndCollectEvents(session, callstack)
82+
.then(rrwebEvents => {
83+
const rrwebEventsFiltered = filterEvents(rrwebEvents);
84+
sendFilteredEvents(session, rrwebEventsFiltered);
85+
})
86+
.catch(e => {
87+
console.warn("An error occurred during capturing snapshots in browser.", e);
88+
});
89+
}
90+
} catch (e) {
91+
console.warn("An error occurred during capturing snapshots in browser.", e);
92+
}
93+
7394
return runWithHooks({
74-
before: async () => {
75-
try {
76-
const timeTravelMode = config.timeTravel.mode;
77-
const isRetry = (session.executionContext?.ctx?.attempt ?? 0) > 0;
78-
const shouldRecord = shouldRecordSnapshots(timeTravelMode, isRetry);
79-
80-
let rrwebEvents: eventWithTime[] = [];
81-
if (shouldRecord && process.send && session.executionContext?.ctx?.currentTest) {
82-
rrwebEvents = await installRrwebAndCollectEvents(session, callstack);
83-
}
84-
85-
const rrwebEventsFiltered = filterEvents(rrwebEvents);
86-
sendFilteredEvents(session, rrwebEventsFiltered);
87-
} catch (e) {
88-
console.warn("An error occurred during capturing snapshots in browser.", e);
95+
before: () => {
96+
callstack.enter(mkHistoryNode(nodeData));
97+
},
98+
fn: () => {
99+
const result = fn();
100+
101+
if (rrwebPromise && isPromise(result)) {
102+
return Promise.all([result, rrwebPromise]).then(([fnResult]) => fnResult) as T;
89103
}
90104

91-
callstack.enter(mkHistoryNode(nodeData));
105+
return result;
92106
},
93-
fn,
94107
after: () => callstack.leave(nodeData.key!),
95108
error: () => callstack.markError(shouldPropagateFn),
96109
});

src/browser/history/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isPromise } from "util/types";
12
import _ from "lodash";
23
import { TestStep, TestStepKey } from "../../types";
34
import { TimeTravelMode } from "../../config";
@@ -29,8 +30,6 @@ export const normalizeCommandArgs = (commandName: string, args: unknown[] = []):
2930
});
3031
};
3132

32-
const isPromise = (val: unknown): val is Promise<unknown> => typeof _.get(val, "then") === "function";
33-
3433
export const isGroup = (node: TestStep): boolean => Boolean(node && node[TestStepKey.IsGroup]);
3534

3635
export const runWithHooks = <T>({ fn, before, after, error }: HookFunctions<T>): T => {

0 commit comments

Comments
 (0)