Skip to content

Commit e405e74

Browse files
authored
fix: correctly serialize promise objects in history steps (#1105)
* fix: correctly serialize promise objects in history steps * fix: show object previews in history args when possible
1 parent 1b6b933 commit e405e74

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/browser/history/utils.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inspect } from "util";
12
import { isPromise } from "util/types";
23
import _ from "lodash";
34
import { TestStep, TestStepKey } from "../../types";
@@ -18,15 +19,26 @@ export const normalizeCommandArgs = (commandName: string, args: unknown[] = []):
1819
}
1920

2021
return args.map(arg => {
21-
if (typeof arg === "string") {
22-
return _.truncate(arg, { length: MAX_STRING_LENGTH });
22+
try {
23+
if (typeof arg === "string") {
24+
return _.truncate(arg, { length: MAX_STRING_LENGTH });
25+
}
26+
27+
if (isPromise(arg)) {
28+
return "promise";
29+
}
30+
31+
if (_.isPlainObject(arg)) {
32+
return _.truncate(
33+
inspect(arg, { depth: 0, compact: true, breakLength: Infinity, maxArrayLength: 10 }),
34+
{ length: MAX_STRING_LENGTH },
35+
);
36+
}
37+
38+
return _.truncate(String(arg), { length: MAX_STRING_LENGTH });
39+
} catch (err) {
40+
return "unknown";
2341
}
24-
25-
if (_.isPlainObject(arg)) {
26-
return "obj";
27-
}
28-
29-
return String(arg);
3042
});
3143
};
3244

test/src/browser/history/utils.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@ describe("commands-history", () => {
77
describe("utils", () => {
88
describe("normalizeCommandArgs", () => {
99
it("should return representation for an object", () => {
10-
assert.deepEqual(normalizeCommandArgs("click", [{ some: "data" }]), ["obj"]);
10+
assert.deepEqual(normalizeCommandArgs("click", [{ some: "data" }]), ["{ some: 'data' }"]);
11+
});
12+
13+
it("should handle large objects", () => {
14+
const largeObject = {
15+
some: {
16+
nested: {
17+
data: "data",
18+
},
19+
},
20+
field: "value",
21+
array: [1, 2, 3, 4, { some: "data" }],
22+
longString: "abc".repeat(100),
23+
};
24+
25+
assert.deepEqual(normalizeCommandArgs("click", [largeObject]), [
26+
"{ some: [Object], field: 'value', array: [Array...",
27+
]);
1128
});
1229

1330
it('should return truncated representation for the "execute" command', () => {
@@ -25,6 +42,22 @@ describe("commands-history", () => {
2542
it("should convert argument to string if it is not string or object", () => {
2643
assert.deepEqual(normalizeCommandArgs("click", [false, null, 100]), ["false", "null", "100"]);
2744
});
45+
46+
it("should return 'promise' for promise arguments", () => {
47+
const promiseArg = Promise.resolve("test");
48+
49+
assert.deepEqual(normalizeCommandArgs("click", [promiseArg]), ["promise"]);
50+
});
51+
52+
it("should return 'unknown' if error occurs during argument normalization", () => {
53+
const problematicArg = Object.create({
54+
toString: () => {
55+
throw new Error("Cannot convert to string");
56+
},
57+
});
58+
59+
assert.deepEqual(normalizeCommandArgs("click", [problematicArg]), ["unknown"]);
60+
});
2861
});
2962

3063
describe("runWithHooks", () => {

0 commit comments

Comments
 (0)