Skip to content

Commit

Permalink
chore(jest-each): normalize new lines for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Jul 25, 2019
1 parent 9084bc4 commit 2415560
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 23 additions & 0 deletions packages/jest-each/src/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,29 @@ describe('jest-each', () => {
]);
});

test('removes comments with windows new line characters "\\r\\n"', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(
globalTestMocks,
)`\r\na | b | expected\r\n// ${1} | ${1} | ${0}\r\n${1} | ${1} | ${2}\r\n/* ${1} | ${1} | ${5} */\r\n${2} | ${2} | ${4}\r\n`;
const testFunction = get(eachObject, keyPath);
testFunction('expected string: a=$a, b=$b, expected=$expected', noop);

const globalMock = get(globalTestMocks, keyPath);
expect(globalMock.mock.calls).toEqual([
[
'expected string: a=1, b=1, expected=2',
expect.any(Function),
undefined,
],
[
'expected string: a=2, b=2, expected=4',
expect.any(Function),
undefined,
],
]);
});

test('removes commented out tests and allow spaces between', () => {
const globalTestMocks = getGlobalTestMocks();
const eachObject = each.withGlobal(globalTestMocks)`
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-each/src/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ function filterTemplate(
) {
let multipleLineCommentCount: number = 0;
let isSingleLineComment: boolean = false;
let skipNext: boolean = false;

function removeCommentsFromLine(line: string): string {
let skipNext: boolean = false;

const result = line
.split('')
.reduce((acc: Array<string>, character, index, array) => {
Expand Down Expand Up @@ -137,7 +138,8 @@ function filterTemplate(
// remove excess space from all lines
.map(line =>
line
.split('\n')
// https://stackoverflow.com/a/52947649
.split(/\r\n|\r|\n/)
.map(subLine => subLine.trim())
.join('\n'),
)
Expand Down

0 comments on commit 2415560

Please sign in to comment.