Skip to content

Commit

Permalink
chore: make TestWatcher extend emittery
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Dec 5, 2020
1 parent 5e2e8ed commit 731ba95
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
- `[jest-core]` make `TestWatcher` extend `emittery` ([#10324](https://github.com/facebook/jest/pull/10324))
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
Expand Down
1 change: 1 addition & 0 deletions packages/jest-core/package.json
Expand Up @@ -17,6 +17,7 @@
"@types/node": "*",
"ansi-escapes": "^4.2.1",
"chalk": "^4.0.0",
"emittery": "^0.7.2",
"exit": "^0.1.2",
"graceful-fs": "^4.2.4",
"jest-changed-files": "^26.6.2",
Expand Down
19 changes: 9 additions & 10 deletions packages/jest-core/src/TestScheduler.ts
Expand Up @@ -419,7 +419,7 @@ export default class TestScheduler {
throw new Error('Reporter should be either a string or an array');
}

private _bailIfNeeded(
private async _bailIfNeeded(
contexts: Set<Context>,
aggregatedResults: AggregatedResult,
watcher: TestWatcher,
Expand All @@ -429,17 +429,16 @@ export default class TestScheduler {
aggregatedResults.numFailedTests >= this._globalConfig.bail
) {
if (watcher.isWatchMode()) {
watcher.setState({interrupted: true});
} else {
const failureExit = () => exit(1);

return this._dispatcher
.onRunComplete(contexts, aggregatedResults)
.then(failureExit)
.catch(failureExit);
await watcher.setState({interrupted: true});
return;
}

try {
await this._dispatcher.onRunComplete(contexts, aggregatedResults);
} finally {
exit(1);
}
}
return Promise.resolve();
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/jest-core/src/TestWatcher.ts
Expand Up @@ -5,13 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

import {EventEmitter} from 'events';
import emittery = require('emittery');

type State = {
interrupted: boolean;
};

export default class TestWatcher extends EventEmitter {
export default class TestWatcher extends emittery.Typed<{change: State}> {
state: State;
private _isWatchMode: boolean;

Expand All @@ -21,9 +21,9 @@ export default class TestWatcher extends EventEmitter {
this._isWatchMode = isWatchMode;
}

setState(state: State): void {
async setState(state: State): Promise<void> {
Object.assign(this.state, state);
this.emit('change', this.state);
await this.emit('change', this.state);
}

isInterrupted(): boolean {
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-runner/src/index.ts
Expand Up @@ -25,7 +25,6 @@ import type {
TestRunnerContext,
TestRunnerOptions,
TestWatcher,
WatcherState,
} from './types';

const TEST_WORKER_PATH = require.resolve('./testWorker');
Expand Down Expand Up @@ -239,7 +238,7 @@ export default class TestRunner {
};

const onInterrupt = new Promise((_, reject) => {
watcher.on('change', (state: WatcherState) => {
watcher.on('change', state => {
if (state.interrupted) {
reject(new CancelRun());
}
Expand Down
8 changes: 3 additions & 5 deletions packages/jest-runner/src/types.ts
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {EventEmitter} from 'events';
import type {Typed as EmitteryTyped} from 'emittery';
import type {JestEnvironment} from '@jest/environment';
import type {
AssertionResult,
Expand Down Expand Up @@ -79,10 +79,8 @@ export type TestRunnerSerializedContext = {
};

// TODO: Should live in `@jest/core` or `jest-watcher`
export type WatcherState = {
interrupted: boolean;
};
export interface TestWatcher extends EventEmitter {
type WatcherState = {interrupted: boolean};
export interface TestWatcher extends EmitteryTyped<{change: WatcherState}> {
state: WatcherState;
setState(state: WatcherState): void;
isInterrupted(): boolean;
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Expand Up @@ -1784,6 +1784,7 @@ __metadata:
"@types/rimraf": ^3.0.0
ansi-escapes: ^4.2.1
chalk: ^4.0.0
emittery: ^0.7.2
exit: ^0.1.2
graceful-fs: ^4.2.4
jest-changed-files: ^26.6.2
Expand Down Expand Up @@ -7623,7 +7624,7 @@ __metadata:
languageName: node
linkType: hard

"emittery@npm:^0.7.1":
"emittery@npm:^0.7.1, emittery@npm:^0.7.2":
version: 0.7.2
resolution: "emittery@npm:0.7.2"
checksum: 34acfef51922a1b73d75cb658bf43ecb279633b263ffa831fb87697abbbd3aa4241ef15d204eeaa6a3c62656bd7563de7145c416a2bb18c4805e54ce6d7cdac6
Expand Down

0 comments on commit 731ba95

Please sign in to comment.