Skip to content

Commit 5aa0c22

Browse files
reubenmillerDavertMik
authored andcommitted
[Data Driven Tests] Accept plain arrays (regression fix) (codeceptjs#924)
* [Data Driven Tests] Accepts plain arrays as well as a DataTable object in the Data() function. This behaviour was already supported in v1.0.1 * Added example of the usage when using a simple array with Data()
1 parent 730e31e commit 5aa0c22

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/data/context.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ module.exports = function (context) {
4646
};
4747
};
4848

49+
function isTableDataRow(row) {
50+
const has = Object.prototype.hasOwnProperty;
51+
return has.call(row, 'data') && has.call(row, 'skip');
52+
}
53+
4954
function detectDataType(dataTable) {
5055
if (dataTable instanceof DataTable) {
5156
return dataTable.rows;
@@ -64,7 +69,15 @@ function detectDataType(dataTable) {
6469
return dataTable();
6570
}
6671
if (Array.isArray(dataTable)) {
67-
return dataTable;
72+
return dataTable.map((item) => {
73+
if (isTableDataRow(item)) {
74+
return item;
75+
}
76+
return {
77+
data: item,
78+
skip: false,
79+
};
80+
});
6881
}
6982

7083
throw new Error('Invalid data type. Data accepts either: DataTable || generator || Array || function');

test/data/sandbox/ddt_test.ddt.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ Data(function* () {
2222
}).Scenario('Should log accounts3', (I, current) => {
2323
console.log(`Got changed login ${current[0]}`);
2424
});
25+
26+
Data(['1', '2', '3']).Scenario('Should log array of strings', (I, current) => {
27+
console.log(`Got array item ${current}`);
28+
});

0 commit comments

Comments
 (0)