Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-message-util to TypeScript #7834

Merged
merged 1 commit into from Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,8 @@
- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835))
- `[jest-docblock]`: Migrate to TypeScript ([#7836](https://github.com/facebook/jest/pull/7836))
- `[jest-serializer]`: Migrate to TypeScript ([#7841](https://github.com/facebook/jest/pull/7841))
- `[jest-message-util]`: Migrate to TypeScript ([#7834](https://github.com/facebook/jest/pull/7834))
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))

### Performance

Expand Down
5 changes: 4 additions & 1 deletion packages/jest-message-util/package.json
Expand Up @@ -11,8 +11,11 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@babel/code-frame": "^7.0.0",
"@jest/types": "^24.1.0",
"@types/stack-utils": "^1.0.1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved since it's part of the exported interface

"chalk": "^2.0.1",
"micromatch": "^3.1.10",
"slash": "^2.0.0",
Expand All @@ -21,7 +24,7 @@
"devDependencies": {
"@types/babel__code-frame": "^7.0.0",
"@types/micromatch": "^3.1.0",
"@types/stack-utils": "^1.0.1"
"@types/slash": "^2.0.0"
},
"gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60"
}
Expand Up @@ -6,9 +6,7 @@
*
*/

'use strict';

const {formatResultsErrors, formatExecError} = require('..');
import {formatExecError, formatResultsErrors} from '..';

const unixStackTrace =
` ` +
Expand Down Expand Up @@ -77,11 +75,16 @@ it('should exclude jasmine from stack trace for Unix paths.', () => {
{
ancestorTitles: [],
failureMessages: [unixStackTrace],
fullName: 'full name',
location: null,
numPassingAsserts: 0,
status: 'failed',
title: 'Unix test',
},
],
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
Expand All @@ -95,9 +98,11 @@ it('.formatExecError()', () => {
const message = formatExecError(
{
message: 'Whoops!',
stack: '',
},
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
Expand All @@ -114,11 +119,16 @@ it('formatStackTrace should strip node internals', () => {
{
ancestorTitles: [],
failureMessages: [assertionStack],
fullName: 'full name',
location: null,
numPassingAsserts: 0,
status: 'failed',
title: 'Unix test',
},
],
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
Expand All @@ -134,11 +144,16 @@ it('should not exclude vendor from stack trace', () => {
{
ancestorTitles: [],
failureMessages: [vendorStack],
fullName: 'full name',
location: null,
numPassingAsserts: 0,
status: 'failed',
title: 'Vendor test',
},
],
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
Expand All @@ -154,11 +169,16 @@ it('retains message in babel code frame error', () => {
{
ancestorTitles: [],
failureMessages: [babelStack],
fullName: 'full name',
location: null,
numPassingAsserts: 0,
status: 'failed',
title: 'Babel test',
},
],
{
rootDir: '',
testMatch: [],
},
{
noStackTrace: false,
Expand Down
Expand Up @@ -3,27 +3,28 @@
*
* 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 {Glob, Path} from 'types/Config';
import type {AssertionResult, SerializableError} from 'types/TestResult';

import fs from 'fs';
import path from 'path';
import {Config, TestResult} from '@jest/types';
import chalk from 'chalk';
import micromatch from 'micromatch';
import slash from 'slash';
import {codeFrameColumns} from '@babel/code-frame';
import StackUtils from 'stack-utils';

type Glob = Config.Glob;
type Path = Config.Path;
type AssertionResult = TestResult.AssertionResult;
type SerializableError = TestResult.SerializableError;

// stack utils tries to create pretty stack by making paths relative.
const stackUtils = new StackUtils({
cwd: 'something which does not exist',
});

let nodeInternals = [];
let nodeInternals: RegExp[] = [];

try {
nodeInternals = StackUtils.nodeInternals();
Expand All @@ -33,12 +34,12 @@ try {
}

type StackTraceConfig = {
rootDir: string,
testMatch: Array<Glob>,
rootDir: string;
testMatch: Array<Glob>;
};

type StackTraceOptions = {
noStackTrace: boolean,
noStackTrace: boolean;
};

const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`;
Expand All @@ -64,13 +65,13 @@ const NOT_EMPTY_LINE_REGEXP = /^(?!$)/gm;
const indentAllLines = (lines: string, indent: string) =>
lines.replace(NOT_EMPTY_LINE_REGEXP, indent);

const trim = string => (string || '').trim();
const trim = (string: string) => (string || '').trim();

// Some errors contain not only line numbers in stack traces
// e.g. SyntaxErrors can contain snippets of code, and we don't
// want to trim those, because they may have pointers to the column/character
// which will get misaligned.
const trimPaths = string =>
const trimPaths = (string: string) =>
string.match(STACK_PATH_REGEXP) ? trim(string) : string;

const getRenderedCallsite = (
Expand All @@ -94,11 +95,11 @@ const getRenderedCallsite = (
// `before/after each` hooks). If it's thrown, none of the tests in the file
// are executed.
export const formatExecError = (
error?: Error | SerializableError | string,
error: Error | SerializableError | string | undefined,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: ?Path,
reuseMessage: ?boolean,
testPath?: Path,
reuseMessage?: boolean,
) => {
if (!error || typeof error === 'number') {
error = new Error(`Expected an Error, but "${String(error)}" was thrown`);
Expand Down Expand Up @@ -198,7 +199,11 @@ const removeInternalStackEntries = (
});
};

const formatPaths = (config: StackTraceConfig, relativeTestPath, line) => {
const formatPaths = (
config: StackTraceConfig,
relativeTestPath: Path | null,
line: string,
) => {
// Extract the file path from the trace line.
const match = line.match(/(^\s*at .*?\(?)([^()]+)(:[0-9]+:[0-9]+\)?.*$)/);
if (!match) {
Expand Down Expand Up @@ -243,7 +248,7 @@ export const formatStackTrace = (
stack: string,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: ?Path,
testPath?: Path,
) => {
const lines = getStackTraceLines(stack, options);
const topFrame = getTopFrame(lines);
Expand All @@ -253,19 +258,15 @@ export const formatStackTrace = (
: null;

if (topFrame) {
const filename = topFrame.file;
const {column, file: filename, line} = topFrame;

if (path.isAbsolute(filename)) {
if (line && filename && path.isAbsolute(filename)) {
let fileContent;
try {
// TODO: check & read HasteFS instead of reading the filesystem:
// see: https://github.com/facebook/jest/pull/5405#discussion_r164281696
fileContent = fs.readFileSync(filename, 'utf8');
renderedCallsite = getRenderedCallsite(
fileContent,
topFrame.line,
topFrame.column,
);
renderedCallsite = getRenderedCallsite(fileContent, line, column);
} catch (e) {
// the file does not exist or is inaccessible, we ignore
}
Expand All @@ -287,12 +288,20 @@ export const formatResultsErrors = (
testResults: Array<AssertionResult>,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: ?Path,
): ?string => {
const failedResults = testResults.reduce((errors, result) => {
result.failureMessages.forEach(content => errors.push({content, result}));
return errors;
}, []);
testPath?: Path,
): string | null => {
type FailedResults = Array<{
content: string;
result: AssertionResult;
}>;

const failedResults: FailedResults = testResults.reduce(
(errors, result) => {
result.failureMessages.forEach(content => errors.push({content, result}));
return errors;
},
[] as FailedResults,
);

if (!failedResults.length) {
return null;
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-message-util/tsconfig.json
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-types"}
]
}
3 changes: 3 additions & 0 deletions packages/jest-types/.npmignore
@@ -0,0 +1,3 @@
**/__mocks__/**
**/__tests__/**
src
15 changes: 15 additions & 0 deletions packages/jest-types/package.json
@@ -0,0 +1,15 @@
{
"name": "@jest/types",
"version": "24.1.0",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-types"
},
"engines": {
"node": ">= 6"
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts"
}