-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
novemberborn
merged 18 commits into
avajs:main
from
codetheweb:feat-expose-internal-events
Nov 26, 2023
+229
−1
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
041a41c
Expose internal events for custom reporters
codetheweb 84dba1d
Address comment from review
codetheweb f174798
Use async generator for events
codetheweb 74eee67
Fix lint issues
codetheweb 4d55422
Merge branch 'main' into feat-expose-internal-events
codetheweb 9296944
No need for class
codetheweb 7b0c55b
Gate behind nonSemVerExperiments
codetheweb 78f9d37
Add types
codetheweb 18f0cd3
Remove unnecssary async changes
codetheweb b7c8053
Rename Run -> ObservedRun
codetheweb 1d4de13
Clean up exports, only export types/ESM
codetheweb e15de69
End iterator when run is finished
codetheweb f10831b
State change -> event
codetheweb fdca2ed
Cause error in test
codetheweb a9eac47
Fix test
codetheweb 96e6ca5
Merge branch 'main' into feat-expose-internal-events
codetheweb ecef15a
Fix lint issues
codetheweb 11d9049
Rename type
novemberborn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type {StateChangeEvent} from '../types/state-change-events.d'; | ||
|
||
export type Event = StateChangeEvent; | ||
|
||
export type ObservedRun = { | ||
events: AsyncIterableIterator<Event>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export async function * asyncEventIteratorFromApi(api) { | ||
// TODO: support multiple runs (watch mode) | ||
const {value: plan} = await api.events('run').next(); | ||
|
||
for await (const stateChange of plan.status.events('stateChange')) { | ||
yield stateChange; | ||
|
||
if (stateChange.type === 'end' || stateChange.type === 'interrupt') { | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
internal-events.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'node:fs/promises'; | ||
|
||
const internalEvents = []; | ||
|
||
export default { | ||
files: [ | ||
'test.js', | ||
], | ||
nonSemVerExperiments: { | ||
observeRunsFromConfig: true, | ||
}, | ||
async observeRun(run) { | ||
for await (const event of run.events) { | ||
internalEvents.push(event); | ||
} | ||
|
||
await fs.writeFile('internal-events.json', JSON.stringify(internalEvents)); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import test from 'ava'; | ||
|
||
test('placeholder', t => { | ||
t.pass(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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: 'starting', | ||
testFile: fileURLToPath(new URL('fixtures/test.js', import.meta.url)), | ||
}); | ||
|
||
const testPassedEvent = result.find(event => event.type === 'test-passed'); | ||
t.like(testPassedEvent, { | ||
type: 'test-passed', | ||
title: 'placeholder', | ||
testFile: fileURLToPath(new URL('fixtures/test.js', import.meta.url)), | ||
}); | ||
|
||
t.like(result.at(-1), { | ||
type: 'end', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
type ErrorSource = { | ||
isDependency: boolean; | ||
isWithinProject: boolean; | ||
file: string; | ||
line: number; | ||
}; | ||
|
||
type SerializedErrorBase = { | ||
message: string; | ||
name: string; | ||
originalError: unknown; | ||
stack: string; | ||
}; | ||
|
||
type AggregateSerializedError = SerializedErrorBase & { | ||
type: 'aggregate'; | ||
errors: SerializedError[]; | ||
}; | ||
|
||
type NativeSerializedError = SerializedErrorBase & { | ||
type: 'native'; | ||
source: ErrorSource | undefined; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
type AVASerializedError = SerializedErrorBase & { | ||
type: 'ava'; | ||
assertion: string; | ||
improperUsage: unknown | undefined; | ||
formattedCause: unknown | undefined; | ||
formattedDetails: unknown | unknown[]; | ||
source: ErrorSource | undefined; | ||
}; | ||
|
||
type SerializedError = AggregateSerializedError | NativeSerializedError | AVASerializedError; | ||
|
||
export type StateChangeEvent = { | ||
type: 'starting'; | ||
testFile: string; | ||
} | { | ||
type: 'stats'; | ||
stats: { | ||
byFile: Map<string, { | ||
declaredTests: number; | ||
failedHooks: number; | ||
failedTests: number; | ||
internalErrors: number; | ||
remainingTests: number; | ||
passedKnownFailingTests: number; | ||
passedTests: number; | ||
selectedTests: number; | ||
selectingLines: boolean; | ||
skippedTests: number; | ||
todoTests: number; | ||
uncaughtExceptions: number; | ||
unhandledRejections: number; | ||
}>; | ||
declaredTests: number; | ||
failedHooks: number; | ||
failedTests: number; | ||
failedWorkers: number; | ||
files: number; | ||
parallelRuns: { | ||
currentIndex: number; | ||
totalRuns: number; | ||
} | undefined; | ||
finishedWorkers: number; | ||
internalErrors: number; | ||
remainingTests: number; | ||
passedKnownFailingTests: number; | ||
passedTests: number; | ||
selectedTests: number; | ||
sharedWorkerErrors: number; | ||
skippedTests: number; | ||
timedOutTests: number; | ||
timeouts: number; | ||
todoTests: number; | ||
uncaughtExceptions: number; | ||
unhandledRejections: number; | ||
}; | ||
} | { | ||
type: 'declared-test'; | ||
title: string; | ||
knownFailing: boolean; | ||
todo: boolean; | ||
testFile: string; | ||
} | { | ||
type: 'selected-test'; | ||
title: string; | ||
knownFailing: boolean; | ||
skip: boolean; | ||
todo: boolean; | ||
testFile: string; | ||
} | { | ||
type: 'test-register-log-reference'; | ||
title: string; | ||
logs: string[]; | ||
testFile: string; | ||
} | { | ||
type: 'test-passed'; | ||
title: string; | ||
duration: number; | ||
knownFailing: boolean; | ||
logs: string[]; | ||
testFile: string; | ||
} | { | ||
type: 'test-failed'; | ||
title: string; | ||
err: SerializedError; | ||
duration: number; | ||
knownFailing: boolean; | ||
logs: string[]; | ||
testFile: string; | ||
} | { | ||
type: 'worker-finished'; | ||
forcedExit: boolean; | ||
testFile: string; | ||
} | { | ||
type: 'worker-failed'; | ||
nonZeroExitCode?: boolean; | ||
signal?: string; | ||
err?: SerializedError; | ||
} | { | ||
type: 'touched-files'; | ||
files: { | ||
changedFiles: string[]; | ||
temporaryFiles: string[]; | ||
}; | ||
} | { | ||
type: 'worker-stdout'; | ||
chunk: Uint8Array; | ||
testFile: string; | ||
} | { | ||
type: 'worker-stderr'; | ||
chunk: Uint8Array; | ||
testFile: string; | ||
} | { | ||
type: 'timeout'; | ||
period: number; | ||
pendingTests: Map<string, Set<string>>; | ||
} | ||
| { | ||
type: 'end'; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a new rule started complaining about this, ava is always stylized AVA so I think it's ok to disable here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to stick with the rule here. Will push up a commit.