Skip to content

Commit

Permalink
refactor(@jest-reporters): move helpers from utils.ts into separate…
Browse files Browse the repository at this point in the history
… files (jestjs#12782)
  • Loading branch information
mrazauskas authored and F3n67u committed May 2, 2022
1 parent b7b4e22 commit ca801ba
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 200 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@

### Chore & Maintenance

- `[@jest-reporters]` Move helper functions from `utils.ts` into separate files ([#12782](https://github.com/facebook/jest/pull/12782))

### Performance

## 28.0.3
Expand Down
97 changes: 97 additions & 0 deletions packages/jest-reporters/__typetests__/jest-reporters.test.ts
@@ -0,0 +1,97 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {expectError, expectType} from 'tsd-lite';
import {utils} from '@jest/reporters';
import type {
AggregatedResult,
Config,
SnapshotSummary,
SummaryOptions,
TestResult,
} from '@jest/reporters';

declare const aggregatedResults: AggregatedResult;
declare const globalConfig: Config.GlobalConfig;
declare const projectConfig: Config.ProjectConfig;
declare const snapshot: TestResult['snapshot'];
declare const snapshotSummary: SnapshotSummary;
declare const summaryOptions: SummaryOptions;
declare const testResult: TestResult;

// utils.formatTestPath()

expectType<string>(utils.formatTestPath(globalConfig, 'some/path'));
expectType<string>(utils.formatTestPath(projectConfig, 'some/path'));
expectError(utils.formatTestPath());
expectError(utils.formatTestPath({}, 'some/path'));
expectError(utils.formatTestPath(globalConfig, 123));
expectError(utils.formatTestPath(projectConfig, 123));

// utils.getResultHeader()

expectType<string>(
utils.getResultHeader(testResult, globalConfig, projectConfig),
);
expectType<string>(utils.getResultHeader(testResult, globalConfig));
expectError(utils.getResultHeader());
expectError(utils.getResultHeader({}, globalConfig));
expectError(utils.getResultHeader({}, globalConfig, projectConfig));
expectError(utils.getResultHeader(testResult, {}));
expectError(utils.getResultHeader(testResult, globalConfig, {}));

// utils.getSnapshotStatus()

expectType<Array<string>>(utils.getSnapshotStatus(snapshot, true));
expectError(utils.getSnapshotStatus());
expectError(utils.getSnapshotStatus({}, true));
expectError(utils.getSnapshotStatus(snapshot, 123));

// utils.getSnapshotSummary()

expectType<Array<string>>(
utils.getSnapshotSummary(snapshotSummary, globalConfig, 'press `u`'),
);
expectError(utils.getSnapshotSummary());
expectError(utils.getSnapshotSummary({}, globalConfig, 'press `u`'));
expectError(utils.getSnapshotSummary(snapshotSummary, {}, 'press `u`'));
expectError(utils.getSnapshotSummary(snapshotSummary, globalConfig, true));

// utils.getSummary()

expectType<string>(utils.getSummary(aggregatedResults, summaryOptions));
expectType<string>(utils.getSummary(aggregatedResults));
expectError(utils.getSummary());
expectError(utils.getSummary({}));
expectError(utils.getSummary(aggregatedResults, true));

// utils.printDisplayName()

expectType<string>(utils.printDisplayName(projectConfig));
expectError(utils.printDisplayName());
expectError(utils.printDisplayName({}));

// utils.relativePath()

expectType<{basename: string; dirname: string}>(
utils.relativePath(globalConfig, 'some/path'),
);
expectType<{basename: string; dirname: string}>(
utils.relativePath(projectConfig, 'some/path'),
);
expectError(utils.relativePath());
expectError(utils.relativePath({}, 'some/path'));
expectError(utils.relativePath(projectConfig, true));

// utils.trimAndFormatPath()

expectType<string>(utils.trimAndFormatPath(2, globalConfig, 'some/path', 4));
expectError(utils.trimAndFormatPath());
expectError(utils.trimAndFormatPath(true, globalConfig, 'some/path', 4));
expectError(utils.trimAndFormatPath(2, {}, 'some/path', 4));
expectError(utils.trimAndFormatPath(2, globalConfig, true, 4));
expectError(utils.trimAndFormatPath(2, globalConfig, 'some/path', '4'));
12 changes: 12 additions & 0 deletions packages/jest-reporters/__typetests__/tsconfig.json
@@ -0,0 +1,12 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"composite": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"skipLibCheck": true,

"types": []
},
"include": ["./**/*"]
}
4 changes: 3 additions & 1 deletion packages/jest-reporters/package.json
Expand Up @@ -33,11 +33,13 @@
"jest-worker": "^28.0.2",
"slash": "^3.0.0",
"string-length": "^4.0.1",
"strip-ansi": "^6.0.0",
"terminal-link": "^2.0.0",
"v8-to-istanbul": "^9.0.0"
},
"devDependencies": {
"@jest/test-utils": "^28.0.2",
"@tsd/typescript": "~4.6.2",
"@types/exit": "^0.1.30",
"@types/glob": "^7.1.1",
"@types/graceful-fs": "^4.1.3",
Expand All @@ -49,7 +51,7 @@
"@types/node-notifier": "^8.0.0",
"jest-resolve": "^28.0.3",
"mock-fs": "^5.1.2",
"strip-ansi": "^6.0.0"
"tsd-lite": "^0.5.1"
},
"peerDependencies": {
"node-notifier": "^8.0.1 || ^9.0.0 || ^10.0.0"
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-reporters/src/Status.ts
Expand Up @@ -14,13 +14,11 @@ import type {
TestResult,
} from '@jest/test-result';
import type {Config} from '@jest/types';
import getSummary from './getSummary';
import printDisplayName from './printDisplayName';
import trimAndFormatPath from './trimAndFormatPath';
import type {ReporterOnStartOptions} from './types';
import {
getSummary,
printDisplayName,
trimAndFormatPath,
wrapAnsiString,
} from './utils';
import wrapAnsiString from './wrapAnsiString';

const RUNNING_TEXT = ' RUNS ';
const RUNNING = `${chalk.reset.inverse.yellow.bold(RUNNING_TEXT)} `;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/SummaryReporter.ts
Expand Up @@ -16,8 +16,8 @@ import {testPathPatternToRegExp} from 'jest-util';
import BaseReporter from './BaseReporter';
import getResultHeader from './getResultHeader';
import getSnapshotSummary from './getSnapshotSummary';
import getSummary from './getSummary';
import type {ReporterOnStartOptions} from './types';
import {getSummary} from './utils';

const TEST_SUMMARY_THRESHOLD = 20;

Expand Down
4 changes: 3 additions & 1 deletion packages/jest-reporters/src/__tests__/utils.test.ts
Expand Up @@ -9,7 +9,9 @@ import * as path from 'path';
import chalk = require('chalk');
import stripAnsi = require('strip-ansi');
import {makeProjectConfig} from '@jest/test-utils';
import {printDisplayName, trimAndFormatPath, wrapAnsiString} from '../utils';
import printDisplayName from '../printDisplayName';
import trimAndFormatPath from '../trimAndFormatPath';
import wrapAnsiString from '../wrapAnsiString';

describe('wrapAnsiString()', () => {
it('wraps a long string containing ansi chars', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-reporters/src/formatTestPath.ts
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import chalk = require('chalk');
import slash = require('slash');
import type {Config} from '@jest/types';
import relativePath from './relativePath';

export default function formatTestPath(
config: Config.GlobalConfig | Config.ProjectConfig,
testPath: string,
): string {
const {dirname, basename} = relativePath(config, testPath);
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
}
3 changes: 2 additions & 1 deletion packages/jest-reporters/src/getResultHeader.ts
Expand Up @@ -10,7 +10,8 @@ import terminalLink = require('terminal-link');
import type {TestResult} from '@jest/test-result';
import type {Config} from '@jest/types';
import {formatTime} from 'jest-util';
import {formatTestPath, printDisplayName} from './utils';
import formatTestPath from './formatTestPath';
import printDisplayName from './printDisplayName';

const LONG_TEST_COLOR = chalk.reset.bold.bgRed;
// Explicitly reset for these messages since they can get written out in the
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/getSnapshotSummary.ts
Expand Up @@ -9,7 +9,7 @@ import chalk = require('chalk');
import type {SnapshotSummary} from '@jest/test-result';
import type {Config} from '@jest/types';
import {pluralize} from 'jest-util';
import {formatTestPath} from './utils';
import formatTestPath from './formatTestPath';

const ARROW = ' \u203A ';
const DOWN_ARROW = ' \u21B3 ';
Expand Down

0 comments on commit ca801ba

Please sign in to comment.