From 165430a32d7e39541d61071f52bf6e7d0fef95bc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 8 Feb 2019 09:28:33 +0100 Subject: [PATCH] chore: migrate jest-message-util to TypeScript --- CHANGELOG.md | 2 + packages/jest-message-util/package.json | 5 +- ...ges.test.js.snap => messages.test.ts.snap} | 0 .../{messages.test.js => messages.test.ts} | 26 +- .../src/{index.js => index.ts} | 67 ++-- packages/jest-message-util/tsconfig.json | 10 + packages/jest-types/.npmignore | 3 + packages/jest-types/package.json | 15 + packages/jest-types/src/Config.ts | 349 ++++++++++++++++++ packages/jest-types/src/Console.ts | 38 ++ packages/jest-types/src/TestResult.ts | 251 +++++++++++++ packages/jest-types/src/index.ts | 12 + packages/jest-types/tsconfig.json | 7 + 13 files changed, 752 insertions(+), 33 deletions(-) rename packages/jest-message-util/src/__tests__/__snapshots__/{messages.test.js.snap => messages.test.ts.snap} (100%) rename packages/jest-message-util/src/__tests__/{messages.test.js => messages.test.ts} (87%) rename packages/jest-message-util/src/{index.js => index.ts} (88%) create mode 100644 packages/jest-message-util/tsconfig.json create mode 100644 packages/jest-types/.npmignore create mode 100644 packages/jest-types/package.json create mode 100644 packages/jest-types/src/Config.ts create mode 100644 packages/jest-types/src/Console.ts create mode 100644 packages/jest-types/src/TestResult.ts create mode 100644 packages/jest-types/src/index.ts create mode 100644 packages/jest-types/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb53e9eb963..7fc71db7f904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ - `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827)) - `[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-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 diff --git a/packages/jest-message-util/package.json b/packages/jest-message-util/package.json index e61c38de40c1..5462adb9c08d 100644 --- a/packages/jest-message-util/package.json +++ b/packages/jest-message-util/package.json @@ -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.0.0", + "@types/stack-utils": "^1.0.1", "chalk": "^2.0.1", "micromatch": "^3.1.10", "slash": "^2.0.0", @@ -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" } diff --git a/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap b/packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap similarity index 100% rename from packages/jest-message-util/src/__tests__/__snapshots__/messages.test.js.snap rename to packages/jest-message-util/src/__tests__/__snapshots__/messages.test.ts.snap diff --git a/packages/jest-message-util/src/__tests__/messages.test.js b/packages/jest-message-util/src/__tests__/messages.test.ts similarity index 87% rename from packages/jest-message-util/src/__tests__/messages.test.js rename to packages/jest-message-util/src/__tests__/messages.test.ts index a26c4dab944e..667038690713 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.js +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -6,9 +6,7 @@ * */ -'use strict'; - -const {formatResultsErrors, formatExecError} = require('..'); +import {formatExecError, formatResultsErrors} from '..'; const unixStackTrace = ` ` + @@ -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, @@ -95,9 +98,11 @@ it('.formatExecError()', () => { const message = formatExecError( { message: 'Whoops!', + stack: '', }, { rootDir: '', + testMatch: [], }, { noStackTrace: false, @@ -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, @@ -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, @@ -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, diff --git a/packages/jest-message-util/src/index.js b/packages/jest-message-util/src/index.ts similarity index 88% rename from packages/jest-message-util/src/index.js rename to packages/jest-message-util/src/index.ts index 48afb9ec2a31..a95be08b6332 100644 --- a/packages/jest-message-util/src/index.js +++ b/packages/jest-message-util/src/index.ts @@ -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(); @@ -33,12 +34,12 @@ try { } type StackTraceConfig = { - rootDir: string, - testMatch: Array, + rootDir: string; + testMatch: Array; }; type StackTraceOptions = { - noStackTrace: boolean, + noStackTrace: boolean; }; const PATH_NODE_MODULES = `${path.sep}node_modules${path.sep}`; @@ -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 = ( @@ -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`); @@ -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) { @@ -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); @@ -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 } @@ -287,12 +288,20 @@ export const formatResultsErrors = ( testResults: Array, 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; diff --git a/packages/jest-message-util/tsconfig.json b/packages/jest-message-util/tsconfig.json new file mode 100644 index 000000000000..b802dbf5ce24 --- /dev/null +++ b/packages/jest-message-util/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-types"} + ] +} diff --git a/packages/jest-types/.npmignore b/packages/jest-types/.npmignore new file mode 100644 index 000000000000..85e48fe7b0a4 --- /dev/null +++ b/packages/jest-types/.npmignore @@ -0,0 +1,3 @@ +**/__mocks__/** +**/__tests__/** +src diff --git a/packages/jest-types/package.json b/packages/jest-types/package.json new file mode 100644 index 000000000000..3dd73f872c01 --- /dev/null +++ b/packages/jest-types/package.json @@ -0,0 +1,15 @@ +{ + "name": "@jest/types", + "version": "24.0.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" +} diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts new file mode 100644 index 000000000000..bd74d87c6b8b --- /dev/null +++ b/packages/jest-types/src/Config.ts @@ -0,0 +1,349 @@ +/** + * 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. + */ + +export type Path = string; + +export type Glob = string; + +export type HasteConfig = { + computeSha1?: boolean; + defaultPlatform?: string | null | undefined; + hasteImplModulePath?: string; + platforms?: Array; + providesModuleNodeModules: Array; +}; + +export type ReporterConfig = [string, Object]; + +export type ConfigGlobals = Object; + +export type DefaultOptions = { + automock: boolean; + bail: number; + browser: boolean; + cache: boolean; + cacheDirectory: Path; + changedFilesWithAncestor: boolean; + clearMocks: boolean; + collectCoverage: boolean; + collectCoverageFrom: Array | null | undefined; + coverageDirectory: string | null | undefined; + coveragePathIgnorePatterns: Array; + coverageReporters: Array; + coverageThreshold: + | { + global: { + [key: string]: number; + }; + } + | null + | undefined; + cwd: Path; + dependencyExtractor: string | null | undefined; + errorOnDeprecated: boolean; + expand: boolean; + filter: Path | null | undefined; + forceCoverageMatch: Array; + globals: ConfigGlobals; + globalSetup: string | null | undefined; + globalTeardown: string | null | undefined; + haste: HasteConfig; + maxConcurrency: number; + moduleDirectories: Array; + moduleFileExtensions: Array; + moduleNameMapper: { + [key: string]: string; + }; + modulePathIgnorePatterns: Array; + noStackTrace: boolean; + notify: boolean; + notifyMode: string; + preset: string | null | undefined; + prettierPath: string | null | undefined; + projects: Array | null | undefined; + resetMocks: boolean; + resetModules: boolean; + resolver: Path | null | undefined; + restoreMocks: boolean; + rootDir: Path | null | undefined; + roots: Array | null | undefined; + runner: string; + runTestsByPath: boolean; + setupFiles: Array; + setupFilesAfterEnv: Array; + skipFilter: boolean; + snapshotSerializers: Array; + testEnvironment: string; + testEnvironmentOptions: Object; + testFailureExitCode: string | number; + testLocationInResults: boolean; + testMatch: Array; + testPathIgnorePatterns: Array; + testRegex: Array; + testResultsProcessor: string | null | undefined; + testRunner: string | null | undefined; + testURL: string; + timers: 'real' | 'fake'; + transform: + | { + [key: string]: string; + } + | null + | undefined; + transformIgnorePatterns: Array; + watchPathIgnorePatterns: Array; + useStderr: boolean; + verbose: boolean | null | undefined; + watch: boolean; + watchman: boolean; +}; + +export type InitialOptions = { + automock?: boolean; + bail?: boolean | number; + browser?: boolean; + cache?: boolean; + cacheDirectory?: Path; + clearMocks?: boolean; + changedFilesWithAncestor?: boolean; + changedSince?: string; + collectCoverage?: boolean; + collectCoverageFrom?: Array; + collectCoverageOnlyFrom?: { + [key: string]: boolean; + }; + coverageDirectory?: string; + coveragePathIgnorePatterns?: Array; + coverageReporters?: Array; + coverageThreshold?: { + global: { + [key: string]: number; + }; + }; + dependencyExtractor?: string; + detectLeaks?: boolean; + detectOpenHandles?: boolean; + displayName?: string; + expand?: boolean; + extraGlobals?: Array; + filter?: Path; + findRelatedTests?: boolean; + forceCoverageMatch?: Array; + forceExit?: boolean; + json?: boolean; + globals?: ConfigGlobals; + globalSetup?: string | null | undefined; + globalTeardown?: string | null | undefined; + haste?: HasteConfig; + reporters?: Array; + logHeapUsage?: boolean; + lastCommit?: boolean; + listTests?: boolean; + mapCoverage?: boolean; + maxConcurrency?: number; + moduleDirectories?: Array; + moduleFileExtensions?: Array; + moduleLoader?: Path; + moduleNameMapper?: { + [key: string]: string; + }; + modulePathIgnorePatterns?: Array; + modulePaths?: Array; + name?: string; + noStackTrace?: boolean; + notify?: boolean; + notifyMode?: string; + onlyChanged?: boolean; + outputFile?: Path; + passWithNoTests?: boolean; + preprocessorIgnorePatterns?: Array; + preset?: string | null | undefined; + prettierPath?: string | null | undefined; + projects?: Array; + replname?: string | null | undefined; + resetMocks?: boolean; + resetModules?: boolean; + resolver?: Path | null | undefined; + restoreMocks?: boolean; + rootDir: Path; + roots?: Array; + runner?: string; + runTestsByPath?: boolean; + scriptPreprocessor?: string; + setupFiles?: Array; + setupTestFrameworkScriptFile?: Path; + setupFilesAfterEnv?: Array; + silent?: boolean; + skipFilter?: boolean; + skipNodeResolution?: boolean; + snapshotResolver?: Path; + snapshotSerializers?: Array; + errorOnDeprecated?: boolean; + testEnvironment?: string; + testEnvironmentOptions?: Object; + testFailureExitCode?: string | number; + testLocationInResults?: boolean; + testMatch?: Array; + testNamePattern?: string; + testPathDirs?: Array; + testPathIgnorePatterns?: Array; + testRegex?: string | Array; + testResultsProcessor?: string | null | undefined; + testRunner?: string; + testURL?: string; + timers?: 'real' | 'fake'; + transform?: { + [key: string]: string; + }; + transformIgnorePatterns?: Array; + watchPathIgnorePatterns?: Array; + unmockedModulePathPatterns?: Array; + updateSnapshot?: boolean; + useStderr?: boolean; + verbose?: boolean | null | undefined; + watch?: boolean; + watchAll?: boolean; + watchman?: boolean; + watchPlugins?: Array; +}; + +export type SnapshotUpdateState = 'all' | 'new' | 'none'; + +export type GlobalConfig = { + bail: number; + changedSince: string; + changedFilesWithAncestor: boolean; + collectCoverage: boolean; + collectCoverageFrom: Array; + collectCoverageOnlyFrom: + | { + [key: string]: boolean; + } + | null + | undefined; + coverageDirectory: string; + coveragePathIgnorePatterns?: Array; + coverageReporters: Array; + coverageThreshold: { + global: { + [key: string]: number; + }; + }; + detectLeaks: boolean; + detectOpenHandles: boolean; + enabledTestsMap: + | { + [key: string]: { + [key: string]: boolean; + }; + } + | null + | undefined; + expand: boolean; + extraGlobals: Array; + filter: Path | null | undefined; + findRelatedTests: boolean; + forceExit: boolean; + json: boolean; + globalSetup: string | null | undefined; + globalTeardown: string | null | undefined; + lastCommit: boolean; + logHeapUsage: boolean; + listTests: boolean; + maxConcurrency: number; + maxWorkers: number; + noStackTrace: boolean; + nonFlagArgs: Array; + noSCM: boolean | null | undefined; + notify: boolean; + notifyMode: string; + outputFile: Path | null | undefined; + onlyChanged: boolean; + onlyFailures: boolean; + passWithNoTests: boolean; + projects: Array; + replname: string | null | undefined; + reporters: Array; + runTestsByPath: boolean; + rootDir: Path; + silent: boolean; + skipFilter: boolean; + errorOnDeprecated: boolean; + testFailureExitCode: number; + testNamePattern: string; + testPathPattern: string; + testResultsProcessor: string | null | undefined; + updateSnapshot: SnapshotUpdateState; + useStderr: boolean; + verbose: boolean | null | undefined; + watch: boolean; + watchAll: boolean; + watchman: boolean; + watchPlugins: + | Array<{ + path: string; + config: Object; + }> + | null + | undefined; +}; + +export type ProjectConfig = { + automock: boolean; + browser: boolean; + cache: boolean; + cacheDirectory: Path; + clearMocks: boolean; + coveragePathIgnorePatterns: Array; + cwd: Path; + dependencyExtractor?: string; + detectLeaks: boolean; + detectOpenHandles: boolean; + displayName: string | null | undefined; + errorOnDeprecated: boolean; + extraGlobals: Array; + filter: Path | null | undefined; + forceCoverageMatch: Array; + globalSetup: string | null | undefined; + globalTeardown: string | null | undefined; + globals: ConfigGlobals; + haste: HasteConfig; + moduleDirectories: Array; + moduleFileExtensions: Array; + moduleLoader: Path; + moduleNameMapper: Array<[string, string]>; + modulePathIgnorePatterns: Array; + modulePaths: Array; + name: string; + prettierPath: string; + resetMocks: boolean; + resetModules: boolean; + resolver: Path | null | undefined; + restoreMocks: boolean; + rootDir: Path; + roots: Array; + runner: string; + setupFiles: Array; + setupFilesAfterEnv: Array; + skipFilter: boolean; + skipNodeResolution: boolean; + snapshotResolver: Path | null | undefined; + snapshotSerializers: Array; + testEnvironment: string; + testEnvironmentOptions: Object; + testMatch: Array; + testLocationInResults: boolean; + testPathIgnorePatterns: Array; + testRegex: Array; + testRunner: string; + testURL: string; + timers: 'real' | 'fake'; + transform: Array<[string, Path]>; + transformIgnorePatterns: Array; + watchPathIgnorePatterns: Array; + unmockedModulePathPatterns: Array | null | undefined; +}; diff --git a/packages/jest-types/src/Console.ts b/packages/jest-types/src/Console.ts new file mode 100644 index 000000000000..c85bef6639d3 --- /dev/null +++ b/packages/jest-types/src/Console.ts @@ -0,0 +1,38 @@ +/** + * 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. + */ + +export type LogMessage = string; + +export type LogEntry = { + message: LogMessage; + origin: string; + type: LogType; +}; + +export type LogCounters = { + [label: string]: number; +}; + +export type LogTimers = { + [label: string]: Date; +}; + +export type LogType = + | 'assert' + | 'count' + | 'debug' + | 'dir' + | 'dirxml' + | 'error' + | 'group' + | 'groupCollapsed' + | 'info' + | 'log' + | 'time' + | 'warn'; + +export type ConsoleBuffer = Array; diff --git a/packages/jest-types/src/TestResult.ts b/packages/jest-types/src/TestResult.ts new file mode 100644 index 000000000000..7c3b082836fd --- /dev/null +++ b/packages/jest-types/src/TestResult.ts @@ -0,0 +1,251 @@ +/** + * 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 {ConsoleBuffer} from './Console'; + +export type RawFileCoverage = { + path: string; + s: { + [statementId: number]: number; + }; + b: { + [branchId: number]: number; + }; + f: { + [functionId: number]: number; + }; + l: { + [lineId: number]: number; + }; + fnMap: { + [functionId: number]: any; + }; + statementMap: { + [statementId: number]: any; + }; + branchMap: { + [branchId: number]: any; + }; + inputSourceMap?: Object; +}; + +export type RawCoverage = { + [filePath: string]: RawFileCoverage; +}; +type FileCoverageTotal = { + total: number; + covered: number; + skipped: number; + pct: number; +}; + +export type CoverageSummary = { + lines: FileCoverageTotal; + statements: FileCoverageTotal; + branches: FileCoverageTotal; + functions: FileCoverageTotal; + merge: (other: CoverageSummary) => undefined; +}; + +export type FileCoverage = { + getLineCoverage: () => Object; + getUncoveredLines: () => Array; + getBranchCoverageByLine: () => Object; + toJSON: () => Object; + merge: (other: Object) => undefined; + computeSimpleTotals: (property: string) => FileCoverageTotal; + computeBranchTotals: () => FileCoverageTotal; + resetHits: () => undefined; + toSummary: () => CoverageSummary; +}; + +export type CoverageMap = { + merge: (data: Object) => undefined; + getCoverageSummary: () => FileCoverage; + data: RawCoverage; + addFileCoverage: (fileCoverage: RawFileCoverage) => undefined; + files: () => Array; + fileCoverageFor: (file: string) => FileCoverage; +}; + +export type SerializableError = { + code?: unknown; + message: string; + stack: string | null | undefined; + type?: string; +}; + +export type FailedAssertion = { + matcherName: string; + message?: string; + actual?: any; + pass?: boolean; + expected?: any; + isNot?: boolean; + stack?: string; +}; + +export type AssertionLocation = { + fullName: string; + path: string; +}; + +export type Status = + | 'passed' + | 'failed' + | 'skipped' + | 'pending' + | 'todo' + | 'disabled'; + +export type Bytes = number; + +export type Milliseconds = number; +type Callsite = { + column: number; + line: number; +}; + +export type AssertionResult = { + ancestorTitles: Array; + duration?: Milliseconds | null | undefined; + failureMessages: Array; + fullName: string; + invocations?: number; + location: Callsite | null | undefined; + numPassingAsserts: number; + status: Status; + title: string; +}; + +export type FormattedAssertionResult = { + failureMessages: Array | null; + fullName: string; + location: Callsite | null | undefined; + status: Status; + title: string; +}; + +export type AggregatedResultWithoutCoverage = { + numFailedTests: number; + numFailedTestSuites: number; + numPassedTests: number; + numPassedTestSuites: number; + numPendingTests: number; + numTodoTests: number; + numPendingTestSuites: number; + numRuntimeErrorTestSuites: number; + numTotalTests: number; + numTotalTestSuites: number; + openHandles: Array; + snapshot: SnapshotSummary; + startTime: number; + success: boolean; + testResults: Array; + wasInterrupted: boolean; +}; + +export type AggregatedResult = AggregatedResultWithoutCoverage & { + coverageMap?: CoverageMap | null | undefined; +}; + +export type Suite = { + title: string; + suites: Array; + tests: Array; +}; + +export type TestResult = { + console: ConsoleBuffer | null | undefined; + coverage?: RawCoverage; + displayName: string | null | undefined; + failureMessage: string | null | undefined; + leaks: boolean; + memoryUsage?: Bytes; + numFailingTests: number; + numPassingTests: number; + numPendingTests: number; + numTodoTests: number; + openHandles: Array; + perfStats: { + end: Milliseconds; + start: Milliseconds; + }; + skipped: boolean; + snapshot: { + added: number; + fileDeleted: boolean; + matched: number; + unchecked: number; + uncheckedKeys: Array; + unmatched: number; + updated: number; + }; + sourceMaps: { + [sourcePath: string]: string; + }; + testExecError?: SerializableError; + testFilePath: string; + testResults: Array; +}; + +export type FormattedTestResult = { + message: string; + name: string; + summary: string; + status: 'failed' | 'passed'; + startTime: number; + endTime: number; + coverage: any; + assertionResults: Array; +}; + +export type FormattedTestResults = { + coverageMap?: CoverageMap | null | undefined; + numFailedTests: number; + numFailedTestSuites: number; + numPassedTests: number; + numPassedTestSuites: number; + numPendingTests: number; + numPendingTestSuites: number; + numRuntimeErrorTestSuites: number; + numTotalTests: number; + numTotalTestSuites: number; + snapshot: SnapshotSummary; + startTime: number; + success: boolean; + testResults: Array; + wasInterrupted: boolean; +}; + +export type CodeCoverageReporter = any; + +export type CodeCoverageFormatter = ( + coverage: RawCoverage | null | undefined, + reporter: CodeCoverageReporter, +) => Object | null | undefined; + +export type UncheckedSnapshot = { + filePath: string; + keys: Array; +}; + +export type SnapshotSummary = { + added: number; + didUpdate: boolean; + failure: boolean; + filesAdded: number; + filesRemoved: number; + filesUnmatched: number; + filesUpdated: number; + matched: number; + total: number; + unchecked: number; + uncheckedKeysByFile: Array; + unmatched: number; + updated: number; +}; diff --git a/packages/jest-types/src/index.ts b/packages/jest-types/src/index.ts new file mode 100644 index 000000000000..60df27cb7dfd --- /dev/null +++ b/packages/jest-types/src/index.ts @@ -0,0 +1,12 @@ +/** + * 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 Config from './Config'; +import * as Console from './Console'; +import * as TestResult from './TestResult'; + +export {Config, Console, TestResult}; diff --git a/packages/jest-types/tsconfig.json b/packages/jest-types/tsconfig.json new file mode 100644 index 000000000000..7bb06bce6d20 --- /dev/null +++ b/packages/jest-types/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + } +}