Skip to content

Commit bd0ef39

Browse files
Normalize test titles
Fixes #2774. Co-authored-by: Mark Wubben <[email protected]>
1 parent 4d3e220 commit bd0ef39

File tree

8 files changed

+117
-1
lines changed

8 files changed

+117
-1
lines changed

lib/parse-test-args.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
const normalize = title => typeof title === 'string' ? title.trim().replace(/\s+/g, ' ') : title;
2+
13
export default function parseTestArgs(args) {
24
const rawTitle = typeof args[0] === 'string' ? args.shift() : undefined;
35
const receivedImplementationArray = Array.isArray(args[0]);
46
const implementations = receivedImplementationArray ? args.shift() : args.splice(0, 1);
57

68
const buildTitle = implementation => {
7-
const title = implementation.title ? implementation.title(rawTitle, ...args) : rawTitle;
9+
const title = normalize(implementation.title ? implementation.title(rawTitle, ...args) : rawTitle);
810
return {title, isSet: typeof title !== 'undefined', isValid: typeof title === 'string', isEmpty: !title};
911
};
1012

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const test = require(process.env.TEST_AVA_IMPORT_FROM);
2+
3+
test('test\r\n\ttitle', t => {
4+
t.snapshot('Hello, World!');
5+
});
6+
7+
test('test\r\n\ttitle', Object.assign(t => {
8+
t.snapshot('Hello, World!');
9+
}, {
10+
title: title => `macro\n${title}`
11+
}));
12+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const test = require(process.env.TEST_AVA_IMPORT_FROM);
2+
3+
test(`a rather wordy test title that is wrapped
4+
to meet line length requirements in
5+
an unconventional way that may interfere
6+
with report formatting `, t => {
7+
t.pass();
8+
});
9+
10+
test('test\r\n\ttitle', Object.assign(t => {
11+
t.pass();
12+
}, {
13+
title: title => title
14+
}));
15+
16+
test('multiline try assertion title', async t => {
17+
const firstTry = await t.try(`try assertions
18+
can have titles too`, tt => tt.pass());
19+
firstTry.commit();
20+
t.log(firstTry.title);
21+
});

test/snapshot-tests/formatting.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import fs from 'fs';
2+
import os from 'os';
23
import path from 'path';
34

45
import test from '@ava/test';
6+
import figures from 'figures';
7+
import replaceString from 'replace-string';
58

69
import {cwd, fixture} from '../helpers/exec.js';
710
import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js';
@@ -22,3 +25,43 @@ test('multiline snapshot label should be formatted correctly in the report', asy
2225
t.snapshot(report, 'resulting snapshot report');
2326
});
2427
});
28+
29+
test('test title should be normalized in stdout', async t => {
30+
await withTemporaryFixture(cwd('normalized-title-in-stdout'), async cwd => {
31+
// Run test fixture
32+
const result = await fixture(['--update-snapshots'], {
33+
cwd,
34+
env: {
35+
AVA_FORCE_CI: 'not-ci'
36+
}
37+
});
38+
39+
// Assert stdout is unchanged
40+
t.snapshot(
41+
replaceString(
42+
replaceString(
43+
replaceString(result.stdout, os.EOL, '\n'),
44+
figures.main.info, figures.windows.info
45+
),
46+
figures.main.tick, figures.windows.tick
47+
),
48+
'stdout');
49+
});
50+
});
51+
52+
test('test title should be normalized in snapshot', async t => {
53+
await withTemporaryFixture(cwd('normalized-title-in-snapshots'), async cwd => {
54+
// Run test fixture
55+
await fixture(['--update-snapshots'], {
56+
cwd,
57+
env: {
58+
AVA_FORCE_CI: 'not-ci'
59+
}
60+
});
61+
62+
// Assert report is unchanged
63+
const reportPath = path.join(cwd, 'test.js.md');
64+
const report = fs.readFileSync(reportPath, {encoding: 'utf8'});
65+
t.snapshot(report, 'resulting snapshot report');
66+
});
67+
});

test/snapshot-tests/snapshots/formatting.js.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,39 @@ Generated by [AVA](https://avajs.dev).
2727
2828
'Hello, World!'␊
2929
`
30+
31+
## test title should be normalized in stdout
32+
33+
> stdout
34+
35+
`␊
36+
√ a rather wordy test title that is wrapped to meet line length requirements in an unconventional way that may interfere with report formatting␊
37+
√ test title␊
38+
√ multiline try assertion title␊
39+
i multiline try assertion title ─ try assertions can have titles too␊
40+
─␊
41+
42+
3 tests passed`
43+
44+
## test title should be normalized in snapshot
45+
46+
> resulting snapshot report
47+
48+
`# Snapshot report for \`test.js\`␊
49+
50+
The actual snapshot is saved in \`test.js.snap\`.␊
51+
52+
Generated by [AVA](https://avajs.dev).␊
53+
54+
## test title␊
55+
56+
> Snapshot 1␊
57+
58+
'Hello, World!'␊
59+
60+
## macro test title␊
61+
62+
> Snapshot 1␊
63+
64+
'Hello, World!'␊
65+
`
239 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)