Skip to content

Commit

Permalink
feature(jest-each): Enable comments inside .each template literal table
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Jul 18, 2019
1 parent bd76829 commit 0198f3e
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 3 deletions.
73 changes: 73 additions & 0 deletions packages/jest-each/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,77 @@ describe('template', () => {
expect(a + b).toBe(expected);
});
});

describe('comments', () => {
describe('removes commented out tests', () => {
let testCount = 0;
const expectedTestCount = 2;

each`
a | b | expected
// ${1} | ${1} | ${0}
${1} | ${1} | ${2}
/* ${1} | ${1} | ${5} */
${1} | ${1} | ${2}
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
testCount += 1;
expect(a + b).toBe(expected);
});

test('test runs', () => {
expect(testCount).toEqual(expectedTestCount);
});
});

describe('removes trailing comments', () => {
let testCount = 0;
const expectedTestCount = 2;

each`
a | b | expected
${0} | ${0} | ${0} // ignores trailing comment
${1} | ${1} | ${2} /* ignores second comment */
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
testCount += 1;
expect(a + b).toBe(expected);
});

test('test runs', () => {
expect(testCount).toEqual(expectedTestCount);
});
});

describe('removes trailing comments in title', () => {
let testCount = 0;
const expectedTestCount = 1;

each`
a | b | expected // should be removed
${0} | ${0} | ${0}
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
testCount += 1;
expect(a + b).toBe(expected);
});

test('test runs', () => {
expect(testCount).toEqual(expectedTestCount);
});
});

describe('removes /**/ comments title', () => {
let testCount = 0;
const expectedTestCount = 1;
each`
a | b /* inside */ | expected /* should be removed */
${0} | ${0} | ${0}
`.test('returns $expected when given $a and $b', ({a, b, expected}) => {
testCount += 1;
expect(a + b).toBe(expected);
});

test('test runs', () => {
expect(testCount).toEqual(expectedTestCount);
});
});
});
});
55 changes: 52 additions & 3 deletions packages/jest-each/src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,63 @@ const buildArrayTests = (title: string, table: Global.EachTable): EachTests => {
return convertArrayTable(title, table as Global.ArrayTable);
};

function filterTemplate(table: Global.EachTable, data: Global.TemplateData) {
let sectionCount: number;

const result = table
.join('')
.trimLeft()
.split('\n')
.reduce(
(
acc: {headings: Array<string>; data: Global.TemplateData},
line: string,
index: number,
) => {
if (index === 0) {
// remove /**/ comments
line = line.replace(/\/\*(.*?)\*\//g, '');
// remove // comments
line = line.split('//')[0].trim();

const headings = getHeadingKeys(line);

sectionCount = headings.length;

return {...acc, headings};
}

line = line.trim();

const isComment = line.startsWith('//') || line.startsWith('/*');

if (isComment === true) {
return acc;
}

const lastIndex = index * sectionCount;
const firstIndex = lastIndex - (sectionCount - 1);
const matchedData = data.slice(firstIndex - 1, lastIndex);

return {
...acc,
data: [...acc.data, ...matchedData],
};
},
{data: [], headings: []},
);

return result;
}

const buildTemplateTests = (
title: string,
table: Global.EachTable,
taggedTemplateData: Global.TemplateData,
): EachTests => {
const headings = getHeadingKeys(table[0] as string);
validateTemplateTableHeadings(headings, taggedTemplateData);
return convertTemplateTable(title, headings, taggedTemplateData);
const {data, headings} = filterTemplate(table, taggedTemplateData);
validateTemplateTableHeadings(headings, data);
return convertTemplateTable(title, headings, data);
};

const getHeadingKeys = (headings: string): Array<string> =>
Expand Down

0 comments on commit 0198f3e

Please sign in to comment.