-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use IPC for granular test reporting (#43)
Resolves #26
- Loading branch information
1 parent
7e9d0ea
commit 84fda0b
Showing
15 changed files
with
492 additions
and
114 deletions.
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
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
/** @type {import('../src/types').JestVSCodeRunnerOptions} */ | ||
/** @type {import('../src/types').RunnerOptions} */ | ||
const config = { | ||
version: '1.56.2', | ||
launchArgs: ['--disable-extensions'], | ||
filterOutput: true, | ||
} | ||
|
||
module.exports = config |
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,88 @@ | ||
import type { | ||
AggregatedResult, | ||
TestCaseResult, | ||
TestResult, | ||
} from '@jest/test-result' | ||
import type { | ||
ReporterOnStartOptions, | ||
Test, | ||
Reporter, | ||
Context, | ||
} from '@jest/reporters' | ||
import IPCClient from './ipc-client' | ||
import wrapIO from './wrap-io' | ||
|
||
export default class ChildReporter implements Reporter { | ||
#ipc: IPCClient | ||
#onConnected: Promise<void> | ||
|
||
constructor() { | ||
this.#ipc = new IPCClient('reporter') | ||
this.#onConnected = this.#ipc.connect() | ||
|
||
wrapIO(this.#ipc) | ||
} | ||
|
||
getLastError() { | ||
return undefined | ||
} | ||
|
||
onTestResult( | ||
test: Test, | ||
testResult: TestResult, | ||
aggregatedResult: AggregatedResult | ||
): void { | ||
this.#ipc.emit('testResult', { | ||
test, | ||
testResult, | ||
aggregatedResult, | ||
}) | ||
} | ||
|
||
onTestFileResult( | ||
test: Test, | ||
testResult: TestResult, | ||
aggregatedResult: AggregatedResult | ||
): void { | ||
this.#ipc.emit('testFileResult', { | ||
test, | ||
testResult, | ||
aggregatedResult, | ||
}) | ||
} | ||
|
||
onTestCaseResult(test: Test, testCaseResult: TestCaseResult): void { | ||
this.#ipc.emit('testCaseResult', { | ||
test, | ||
testCaseResult, | ||
}) | ||
} | ||
|
||
onRunStart( | ||
aggregatedResult: AggregatedResult, | ||
options: ReporterOnStartOptions | ||
): void { | ||
this.#ipc.emit('runStart', { | ||
aggregatedResult, | ||
options, | ||
}) | ||
} | ||
|
||
onTestStart(test: Test): void { | ||
this.#ipc.emit('testStart', { test }) | ||
} | ||
|
||
onTestFileStart(test: Test): void { | ||
this.#ipc.emit('testFileStart', { test }) | ||
} | ||
|
||
async onRunComplete( | ||
contexts: Set<Context>, | ||
results: AggregatedResult | ||
): Promise<void> { | ||
this.#ipc.emit('runComplete', { contexts, results }) | ||
|
||
await this.#onConnected | ||
await this.#ipc.disconnect() | ||
} | ||
} |
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,117 @@ | ||
import console from 'console' | ||
import EventEmitter from 'events' | ||
import { IPC } from 'node-ipc' | ||
import MessageWriter from './message-writer' | ||
|
||
export default class IPCClient { | ||
#ipc: InstanceType<typeof IPC> | ||
#ipcChannel: string | ||
#messageQueue: Array<[string, unknown]> = [] | ||
#writer: MessageWriter | ||
#connected = false | ||
#promises: Set<Promise<void>> = new Set() | ||
#emitter: EventEmitter = new EventEmitter() | ||
|
||
get on(): EventEmitter['on'] { | ||
return this.#emitter.on.bind(this.#emitter) | ||
} | ||
|
||
get once(): EventEmitter['once'] { | ||
return this.#emitter.once.bind(this.#emitter) | ||
} | ||
|
||
get off(): EventEmitter['off'] { | ||
return this.#emitter.off.bind(this.#emitter) | ||
} | ||
|
||
get removeListener(): EventEmitter['removeListener'] { | ||
return this.#emitter.removeListener.bind(this.#emitter) | ||
} | ||
|
||
get removeAllListeners(): EventEmitter['removeAllListeners'] { | ||
return this.#emitter.removeAllListeners.bind(this.#emitter) | ||
} | ||
|
||
constructor(id: string) { | ||
const { IPC_CHANNEL, DEBUG_VSCODE_IPC } = process.env | ||
|
||
if (!IPC_CHANNEL) { | ||
throw new Error('IPC_CHANNEL is not defined') | ||
} | ||
|
||
this.#ipcChannel = IPC_CHANNEL | ||
|
||
this.#ipc = new IPC() | ||
this.#ipc.config.silent = !DEBUG_VSCODE_IPC | ||
this.#ipc.config.id = `jest-runner-vscode-${id}-${process.pid}` | ||
this.#ipc.config.logger = (message: string) => { | ||
// keep message no longer than 500 characters | ||
const truncatedMessage = | ||
message.length > 500 ? `${message.slice(0, 500)}...\u001b[0m` : message | ||
|
||
console.log(truncatedMessage) | ||
} | ||
|
||
this.#writer = new MessageWriter(this.#ipc, this.#ipcChannel) | ||
} | ||
|
||
async #flush(): Promise<void> { | ||
while (this.#messageQueue.length) { | ||
const message = this.#messageQueue.shift() | ||
|
||
if (message) { | ||
const [type, data] = message | ||
|
||
await this.#writer.write(type, data) | ||
|
||
this.#emitter.emit(type, data) | ||
} | ||
} | ||
} | ||
|
||
async connect(): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
this.#ipc.connectTo(this.#ipcChannel, async () => { | ||
this.#connected = true | ||
|
||
await this.#flush() | ||
|
||
this.#emitter.emit('connect') | ||
|
||
this.#ipc.of[this.#ipcChannel].on('disconnect', () => { | ||
this.#connected = false | ||
this.#emitter.emit('disconnect') | ||
}) | ||
|
||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
if (!this.#connected) { | ||
return | ||
} | ||
|
||
await Promise.all(this.#promises) | ||
|
||
const disconnected = new Promise<void>(resolve => { | ||
this.#emitter.once('disconnect', resolve) | ||
}) | ||
|
||
this.#ipc.disconnect(this.#ipcChannel) | ||
|
||
return disconnected | ||
} | ||
|
||
emit(type: string, data: unknown): void { | ||
if (!this.#connected) { | ||
this.#messageQueue.push([type, data]) | ||
} else { | ||
const promise = this.#writer.write(type, data) | ||
|
||
this.#promises.add(promise) | ||
promise.then(() => this.#promises.delete(promise)) | ||
} | ||
} | ||
} |
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,6 @@ | ||
declare module 'js-message' { | ||
export default class Message { | ||
type: string | ||
data: unknown | ||
} | ||
} |
Oops, something went wrong.