Skip to content

Commit e14cdd0

Browse files
authored
Serialize data-driven object titles as JSON (#5709)
1 parent cfc9545 commit e14cdd0

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎docs/advanced.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Data(function*() {
7979
}).Scenario() // ...
8080
```
8181
82-
*HINT: If you don't use DataTable. add `toString()` method to each object added to data set, so the data could be pretty printed in a test name*
82+
Objects in a data set are serialized as JSON in the test name.
8383
8484
8585
## Debug

‎lib/data/context.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,11 @@ function replaceTitle(title, dataRow) {
7272
return `${title} | ${dataRow.data.getMasked()}`
7373
}
7474

75-
// if `dataRow` is object and has own `toString()` method,
76-
// it should be printed
77-
if (Object.prototype.toString.call(dataRow.data) === Object().toString() && dataRow.data.toString() !== Object().toString()) {
78-
return `${title} | ${dataRow.data}`
79-
}
75+
return `${title} | ${JSON.stringify(dataRow.data, maskSecret)}`
76+
}
8077

81-
return `${title} | ${JSON.stringify(dataRow.data)}`
78+
function maskSecret(key, value) {
79+
return typeof value?.getMasked === 'function' ? value.getMasked() : value
8280
}
8381

8482
function isTableDataRow(row) {

‎test/unit/data/ui_test.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ describe('ui', () => {
8181
dataScenarioConfig.scenarios.forEach(scenario => expect(helper).to.equal(scenario.test.config[helperName]))
8282
})
8383

84-
it("should shows object's toString() method in each scenario's name if the toString() method is overridden", () => {
85-
const data = [{ toString: () => 'test case title' }]
84+
it("should use JSON in each scenario's name if the object overrides toString()", () => {
85+
const data = [{ name: 'John Do', toString: () => 'test case title' }]
8686
const dataScenarioConfig = context.Data(data).Scenario('scenario', () => {})
87-
expect('scenario | test case title').to.equal(dataScenarioConfig.scenarios[0].test.title)
87+
expect('scenario | {"name":"John Do"}').to.equal(dataScenarioConfig.scenarios[0].test.title)
8888
})
8989

9090
it("should shows JSON.stringify() in each scenario's name if the toString() method isn't overridden", () => {
@@ -128,5 +128,11 @@ describe('ui', () => {
128128
const dataScenarioConfig = context.Data([new Secret('theSecretPassword')]).Scenario('scenario', () => {})
129129
expect(dataScenarioConfig.scenarios[0].test.title).to.equal('scenario | *****')
130130
})
131+
132+
it("should not leak a skipped secret object's value into the title", () => {
133+
const data = Secret.secret({ username: 'jon', password: 'theSecretPassword' }, 'password')
134+
context.xData([data]).Scenario('scenario')
135+
expect(suite.tests.at(-1).title).to.equal('scenario | {"username":"jon","password":"*****"}')
136+
})
131137
})
132138
})

0 commit comments

Comments
 (0)