Skip to content

Commit

Permalink
chore: migrate @jest/core to TypeScript (#7998)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 2, 2019
1 parent 160d27a commit 70bed72
Show file tree
Hide file tree
Showing 65 changed files with 1,006 additions and 857 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -77,6 +77,7 @@
- `[jest-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000))
- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991))
- `[docs]`: Update CONTRIBUTING.md to add information about running jest with `jest-circus` locally ([#8013](https://github.com/facebook/jest/pull/8013)).
- `[@jest/core]`: Migrate to TypeScript ([#7998](https://github.com/facebook/jest/pull/7998))

### Performance

Expand Down
2 changes: 2 additions & 0 deletions packages/jest-changed-files/src/index.ts
Expand Up @@ -15,6 +15,8 @@ import hg from './hg';

type RootPromise = ReturnType<SCMAdapter['getRoot']>;

export {ChangedFiles, ChangedFilesPromise} from './types';

function notEmpty<T>(value: T | null | undefined): value is T {
return value != null;
}
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-changed-files/src/types.ts
Expand Up @@ -14,12 +14,10 @@ export type Options = {
includePaths?: Array<Config.Path>;
};

type ChangedFiles = Set<Config.Path>;
export type Repos = {git: ChangedFiles; hg: ChangedFiles};
export type ChangedFilesPromise = Promise<{
repos: Repos;
changedFiles: ChangedFiles;
}>;
type Paths = Set<Config.Path>;
export type Repos = {git: Paths; hg: Paths};
export type ChangedFiles = {repos: Repos; changedFiles: Paths};
export type ChangedFilesPromise = Promise<ChangedFiles>;

export type SCMAdapter = {
findChangedFiles: (
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-core/package.json
Expand Up @@ -4,6 +4,7 @@
"version": "24.1.0",
"main": "build/jest.js",
"dependencies": {
"@jest/types": "^24.1.0",
"@jest/reporters": "^24.1.0",
"@jest/transform": "^24.1.0",
"ansi-escapes": "^3.0.0",
Expand All @@ -24,7 +25,7 @@
"jest-watcher": "^24.0.0",
"micromatch": "^3.1.10",
"p-each-series": "^1.0.0",
"pirates": "^4.0.0",
"pirates": "^4.0.1",
"realpath-native": "^1.1.0",
"rimraf": "^2.5.4",
"strip-ansi": "^5.0.0"
Expand Down
Expand Up @@ -3,45 +3,49 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Test} from 'types/TestRunner';
import type {TestResult} from 'types/TestResult';
import type {GlobalConfig} from 'types/Config';
import {Test} from 'jest-runner';
import {Config, TestResult} from '@jest/types';

type TestMap = {[key: string]: {[key: string]: boolean}};

export default class FailedTestsCache {
_enabledTestsMap: ?{[key: string]: {[key: string]: boolean}};
private _enabledTestsMap?: TestMap;

filterTests(tests: Array<Test>): Array<Test> {
if (!this._enabledTestsMap) {
const enabledTestsMap = this._enabledTestsMap;

if (!enabledTestsMap) {
return tests;
}
// $FlowFixMe
return tests.filter(testResult => this._enabledTestsMap[testResult.path]);
return tests.filter(testResult => enabledTestsMap[testResult.path]);
}

setTestResults(testResults: Array<TestResult>) {
setTestResults(testResults: Array<TestResult.TestResult>) {
this._enabledTestsMap = (testResults || [])
.filter(testResult => testResult.numFailingTests)
.reduce((suiteMap, testResult) => {
.reduce<TestMap>((suiteMap, testResult) => {
suiteMap[testResult.testFilePath] = testResult.testResults
.filter(test => test.status === 'failed')
.reduce((testMap, test) => {
testMap[test.fullName] = true;
return testMap;
}, {});
.reduce(
(testMap, test) => {
testMap[test.fullName] = true;
return testMap;
},
{} as {[name: string]: true},
);
return suiteMap;
}, {});

this._enabledTestsMap = Object.freeze(this._enabledTestsMap);
}

updateConfig(globalConfig: GlobalConfig): GlobalConfig {
updateConfig(globalConfig: Config.GlobalConfig): Config.GlobalConfig {
if (!this._enabledTestsMap) {
return globalConfig;
}
const newConfig: GlobalConfig = {...globalConfig};
const newConfig: Config.GlobalConfig = {...globalConfig};
newConfig.enabledTestsMap = this._enabledTestsMap;
return Object.freeze(newConfig);
}
Expand Down
Expand Up @@ -3,23 +3,15 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Context} from 'types/Context';
import type {Reporter, Test} from 'types/TestRunner';
import type {TestResult, AggregatedResult} from 'types/TestResult';
import type {ReporterOnStartOptions} from 'types/Reporters';

export type RunOptions = {|
estimatedTime: number,
showStatus: boolean,
|};
import {TestResult} from '@jest/types';
import {Test} from 'jest-runner';
import {Context} from 'jest-runtime';
import {Reporter, ReporterOnStartOptions} from './types';

export default class ReporterDispatcher {
_disabled: boolean;
_reporters: Array<Reporter>;
private _reporters: Array<Reporter>;

constructor() {
this._reporters = [];
Expand All @@ -37,8 +29,8 @@ export default class ReporterDispatcher {

async onTestResult(
test: Test,
testResult: TestResult,
results: AggregatedResult,
testResult: TestResult.TestResult,
results: TestResult.AggregatedResult,
) {
for (const reporter of this._reporters) {
reporter.onTestResult &&
Expand All @@ -52,13 +44,19 @@ export default class ReporterDispatcher {
}
}

async onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {
async onRunStart(
results: TestResult.AggregatedResult,
options: ReporterOnStartOptions,
) {
for (const reporter of this._reporters) {
reporter.onRunStart && (await reporter.onRunStart(results, options));
}
}

async onRunComplete(contexts: Set<Context>, results: AggregatedResult) {
async onRunComplete(
contexts: Set<Context>,
results: TestResult.AggregatedResult,
) {
for (const reporter of this._reporters) {
reporter.onRunComplete &&
(await reporter.onRunComplete(contexts, results));
Expand All @@ -67,7 +65,7 @@ export default class ReporterDispatcher {

// Return a list of last errors for every reporter
getErrors(): Array<Error> {
return this._reporters.reduce((list, reporter) => {
return this._reporters.reduce<Array<Error>>((list, reporter) => {
const error = reporter.getLastError && reporter.getLastError();
return error ? list.concat(error) : list;
}, []);
Expand Down

0 comments on commit 70bed72

Please sign in to comment.