Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move hairy filtering out of index.js
  • Loading branch information
SimenB committed Aug 9, 2018
commit 41fcf39228acf31149bb6d6856db0dfe123d634b
32 changes: 2 additions & 30 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {validateCLIOptions} from 'jest-validate';
import {readConfig, deprecationEntries} from 'jest-config';
import * as args from './args';
import chalk from 'chalk';
import stripAnsi from 'strip-ansi';
import createContext from '../lib/create_context';
import exit from 'exit';
import getChangedFilesPromise from '../getChangedFilesPromise';
Expand Down Expand Up @@ -113,39 +112,12 @@ export const runCLI = async (
if (openHandles && openHandles.length) {
const formatted = formatHandleErrors(openHandles, configs[0]);

const stacks = new Set();

// E.g. timeouts might give multiple traces to the same line of code
// This hairy filtering tries to remove entries with duplicate stack traces
const uniqueErrors = formatted.filter(handle => {
const ansiFree: string = stripAnsi(handle);

const match = ansiFree.match(/\s+at(.*)/);

if (!match || match.length < 2) {
return true;
}

const stack = ansiFree.substr(ansiFree.indexOf(match[1])).trim();

if (stacks.has(stack)) {
return false;
}

stacks.add(stack);

return true;
});
const openHandlesString = pluralize(
'open handle',
uniqueErrors.length,
's',
);
const openHandlesString = pluralize('open handle', formatted.length, 's');

const message =
chalk.red(
`\nJest has detected the following ${openHandlesString} potentially keeping Jest from exiting:\n\n`,
) + uniqueErrors.join('\n\n');
) + formatted.join('\n\n');

console.error(message);
}
Expand Down
31 changes: 29 additions & 2 deletions packages/jest-cli/src/collectHandles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {ProjectConfig} from 'types/Config';

import {formatExecError} from 'jest-message-util';
import stripAnsi from 'strip-ansi';

function stackIsFromUser(stack) {
// Either the test file, or something required by it
Expand Down Expand Up @@ -90,7 +91,33 @@ export function formatHandleErrors(
errors: Array<Error>,
config: ProjectConfig,
): Array<string> {
return errors.map(err =>
formatExecError(err, config, {noStackTrace: false}, undefined, true),
const stacks = new Set();

return (
errors
.map(err =>
formatExecError(err, config, {noStackTrace: false}, undefined, true),
)
// E.g. timeouts might give multiple traces to the same line of code
// This hairy filtering tries to remove entries with duplicate stack traces
.filter(handle => {
const ansiFree: string = stripAnsi(handle);

const match = ansiFree.match(/\s+at(.*)/);

if (!match || match.length < 2) {
return true;
}

const stack = ansiFree.substr(ansiFree.indexOf(match[1])).trim();

if (stacks.has(stack)) {
return false;
}

stacks.add(stack);

return true;
})
);
}