Skip to content

Commit 4a7592c

Browse files
committed
chore: migrate jest-message-util to TypeScript
1 parent 7906f1d commit 4a7592c

File tree

13 files changed

+756
-37
lines changed

13 files changed

+756
-37
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825))
1818
- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827))
1919
- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835))
20+
- `[jest-message-util]`: Migrate to TypeScript ([#7834](https://github.com/facebook/jest/pull/7834))
21+
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
2022

2123
### Performance
2224

packages/jest-message-util/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
},
1212
"license": "MIT",
1313
"main": "build/index.js",
14+
"types": "build/index.d.ts",
1415
"dependencies": {
1516
"@babel/code-frame": "^7.0.0",
17+
"@jest/types": "^24.0.0",
18+
"@types/stack-utils": "^1.0.1",
1619
"chalk": "^2.0.1",
1720
"micromatch": "^3.1.10",
1821
"slash": "^2.0.0",
@@ -21,7 +24,7 @@
2124
"devDependencies": {
2225
"@types/babel__code-frame": "^7.0.0",
2326
"@types/micromatch": "^3.1.0",
24-
"@types/stack-utils": "^1.0.1"
27+
"@types/slash": "^2.0.0"
2528
},
2629
"gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60"
2730
}

packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap renamed to packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports[`.formatExecError() 1`] = `
1010
exports[`formatStackTrace should strip node internals 1`] = `
1111
"<bold><red> <bold>● <bold>Unix test</></>
1212
13-
13+
1414
Expected value to be of type:
1515
\\"number\\"
1616
Received:
@@ -26,7 +26,7 @@ exports[`formatStackTrace should strip node internals 1`] = `
2626
exports[`retains message in babel code frame error 1`] = `
2727
"<bold><red> <bold>● <bold>Babel test</></>
2828
29-
29+
3030
packages/react/src/forwardRef.js: Unexpected token, expected , (20:10)
3131
<dim></>
3232
<dim> </> <gray> 18 | </> <cyan>false</><yellow>,</></>
@@ -52,7 +52,7 @@ exports[`should exclude jasmine from stack trace for Unix paths. 1`] = `
5252
exports[`should not exclude vendor from stack trace 1`] = `
5353
"<bold><red> <bold>● <bold>Vendor test</></>
5454
55-
55+
5656
Expected value to be of type:
5757
\\"number\\"
5858
Received:

packages/jest-message-util/src/__tests__/messages.test.js renamed to packages/jest-message-util/src/__tests__/messages.test.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
*
77
*/
88

9-
'use strict';
10-
11-
const {formatResultsErrors, formatExecError} = require('..');
9+
import {formatExecError, formatResultsErrors} from '..';
1210

1311
const unixStackTrace =
1412
` ` +
@@ -63,7 +61,7 @@ const babelStack =
6361
`
6462
packages/react/src/forwardRef.js: Unexpected token, expected , (20:10)
6563
\u001b[0m \u001b[90m 18 | \u001b[39m \u001b[36mfalse\u001b[39m\u001b[33m,\u001b[39m
66-
\u001b[90m 19 | \u001b[39m \u001b[32m'forwardRef requires a render function but received a \`memo\` '\u001b[39m
64+
\u001b[90m 19 | \u001b[39m \u001b[32m'forwardRef requires a render function but received a \`memo\` '\u001b[39m
6765
\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 20 | \u001b[39m \u001b[32m'component. Instead of forwardRef(memo(...)), use '\u001b[39m \u001b[33m+\u001b[39m
6866
\u001b[90m | \u001b[39m \u001b[31m\u001b[1m^\u001b[22m\u001b[39m
6967
\u001b[90m 21 | \u001b[39m \u001b[32m'memo(forwardRef(...)).'\u001b[39m\u001b[33m,\u001b[39m
@@ -77,11 +75,16 @@ it('should exclude jasmine from stack trace for Unix paths.', () => {
7775
{
7876
ancestorTitles: [],
7977
failureMessages: [unixStackTrace],
78+
fullName: 'full name',
79+
location: null,
80+
numPassingAsserts: 0,
81+
status: 'failed',
8082
title: 'Unix test',
8183
},
8284
],
8385
{
8486
rootDir: '',
87+
testMatch: [],
8588
},
8689
{
8790
noStackTrace: false,
@@ -95,9 +98,11 @@ it('.formatExecError()', () => {
9598
const message = formatExecError(
9699
{
97100
message: 'Whoops!',
101+
stack: '',
98102
},
99103
{
100104
rootDir: '',
105+
testMatch: [],
101106
},
102107
{
103108
noStackTrace: false,
@@ -114,11 +119,16 @@ it('formatStackTrace should strip node internals', () => {
114119
{
115120
ancestorTitles: [],
116121
failureMessages: [assertionStack],
122+
fullName: 'full name',
123+
location: null,
124+
numPassingAsserts: 0,
125+
status: 'failed',
117126
title: 'Unix test',
118127
},
119128
],
120129
{
121130
rootDir: '',
131+
testMatch: [],
122132
},
123133
{
124134
noStackTrace: false,
@@ -134,11 +144,16 @@ it('should not exclude vendor from stack trace', () => {
134144
{
135145
ancestorTitles: [],
136146
failureMessages: [vendorStack],
147+
fullName: 'full name',
148+
location: null,
149+
numPassingAsserts: 0,
150+
status: 'failed',
137151
title: 'Vendor test',
138152
},
139153
],
140154
{
141155
rootDir: '',
156+
testMatch: [],
142157
},
143158
{
144159
noStackTrace: false,
@@ -154,11 +169,16 @@ it('retains message in babel code frame error', () => {
154169
{
155170
ancestorTitles: [],
156171
failureMessages: [babelStack],
172+
fullName: 'full name',
173+
location: null,
174+
numPassingAsserts: 0,
175+
status: 'failed',
157176
title: 'Babel test',
158177
},
159178
],
160179
{
161180
rootDir: '',
181+
testMatch: [],
162182
},
163183
{
164184
noStackTrace: false,

packages/jest-message-util/src/index.js renamed to packages/jest-message-util/src/index.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow
86
*/
97

10-
import type {Glob, Path} from 'types/Config';
11-
import type {AssertionResult, SerializableError} from 'types/TestResult';
12-
138
import fs from 'fs';
149
import path from 'path';
10+
import {Config, TestResult} from '@jest/types';
1511
import chalk from 'chalk';
1612
import micromatch from 'micromatch';
1713
import slash from 'slash';
1814
import {codeFrameColumns} from '@babel/code-frame';
1915
import StackUtils from 'stack-utils';
2016

17+
type Glob = Config.Glob;
18+
type Path = Config.Path;
19+
type AssertionResult = TestResult.AssertionResult;
20+
type SerializableError = TestResult.SerializableError;
21+
2122
// stack utils tries to create pretty stack by making paths relative.
2223
const stackUtils = new StackUtils({
2324
cwd: 'something which does not exist',
2425
});
2526

26-
let nodeInternals = [];
27+
let nodeInternals: RegExp[] = [];
2728

2829
try {
2930
nodeInternals = StackUtils.nodeInternals();
@@ -33,12 +34,12 @@ try {
3334
}
3435

3536
type StackTraceConfig = {
36-
rootDir: string,
37-
testMatch: Array<Glob>,
37+
rootDir: string;
38+
testMatch: Array<Glob>;
3839
};
3940

4041
type StackTraceOptions = {
41-
noStackTrace: boolean,
42+
noStackTrace: boolean;
4243
};
4344

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

67-
const trim = string => (string || '').trim();
68+
const trim = (string: string) => (string || '').trim();
6869

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

7677
const getRenderedCallsite = (
@@ -94,11 +95,11 @@ const getRenderedCallsite = (
9495
// `before/after each` hooks). If it's thrown, none of the tests in the file
9596
// are executed.
9697
export const formatExecError = (
97-
error?: Error | SerializableError | string,
98+
error: Error | SerializableError | string | undefined,
9899
config: StackTraceConfig,
99100
options: StackTraceOptions,
100-
testPath: ?Path,
101-
reuseMessage: ?boolean,
101+
testPath?: Path,
102+
reuseMessage?: boolean,
102103
) => {
103104
if (!error || typeof error === 'number') {
104105
error = new Error(`Expected an Error, but "${String(error)}" was thrown`);
@@ -198,7 +199,11 @@ const removeInternalStackEntries = (
198199
});
199200
};
200201

201-
const formatPaths = (config: StackTraceConfig, relativeTestPath, line) => {
202+
const formatPaths = (
203+
config: StackTraceConfig,
204+
relativeTestPath: Path | null,
205+
line: string,
206+
) => {
202207
// Extract the file path from the trace line.
203208
const match = line.match(/(^\s*at .*?\(?)([^()]+)(:[0-9]+:[0-9]+\)?.*$)/);
204209
if (!match) {
@@ -243,7 +248,7 @@ export const formatStackTrace = (
243248
stack: string,
244249
config: StackTraceConfig,
245250
options: StackTraceOptions,
246-
testPath: ?Path,
251+
testPath?: Path,
247252
) => {
248253
const lines = getStackTraceLines(stack, options);
249254
const topFrame = getTopFrame(lines);
@@ -253,19 +258,15 @@ export const formatStackTrace = (
253258
: null;
254259

255260
if (topFrame) {
256-
const filename = topFrame.file;
261+
const {column, file: filename, line} = topFrame;
257262

258-
if (path.isAbsolute(filename)) {
263+
if (line && filename && path.isAbsolute(filename)) {
259264
let fileContent;
260265
try {
261266
// TODO: check & read HasteFS instead of reading the filesystem:
262267
// see: https://github.com/facebook/jest/pull/5405#discussion_r164281696
263268
fileContent = fs.readFileSync(filename, 'utf8');
264-
renderedCallsite = getRenderedCallsite(
265-
fileContent,
266-
topFrame.line,
267-
topFrame.column,
268-
);
269+
renderedCallsite = getRenderedCallsite(fileContent, line, column);
269270
} catch (e) {
270271
// the file does not exist or is inaccessible, we ignore
271272
}
@@ -287,12 +288,20 @@ export const formatResultsErrors = (
287288
testResults: Array<AssertionResult>,
288289
config: StackTraceConfig,
289290
options: StackTraceOptions,
290-
testPath: ?Path,
291-
): ?string => {
292-
const failedResults = testResults.reduce((errors, result) => {
293-
result.failureMessages.forEach(content => errors.push({content, result}));
294-
return errors;
295-
}, []);
291+
testPath?: Path,
292+
): string | null => {
293+
type FailedResults = Array<{
294+
content: string;
295+
result: AssertionResult;
296+
}>;
297+
298+
const failedResults: FailedResults = testResults.reduce(
299+
(errors, result) => {
300+
result.failureMessages.forEach(content => errors.push({content, result}));
301+
return errors;
302+
},
303+
[] as FailedResults,
304+
);
296305

297306
if (!failedResults.length) {
298307
return null;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "build"
6+
},
7+
"references": [
8+
{"path": "../jest-types"}
9+
]
10+
}

packages/jest-types/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
**/__mocks__/**
2+
**/__tests__/**
3+
src

packages/jest-types/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@jest/types",
3+
"version": "24.0.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "https://github.com/facebook/jest.git",
7+
"directory": "packages/jest-types"
8+
},
9+
"engines": {
10+
"node": ">= 6"
11+
},
12+
"license": "MIT",
13+
"main": "build/index.d.ts",
14+
"types": "build/index.d.ts"
15+
}

0 commit comments

Comments
 (0)