Skip to content

Commit 174afbc

Browse files
committed
feature(jest-each): Enable comments inside .each template literal table
1 parent bd76829 commit 174afbc

File tree

3 files changed

+126
-3
lines changed

3 files changed

+126
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `[jest-validate]` Allow `maxWorkers` as part of the `jest.config.js` ([#8565](https://github.com/facebook/jest/pull/8565))
1515
- `[jest-runtime]` Allow passing configuration objects to transformers ([#7288](https://github.com/facebook/jest/pull/7288))
1616
- `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642))
17+
- `[jest-each]` Enable comments inside `.each` template literal table ([#8717](https://github.com/facebook/jest/pull/8717))
1718

1819
### Fixes
1920

packages/jest-each/src/__tests__/index.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,77 @@ describe('template', () => {
3030
expect(a + b).toBe(expected);
3131
});
3232
});
33+
34+
describe('comments', () => {
35+
describe('removes commented out tests', () => {
36+
let testCount = 0;
37+
const expectedTestCount = 2;
38+
39+
each`
40+
a | b | expected
41+
// ${1} | ${1} | ${0}
42+
${1} | ${1} | ${2}
43+
/* ${1} | ${1} | ${5} */
44+
${1} | ${1} | ${2}
45+
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
46+
testCount += 1;
47+
expect(a + b).toBe(expected);
48+
});
49+
50+
test('test runs', () => {
51+
expect(testCount).toEqual(expectedTestCount);
52+
});
53+
});
54+
55+
describe('removes trailing comments', () => {
56+
let testCount = 0;
57+
const expectedTestCount = 2;
58+
59+
each`
60+
a | b | expected
61+
${0} | ${0} | ${0} // ignores trailing comment
62+
${1} | ${1} | ${2} /* ignores second comment */
63+
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
64+
testCount += 1;
65+
expect(a + b).toBe(expected);
66+
});
67+
68+
test('test runs', () => {
69+
expect(testCount).toEqual(expectedTestCount);
70+
});
71+
});
72+
73+
describe('removes trailing comments in title', () => {
74+
let testCount = 0;
75+
const expectedTestCount = 1;
76+
77+
each`
78+
a | b | expected // should be removed
79+
${0} | ${0} | ${0}
80+
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
81+
testCount += 1;
82+
expect(a + b).toBe(expected);
83+
});
84+
85+
test('test runs', () => {
86+
expect(testCount).toEqual(expectedTestCount);
87+
});
88+
});
89+
90+
describe('removes /**/ comments title', () => {
91+
let testCount = 0;
92+
const expectedTestCount = 1;
93+
each`
94+
a | b /* inside */ | expected /* should be removed */
95+
${0} | ${0} | ${0}
96+
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
97+
testCount += 1;
98+
expect(a + b).toBe(expected);
99+
});
100+
101+
test('test runs', () => {
102+
expect(testCount).toEqual(expectedTestCount);
103+
});
104+
});
105+
});
33106
});

packages/jest-each/src/bind.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,63 @@ const buildArrayTests = (title: string, table: Global.EachTable): EachTests => {
5757
return convertArrayTable(title, table as Global.ArrayTable);
5858
};
5959

60+
function filterTemplate(table: Global.EachTable, data: Global.TemplateData) {
61+
let sectionCount: number;
62+
63+
const result = table
64+
.join('')
65+
.trimLeft()
66+
.split('\n')
67+
.reduce(
68+
(
69+
acc: {headings: Array<string>; data: Global.TemplateData},
70+
line: string,
71+
index: number,
72+
) => {
73+
if (index === 0) {
74+
// remove /**/ comments
75+
line = line.replace(/\/\*(.*?)\*\//g, '');
76+
// remove // comments
77+
line = line.split('//')[0].trim();
78+
79+
const headings = getHeadingKeys(line);
80+
81+
sectionCount = headings.length;
82+
83+
return {...acc, headings};
84+
}
85+
86+
line = line.trim();
87+
88+
const isComment = line.startsWith('//') || line.startsWith('/*');
89+
90+
if (isComment === true) {
91+
return acc;
92+
}
93+
94+
const lastIndex = index * sectionCount;
95+
const firstIndex = lastIndex - (sectionCount - 1);
96+
const matchedData = data.slice(firstIndex - 1, lastIndex);
97+
98+
return {
99+
...acc,
100+
data: [...acc.data, ...matchedData],
101+
};
102+
},
103+
{data: [], headings: []},
104+
);
105+
106+
return result;
107+
}
108+
60109
const buildTemplateTests = (
61110
title: string,
62111
table: Global.EachTable,
63112
taggedTemplateData: Global.TemplateData,
64113
): EachTests => {
65-
const headings = getHeadingKeys(table[0] as string);
66-
validateTemplateTableHeadings(headings, taggedTemplateData);
67-
return convertTemplateTable(title, headings, taggedTemplateData);
114+
const {data, headings} = filterTemplate(table, taggedTemplateData);
115+
validateTemplateTableHeadings(headings, data);
116+
return convertTemplateTable(title, headings, data);
68117
};
69118

70119
const getHeadingKeys = (headings: string): Array<string> =>

0 commit comments

Comments
 (0)