Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose internal events for custom reporters via config #3247

Merged
merged 18 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,24 @@ export default async function loadCli() { // eslint-disable-line complexity
});
}

api.on('run', plan => {
api.on('run', async plan => {
if (combined.onInternalEvent) {
await combined.onInternalEvent({
codetheweb marked this conversation as resolved.
Show resolved Hide resolved
type: 'run',
plan,
});
}

reporter.startRun(plan);

plan.status.on('stateChange', evt => {
plan.status.on('stateChange', async evt => {
if (combined.onInternalEvent) {
await combined.onInternalEvent({
type: 'stateChange',
stateChange: evt,
});
}

if (evt.type === 'end' || evt.type === 'interrupt') {
// Write out code coverage data when the run ends, lest a process
// interrupt causes it to be lost.
Expand Down
1 change: 1 addition & 0 deletions test/internal-events/fixtures/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
internal-events.json
16 changes: 16 additions & 0 deletions test/internal-events/fixtures/ava.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'node:fs/promises';

const internalEvents = [];

export default {
files: [
'test.js',
],
async onInternalEvent(event) {
codetheweb marked this conversation as resolved.
Show resolved Hide resolved
internalEvents.push(event);

if (event.type === 'stateChange' && event.stateChange.type === 'end') {
await fs.writeFile('internal-events.json', JSON.stringify(internalEvents));
}
},
};
3 changes: 3 additions & 0 deletions test/internal-events/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
5 changes: 5 additions & 0 deletions test/internal-events/fixtures/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from 'ava';

test('placeholder', t => {
t.pass();
});
38 changes: 38 additions & 0 deletions test/internal-events/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'node:fs/promises';
import {fileURLToPath} from 'node:url';

import test from '@ava/test';

import {fixture} from '../helpers/exec.js';

test('internal events are emitted', async t => {
await fixture();

const result = JSON.parse(await fs.readFile(fileURLToPath(new URL('fixtures/internal-events.json', import.meta.url))));

t.like(result[0], {
type: 'run',
plan: {
files: [
fileURLToPath(new URL('fixtures/test.js', import.meta.url)),
],
},
});

const testPassedEvent = result.find(event => event.type === 'stateChange' && event.stateChange.type === 'test-passed');
t.like(testPassedEvent, {
type: 'stateChange',
stateChange: {
type: 'test-passed',
title: 'placeholder',
testFile: fileURLToPath(new URL('fixtures/test.js', import.meta.url)),
},
});

t.like(result[result.length - 1], {
type: 'stateChange',
stateChange: {
type: 'end',
},
});
});