Skip to content
Permalink
Browse files
Test AVA using AVA
  • Loading branch information
novemberborn committed May 5, 2020
1 parent a5385a4 commit c8f31a39ca7a2b13a5b3c85c5b1467da2ecc5c7a
Showing 18 changed files with 259 additions and 5 deletions.
@@ -1,7 +1,7 @@
{
"all": true,
"exclude": [
"{coverage,media,test-d,test-tap}/**",
"{coverage,media,test,test-d,test-tap}/**",
"*.config.js",
"index.d.ts"
],
@@ -59,7 +59,7 @@ Try and avoid making breaking changes. Those take more time to ship. Instead mak

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

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`.
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`.

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

@@ -1,2 +1,5 @@
node_modules
coverage
/coverage
/media/**/node_modules/
/node_modules/
/test-tap/**/node_modules/
/test/**/fixtures/**/node_modules/*/
@@ -0,0 +1,4 @@
export default {
files: ['test/**', '!test/**/{fixtures,helpers}/**'],
ignoredByWatcher: ['{coverage,docs,media,test-d,test-tap}/**']
};
@@ -410,6 +410,19 @@ exports.run = async () => { // eslint-disable-line complexity
api.on('run', plan => {
reporter.startRun(plan);

if (process.env.AVA_EMIT_RUN_STATUS_OVER_IPC === 'I\'ll find a payphone baby / Take some time to talk to you') {
if (process.versions.node >= '12.16.0') {
plan.status.on('stateChange', evt => {
process.send(evt);
});
} else {
const v8 = require('v8');
plan.status.on('stateChange', evt => {
process.send([...v8.serialize(evt)]);
});
}
}

plan.status.on('stateChange', evt => {
if (evt.type === 'interrupt') {
reporter.endRun();
@@ -8,6 +8,7 @@

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

## CI

Some generated files are not rendered by default. Learn more.

@@ -10,7 +10,7 @@
"node": ">=10.18.0 <11 || >=12.14.0 <13 || >=13.5.0 <14 || >=14.0.0"
},
"scripts": {
"test": "xo && tsd && c8 tap"
"test": "xo && tsd && c8 --report=none tap && c8 --report=none --no-clean test-ava && c8 report"
},
"files": [
"lib",
@@ -113,6 +113,7 @@
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@ava/test": "github:avajs/test",
"@babel/plugin-proposal-do-expressions": "^7.8.3",
"@sinonjs/fake-timers": "^6.0.1",
"ansi-escapes": "^4.3.1",
@@ -0,0 +1,7 @@
# Self-hosted tests

This directory contains tests that are run using a stable version of AVA. You can run them using `npx test-ava`.

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`.

Prefer snapshotting the test results.
@@ -0,0 +1,29 @@
const test = require('ava');

const passes = async (t, assertion, ...args) => {
await t[assertion](...args);
};

passes.title = (_, assertion, ...args) => `t.${assertion}(${args.map(v => JSON.stringify(v)).join(', ')}) passes`;

test(passes, 'pass');
test(passes, 'is', 1, 1);
test(passes, 'not', 1, 2);
test(passes, 'deepEqual', {foo: 'bar'}, {foo: 'bar'});
test(passes, 'notDeepEqual', {foo: 'bar'}, {foo: 'baz'});
test(passes, 'throws', () => {
throw new Error('error');
});
test(passes, 'throwsAsync', async () => {
throw new Error('error');
});
test(passes, 'notThrows', () => {});
test(passes, 'notThrowsAsync', async () => {});
test(passes, 'snapshot', {foo: 'bar'});
test(passes, 'truthy', 1);
test(passes, 'falsy', '');
test(passes, 'true', true);
test(passes, 'false', false);
test(passes, 'regex', 'foo', /foo/);
test(passes, 'notRegex', 'bar', /foo/);
test(passes, 'assert', 1);
@@ -0,0 +1,13 @@
# Snapshot report for `happy-path.js`

The actual snapshot is saved in `happy-path.js.snap`.

Generated by [AVA](https://avajs.dev).

## t.snapshot({"foo":"bar"}) passes

> Snapshot 1
{
foo: 'bar',
}
Binary file not shown.
@@ -0,0 +1,10 @@
{
"ava": {
"files": [
"*.js"
]
},
"dependencies": {
"ava": "file:../../.."
}
}
@@ -0,0 +1,29 @@
# Snapshot report for `test/assertions/test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://avajs.dev).

## happy path

> Snapshot 1
[
't.assert(1) passes',
't.deepEqual({"foo":"bar"}, {"foo":"bar"}) passes',
't.false(false) passes',
't.falsy("") passes',
't.is(1, 1) passes',
't.not(1, 2) passes',
't.notDeepEqual({"foo":"bar"}, {"foo":"baz"}) passes',
't.notRegex("bar", {}) passes',
't.notThrows() passes',
't.notThrowsAsync() passes',
't.pass() passes',
't.regex("foo", {}) passes',
't.snapshot({"foo":"bar"}) passes',
't.throws() passes',
't.throwsAsync() passes',
't.true(true) passes',
't.truthy(1) passes',
]
Binary file not shown.
@@ -0,0 +1,7 @@
const test = require('@ava/test');
const exec = require('../helpers/exec');

test('happy path', async t => {
const result = await exec.fixture('happy-path.js');
t.snapshot(result.stats.passed.map(({title}) => title));
});
@@ -0,0 +1,65 @@
const path = require('path');
const v8 = require('v8');

const test = require('@ava/test');
const execa = require('execa');

const cliPath = path.resolve(__dirname, '../../cli.js');
const serialization = process.versions.node >= '12.16.0' ? 'advanced' : 'json';

exports.fixture = async (...args) => {
const cwd = path.join(path.dirname(test.meta.file), 'fixtures');
const running = execa.node(cliPath, args, {
env: {
AVA_EMIT_RUN_STATUS_OVER_IPC: 'I\'ll find a payphone baby / Take some time to talk to you'
},
cwd,
serialization
});

const stats = {
passed: []
};

running.on('message', message => {
if (serialization === 'json') {
message = v8.deserialize(Uint8Array.from(message));
}

switch (message.type) {
case 'test-passed': {
const {title, testFile} = message;
stats.passed.push({title, file: path.posix.relative(cwd, testFile)});
break;
}

default:
break;
}
});

try {
return {
stats,
...await running
};
} catch (error) {
throw Object.assign(error, {stats});
} finally {
stats.passed.sort((a, b) => {
if (a.file < b.file) {
return -1;
}

if (a.file > b.file) {
return 1;
}

if (a.title < b.title) {
return -1;
}

return 1;
});
}
};

Some generated files are not rendered by default. Learn more.

0 comments on commit c8f31a3

Please sign in to comment.