Skip to content

Commit 7cb68de

Browse files
DavertMikDavertMikclaude
authored
fix(data): detect DataTable structurally so Data() works under tsx/cjs (#5641) (#5649)
`Data(table)` threw "Invalid data type" for a valid DataTable when the test file is loaded via `require: ['tsx/cjs']`. tsx transpiles the test to CommonJS and gives it its own copy of the `DataTable` class, while the framework runs as ESM with a different copy, so the `instanceof DataTable` check fails for a genuine DataTable. Detect a DataTable by its structure (`array`/`rows` arrays + `add` method) in addition to `instanceof`, which survives duplicate module instances. Plain arrays, generators and functions still fall through to their own branches. Co-authored-by: DavertMik <davert@testomat.io> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 060d72a commit 7cb68de

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/data/context.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ function isTableDataRow(row) {
8282
return has.call(row, 'data') && has.call(row, 'skip')
8383
}
8484

85+
function isDataTable(dataTable) {
86+
if (dataTable instanceof DataTable) return true
87+
return Boolean(dataTable) && Array.isArray(dataTable.array) && Array.isArray(dataTable.rows) && typeof dataTable.add === 'function'
88+
}
89+
8590
function detectDataType(dataTable) {
86-
if (dataTable instanceof DataTable) {
91+
if (isDataTable(dataTable)) {
8792
return dataTable.rows
8893
}
8994

test/unit/data/ui_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,29 @@ describe('ui', () => {
9797
const dataScenarioConfig = context.Data(dataTable).Scenario('scenario', () => {})
9898
expect('scenario | {"username":"Username","password":"*****"}').to.equal(dataScenarioConfig.scenarios[2].test.title)
9999
})
100+
101+
it('accepts a DataTable coming from a duplicate module instance', () => {
102+
class ForeignDataTable {
103+
constructor(array) {
104+
this.array = array
105+
this.rows = []
106+
}
107+
108+
add(array) {
109+
const data = {}
110+
this.array.forEach((key, i) => (data[key] = array[i]))
111+
this.rows.push({ skip: false, data })
112+
}
113+
}
114+
115+
const foreign = new ForeignDataTable(['username', 'password'])
116+
foreign.add(['jon', 'snow'])
117+
foreign.add(['tyrion', 'lannister'])
118+
119+
expect(foreign).to.not.be.instanceOf(DataTable)
120+
const dataScenarioConfig = context.Data(foreign).Scenario('scenario', () => {})
121+
expect(dataScenarioConfig.scenarios).to.have.lengthOf(2)
122+
expect(dataScenarioConfig.scenarios[0].test.title).to.equal('scenario | {"username":"jon","password":"snow"}')
123+
})
100124
})
101125
})

0 commit comments

Comments
 (0)