Skip to content

Commit 725b13b

Browse files
committed
Added test case to ensure that only the header row is created when no data is passed and the keys option is set
1 parent 36fed3c commit 725b13b

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

test/config/testCsvFilesList.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const csvFileConfig = [
2525
{key: 'trimmedHeader', file: '../data/csv/trimmedHeader.csv'},
2626
{key: 'excelBOM', file: '../data/csv/excelBOM.csv'},
2727
{key: 'specifiedKeys', file: '../data/csv/specifiedKeys.csv'},
28+
{key: 'specifiedKeysNoData', file: '../data/csv/specifiedKeysNoData.csv'},
2829
{key: 'extraLine', file: '../data/csv/extraLine.csv'},
2930
{key: 'noHeader', file: '../data/csv/noHeader.csv'},
3031
{key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'},

test/data/csv/specifiedKeysNoData.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arrayOfStrings,object.subField

test/json2csv.ts

+7
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ export function runTests() {
293293
assert.equal(csv, csvTestData.specifiedKeys);
294294
});
295295

296+
it('should only contain a header when given an empty array and the keys option is provided', () => {
297+
const csv = json2csv(jsonTestData.noData, {
298+
keys: ['arrayOfStrings', 'object.subField']
299+
});
300+
assert.equal(csv, csvTestData.specifiedKeysNoData);
301+
});
302+
296303
it('should use the specified empty field value, if provided', () => {
297304
jsonTestData.emptyFieldValues[0].number = undefined;
298305

0 commit comments

Comments
 (0)