Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.

Commit 47f0bf2

Browse files
committed
Use snapshots
Remove example output, the snapshots should be clear enough.
1 parent eb91896 commit 47f0bf2

File tree

6 files changed

+82
-189
lines changed

6 files changed

+82
-189
lines changed

example-output.md

Lines changed: 0 additions & 99 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"node": ">=4"
1414
},
1515
"scripts": {
16-
"test": "xo && nyc ava",
17-
"preversion": "WRITE_EXAMPLES=1 npm run test && git add example-output.md"
16+
"test": "xo && nyc ava"
1817
},
1918
"files": [
2019
"index.js"

readme.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@ The difficulty is that `t.throws(foo())` is allowed if `foo()` returns a promise
1717

1818
## Solution
1919

20-
See [`example-output.md`](example-output.md) for the transformation this plugin performs.
21-
22-
The example output can be generated by calling:
23-
24-
```
25-
$ WRITE_EXAMPLES=1 ava
26-
```
27-
28-
Contributors should not commit new examples. They will be regenerated as part of the release process.
29-
20+
See [`test.js.md`](test.js.md) for the transformation this plugin performs.
3021

3122
## License
3223

test.js

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import path from 'path';
2-
import fs from 'fs';
3-
import {serial as test} from 'ava';
1+
import test from 'ava';
42
import * as babel from '@babel/core';
53
import fn from './';
64

@@ -11,76 +9,18 @@ function transform(input) {
119
});
1210
}
1311

14-
const examples = [];
15-
16-
function addExample(input, output) {
17-
examples.push(
18-
'input:',
19-
'',
20-
'```js',
21-
input,
22-
'```',
23-
'',
24-
'becomes:',
25-
'',
26-
'```js',
27-
output,
28-
'```',
29-
'',
30-
'---',
31-
''
32-
);
33-
}
34-
35-
const HELPERS = `function _avaThrowsHelperStart(t, assertion, file, line) {
36-
if (t._throwsArgStart) {
37-
t._throwsArgStart(assertion, file, line);
38-
}
39-
}
40-
41-
function _avaThrowsHelperEnd(t, arg) {
42-
if (t._throwsArgEnd) {
43-
t._throwsArgEnd();
44-
}
45-
46-
return arg;
47-
}\n\n`;
48-
49-
function wrapped(throws, expression, line) {
50-
return `t.${throws}((_avaThrowsHelperStart(t, "${throws}", "some-file.js", ${line}), _avaThrowsHelperEnd(t, ${expression})));`;
51-
}
52-
53-
test('creates a helper', t => {
54-
const input = 't.throws(foo())';
55-
const {code} = transform(input);
56-
57-
t.is(code, HELPERS + wrapped('throws', 'foo()', 1));
58-
addExample(input, code);
59-
});
60-
61-
test('creates the helper only once', t => {
62-
const input = 't.throws(foo());\nt.throws(bar());';
12+
function snapshotTransform(t, input) {
6313
const {code} = transform(input);
14+
t.snapshot(code, input);
15+
}
6416

65-
t.is(code, HELPERS + wrapped('throws', 'foo()', 1) + '\n' + wrapped('throws', 'bar()', 2));
66-
addExample(input, code);
67-
});
17+
test('creates a helper', snapshotTransform, 't.throws(foo())');
6818

69-
test('does nothing if it does not match', t => {
70-
const input = 't.is(foo());';
71-
const {code} = transform(input);
19+
test('creates the helper only once', snapshotTransform, 't.throws(foo()); t.throws(bar());');
7220

73-
t.is(code, input);
74-
addExample(input, code);
75-
});
21+
test('does nothing if it does not match', snapshotTransform, 't.is(foo());');
7622

77-
test('helps notThrows', t => {
78-
const input = 't.notThrows(baz())';
79-
const {code} = transform(input);
80-
81-
t.is(code, HELPERS + wrapped('notThrows', 'baz()', 1));
82-
addExample(input, code);
83-
});
23+
test('helps notThrows', snapshotTransform, 't.notThrows(baz())');
8424

8525
test('does not throw on generated code', t => {
8626
t.notThrows(() => {
@@ -103,13 +43,3 @@ test('does not throw on generated code', t => {
10343
});
10444
});
10545
});
106-
107-
if (process.env.WRITE_EXAMPLES) {
108-
test('writing examples', t => {
109-
fs.writeFileSync(
110-
path.join(__dirname, 'example-output.md'),
111-
examples.join('\n')
112-
);
113-
t.pass();
114-
});
115-
}

test.js.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Snapshot report for `test.js`
2+
3+
The actual snapshot is saved in `test.js.snap`.
4+
5+
Generated by [AVA](https://ava.li).
6+
7+
## creates a helper
8+
9+
> t.throws(foo())
10+
11+
`function _avaThrowsHelperStart(t, assertion, file, line) {␊
12+
if (t._throwsArgStart) {␊
13+
t._throwsArgStart(assertion, file, line);␊
14+
}␊
15+
}␊
16+
17+
function _avaThrowsHelperEnd(t, arg) {␊
18+
if (t._throwsArgEnd) {␊
19+
t._throwsArgEnd();␊
20+
}␊
21+
22+
return arg;␊
23+
}␊
24+
25+
t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 1), _avaThrowsHelperEnd(t, foo())));`
26+
27+
## creates the helper only once
28+
29+
> t.throws(foo()); t.throws(bar());
30+
31+
`function _avaThrowsHelperStart(t, assertion, file, line) {␊
32+
if (t._throwsArgStart) {␊
33+
t._throwsArgStart(assertion, file, line);␊
34+
}␊
35+
}␊
36+
37+
function _avaThrowsHelperEnd(t, arg) {␊
38+
if (t._throwsArgEnd) {␊
39+
t._throwsArgEnd();␊
40+
}␊
41+
42+
return arg;␊
43+
}␊
44+
45+
t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 1), _avaThrowsHelperEnd(t, foo())));␊
46+
t.throws((_avaThrowsHelperStart(t, "throws", "some-file.js", 1), _avaThrowsHelperEnd(t, bar())));
47+
48+
## does nothing if it does not match
49+
50+
> t.is(foo());
51+
52+
't.is(foo());
53+
54+
## helps notThrows
55+
56+
> t.notThrows(baz())
57+
58+
`function _avaThrowsHelperStart(t, assertion, file, line) {␊
59+
if (t._throwsArgStart) {␊
60+
t._throwsArgStart(assertion, file, line);␊
61+
}␊
62+
}␊
63+
64+
function _avaThrowsHelperEnd(t, arg) {␊
65+
if (t._throwsArgEnd) {␊
66+
t._throwsArgEnd();␊
67+
}␊
68+
69+
return arg;␊
70+
}␊
71+
72+
t.notThrows((_avaThrowsHelperStart(t, "notThrows", "some-file.js", 1), _avaThrowsHelperEnd(t, baz())));`

test.js.snap

375 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)