Skip to content

Commit 04d8d0a

Browse files
committed
Normalize test titles
Issue #2774
1 parent 074373c commit 04d8d0a

File tree

10 files changed

+72
-1
lines changed

10 files changed

+72
-1
lines changed

lib/parse-test-args.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function parseTestArgs(args) {
2-
const rawTitle = typeof args[0] === 'string' ? args.shift() : undefined;
2+
const rawTitle = typeof args[0] === 'string' ? args.shift().trim().replace(/\s+/g, ' ') : undefined;
33
const receivedImplementationArray = Array.isArray(args[0]);
44
const implementations = receivedImplementationArray ? args.shift() : args.splice(0, 1);
55

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const test = require(process.env.TEST_AVA_IMPORT_FROM);
2+
3+
test(process.env.TEMPLATE ? 'test\r\n\ttitle' : ' test title ', t => {
4+
t.snapshot('Hello, World!');
5+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Snapshot report for `test.js`
2+
3+
The actual snapshot is saved in `test.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## test title
8+
9+
> Snapshot 1
10+
11+
'Hello, World!'
137 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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(`multiline try assertion title`, async t => {
11+
const firstTry = await t.try(`try assertions
12+
can have titles too`, (tt) => tt.pass());
13+
firstTry.commit();
14+
t.log(firstTry.title);
15+
});

test/snapshot-tests/formatting.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import test from '@ava/test';
55

66
import {cwd, fixture} from '../helpers/exec.js';
77
import {withTemporaryFixture} from '../helpers/with-temporary-fixture.js';
8+
import {beforeAndAfter} from '../snapshot-workflow/helpers/macros.js';
89

910
test('multiline snapshot label should be formatted correctly in the report', async t => {
1011
await withTemporaryFixture(cwd('multiline-snapshot-label'), async cwd => {
@@ -22,3 +23,28 @@ test('multiline snapshot label should be formatted correctly in the report', asy
2223
t.snapshot(report, 'resulting snapshot report');
2324
});
2425
});
26+
27+
test('test title should be normalized in stdout', async t => {
28+
await withTemporaryFixture(cwd('normalized-title-in-stdout'), async cwd => {
29+
// Run test fixture
30+
const result = await fixture(['--update-snapshots'], {
31+
cwd,
32+
env: {
33+
AVA_FORCE_CI: 'not-ci'
34+
}
35+
});
36+
37+
// Assert stdout is unchanged
38+
t.snapshot(result.stdout, 'stdout');
39+
});
40+
});
41+
42+
test(
43+
'test title should be normalized in snapshot',
44+
beforeAndAfter,
45+
{
46+
cwd: cwd('normalized-title-in-snapshots'),
47+
cli: ['--update-snapshots'],
48+
expectChanged: false
49+
}
50+
);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ 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+
√ multiline try assertion title␍␊
38+
i multiline try assertion title ─ try assertions can have titles too␍␊
39+
─␍␊
40+
␍␊
41+
2 tests passed`
182 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)