Skip to content

Commit c8f31a3

Browse files
committed
Test AVA using AVA
1 parent a5385a4 commit c8f31a3

18 files changed

+259
-5
lines changed

.c8rc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"all": true,
33
"exclude": [
4-
"{coverage,media,test-d,test-tap}/**",
4+
"{coverage,media,test,test-d,test-tap}/**",
55
"*.config.js",
66
"index.d.ts"
77
],

.github/CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Try and avoid making breaking changes. Those take more time to ship. Instead mak
5959

6060
Non-experimental features should be accompanied with tests and documentation.
6161

62-
Don't include unrelated changes in your pull request. Make sure tests pass on your machine by running `npm test`. You can run specific test files as well using `npx tap test-tap/{file}.js`.
62+
Don't include unrelated changes in your pull request. Make sure tests pass on your machine by running `npm test`. You can run specific test files as well using `npx tap test-tap/{file}.js` or `npx test-ava test/{file}.js`.
6363

6464
When you make a pull request please use a clear and descriptive title. Be specific about what's changed and why.
6565

.gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
node_modules
2-
coverage
1+
/coverage
2+
/media/**/node_modules/
3+
/node_modules/
4+
/test-tap/**/node_modules/
5+
/test/**/fixtures/**/node_modules/*/

ava.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
files: ['test/**', '!test/**/{fixtures,helpers}/**'],
3+
ignoredByWatcher: ['{coverage,docs,media,test-d,test-tap}/**']
4+
};

lib/cli.js

+13
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,19 @@ exports.run = async () => { // eslint-disable-line complexity
410410
api.on('run', plan => {
411411
reporter.startRun(plan);
412412

413+
if (process.env.AVA_EMIT_RUN_STATUS_OVER_IPC === 'I\'ll find a payphone baby / Take some time to talk to you') {
414+
if (process.versions.node >= '12.16.0') {
415+
plan.status.on('stateChange', evt => {
416+
process.send(evt);
417+
});
418+
} else {
419+
const v8 = require('v8');
420+
plan.status.on('stateChange', evt => {
421+
process.send([...v8.serialize(evt)]);
422+
});
423+
}
424+
}
425+
413426
plan.status.on('stateChange', evt => {
414427
if (evt.type === 'interrupt') {
415428
reporter.endRun();

maintaining.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* `npm test`: Lint the code and run the entire test suite with coverage.
1010
* `npx tap test-tap/fork.js --bail`: Run a specific test file and bail on the first failure (useful when hunting bugs).
11+
* `npx test-ava test/{file}.js`: Run self-hosted tests.
1112

1213
## CI
1314

package-lock.json

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"node": ">=10.18.0 <11 || >=12.14.0 <13 || >=13.5.0 <14 || >=14.0.0"
1111
},
1212
"scripts": {
13-
"test": "xo && tsd && c8 tap"
13+
"test": "xo && tsd && c8 --report=none tap && c8 --report=none --no-clean test-ava && c8 report"
1414
},
1515
"files": [
1616
"lib",
@@ -113,6 +113,7 @@
113113
},
114114
"devDependencies": {
115115
"@ava/babel": "^1.0.1",
116+
"@ava/test": "github:avajs/test",
116117
"@babel/plugin-proposal-do-expressions": "^7.8.3",
117118
"@sinonjs/fake-timers": "^6.0.1",
118119
"ansi-escapes": "^4.3.1",

test/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Self-hosted tests
2+
3+
This directory contains tests that are run using a stable version of AVA. You can run them using `npx test-ava`.
4+
5+
Tests should be placed in their own directory, grouped by area of responsibility. Use the `exec.fixture()` helper to launch the AVA version that is in the repository to run tests. Place these in a nested `fixtures` directory. Add a relative dependency in `package.json`. You can then import from `ava`.
6+
7+
Prefer snapshotting the test results.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const test = require('ava');
2+
3+
const passes = async (t, assertion, ...args) => {
4+
await t[assertion](...args);
5+
};
6+
7+
passes.title = (_, assertion, ...args) => `t.${assertion}(${args.map(v => JSON.stringify(v)).join(', ')}) passes`;
8+
9+
test(passes, 'pass');
10+
test(passes, 'is', 1, 1);
11+
test(passes, 'not', 1, 2);
12+
test(passes, 'deepEqual', {foo: 'bar'}, {foo: 'bar'});
13+
test(passes, 'notDeepEqual', {foo: 'bar'}, {foo: 'baz'});
14+
test(passes, 'throws', () => {
15+
throw new Error('error');
16+
});
17+
test(passes, 'throwsAsync', async () => {
18+
throw new Error('error');
19+
});
20+
test(passes, 'notThrows', () => {});
21+
test(passes, 'notThrowsAsync', async () => {});
22+
test(passes, 'snapshot', {foo: 'bar'});
23+
test(passes, 'truthy', 1);
24+
test(passes, 'falsy', '');
25+
test(passes, 'true', true);
26+
test(passes, 'false', false);
27+
test(passes, 'regex', 'foo', /foo/);
28+
test(passes, 'notRegex', 'bar', /foo/);
29+
test(passes, 'assert', 1);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Snapshot report for `happy-path.js`
2+
3+
The actual snapshot is saved in `happy-path.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## t.snapshot({"foo":"bar"}) passes
8+
9+
> Snapshot 1
10+
11+
{
12+
foo: 'bar',
13+
}
144 Bytes
Binary file not shown.

test/assertions/fixtures/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ava": {
3+
"files": [
4+
"*.js"
5+
]
6+
},
7+
"dependencies": {
8+
"ava": "file:../../.."
9+
}
10+
}

test/assertions/snapshots/test.js.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Snapshot report for `test/assertions/test.js`
2+
3+
The actual snapshot is saved in `test.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## happy path
8+
9+
> Snapshot 1
10+
11+
[
12+
't.assert(1) passes',
13+
't.deepEqual({"foo":"bar"}, {"foo":"bar"}) passes',
14+
't.false(false) passes',
15+
't.falsy("") passes',
16+
't.is(1, 1) passes',
17+
't.not(1, 2) passes',
18+
't.notDeepEqual({"foo":"bar"}, {"foo":"baz"}) passes',
19+
't.notRegex("bar", {}) passes',
20+
't.notThrows() passes',
21+
't.notThrowsAsync() passes',
22+
't.pass() passes',
23+
't.regex("foo", {}) passes',
24+
't.snapshot({"foo":"bar"}) passes',
25+
't.throws() passes',
26+
't.throwsAsync() passes',
27+
't.true(true) passes',
28+
't.truthy(1) passes',
29+
]
415 Bytes
Binary file not shown.

test/assertions/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const test = require('@ava/test');
2+
const exec = require('../helpers/exec');
3+
4+
test('happy path', async t => {
5+
const result = await exec.fixture('happy-path.js');
6+
t.snapshot(result.stats.passed.map(({title}) => title));
7+
});

test/helpers/exec.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const path = require('path');
2+
const v8 = require('v8');
3+
4+
const test = require('@ava/test');
5+
const execa = require('execa');
6+
7+
const cliPath = path.resolve(__dirname, '../../cli.js');
8+
const serialization = process.versions.node >= '12.16.0' ? 'advanced' : 'json';
9+
10+
exports.fixture = async (...args) => {
11+
const cwd = path.join(path.dirname(test.meta.file), 'fixtures');
12+
const running = execa.node(cliPath, args, {
13+
env: {
14+
AVA_EMIT_RUN_STATUS_OVER_IPC: 'I\'ll find a payphone baby / Take some time to talk to you'
15+
},
16+
cwd,
17+
serialization
18+
});
19+
20+
const stats = {
21+
passed: []
22+
};
23+
24+
running.on('message', message => {
25+
if (serialization === 'json') {
26+
message = v8.deserialize(Uint8Array.from(message));
27+
}
28+
29+
switch (message.type) {
30+
case 'test-passed': {
31+
const {title, testFile} = message;
32+
stats.passed.push({title, file: path.posix.relative(cwd, testFile)});
33+
break;
34+
}
35+
36+
default:
37+
break;
38+
}
39+
});
40+
41+
try {
42+
return {
43+
stats,
44+
...await running
45+
};
46+
} catch (error) {
47+
throw Object.assign(error, {stats});
48+
} finally {
49+
stats.passed.sort((a, b) => {
50+
if (a.file < b.file) {
51+
return -1;
52+
}
53+
54+
if (a.file > b.file) {
55+
return 1;
56+
}
57+
58+
if (a.title < b.title) {
59+
return -1;
60+
}
61+
62+
return 1;
63+
});
64+
}
65+
};

test/node_modules/ava

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)