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 Jul 30, 2020
1 parent e270997 commit d4e3b4f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/jest-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@types/node": "*",
"ansi-escapes": "^4.2.1",
"chalk": "^4.0.0",
"emittery": "^0.7.1",
"exit": "^0.1.2",
"graceful-fs": "^4.2.4",
"jest-changed-files": "^26.1.0",
Expand Down
16 changes: 8 additions & 8 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,15 @@ 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);
return watcher.setState({interrupted: true});
}

const failureExit = () => exit(1);

return this._dispatcher
.onRunComplete(contexts, aggregatedResults)
.then(failureExit)
.catch(failureExit);
}
return Promise.resolve();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-core/src/TestWatcher.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type {
TestRunnerContext as JestTestRunnerContext,
TestRunnerOptions as JestTestRunnerOptions,
TestWatcher as JestTestWatcher,
WatcherState,
} from './types';

const TEST_WORKER_PATH = require.resolve('./testWorker');
Expand Down Expand Up @@ -243,7 +242,7 @@ 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
Original file line number Diff line number Diff line change
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 {Config} from '@jest/types';
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
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,7 @@ __metadata:
"@types/rimraf": ^3.0.0
ansi-escapes: ^4.2.1
chalk: ^4.0.0
emittery: ^0.7.1
exit: ^0.1.2
graceful-fs: ^4.2.4
jest-changed-files: ^26.1.0
Expand Down

0 comments on commit d4e3b4f

Please sign in to comment.