diff --git a/e2e/__tests__/coverageReport.test.ts b/e2e/__tests__/coverageReport.test.ts index bb72df69521b..13765ec0dade 100644 --- a/e2e/__tests__/coverageReport.test.ts +++ b/e2e/__tests__/coverageReport.test.ts @@ -30,7 +30,7 @@ test('outputs coverage report', () => { // with 0 % coverage. expect(wrap(stdout)).toMatchSnapshot(); - expect(() => fs.accessSync(coverageDir, fs.F_OK)).not.toThrow(); + expect(() => fs.accessSync(coverageDir, fs.constants.F_OK)).not.toThrow(); expect(exitCode).toBe(0); }); diff --git a/e2e/__tests__/env.test.ts b/e2e/__tests__/env.test.ts index 3d7e2193665a..7555335e0853 100644 --- a/e2e/__tests__/env.test.ts +++ b/e2e/__tests__/env.test.ts @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import runJest from '../runJest'; +import runJest, {RunJestResult} from '../runJest'; -const getLog = result => result.stdout.split('\n')[1].trim(); +const getLog = (result: RunJestResult) => result.stdout.split('\n')[1].trim(); describe('Environment override', () => { it('uses jsdom when specified', () => { diff --git a/e2e/__tests__/hasteMapMockChanged.test.ts b/e2e/__tests__/hasteMapMockChanged.test.ts index 1e1427b0aacd..355534913066 100644 --- a/e2e/__tests__/hasteMapMockChanged.test.ts +++ b/e2e/__tests__/hasteMapMockChanged.test.ts @@ -6,7 +6,7 @@ */ import * as path from 'path'; -import JestHasteMap from 'jest-haste-map'; +import JestHasteMap = require('jest-haste-map'); import {cleanup, writeFiles} from '../Utils'; // Directory must be here for Watchman to be enabled. diff --git a/e2e/__tests__/hasteMapSha1.test.ts b/e2e/__tests__/hasteMapSha1.test.ts index c611da6172ba..b3005a4ac806 100644 --- a/e2e/__tests__/hasteMapSha1.test.ts +++ b/e2e/__tests__/hasteMapSha1.test.ts @@ -7,7 +7,7 @@ import {tmpdir} from 'os'; import * as path from 'path'; -import JestHasteMap from 'jest-haste-map'; +import JestHasteMap = require('jest-haste-map'); import {cleanup, writeFiles} from '../Utils'; const DIR = path.resolve(tmpdir(), 'haste_map_sha1'); diff --git a/e2e/__tests__/jsonReporter.test.ts b/e2e/__tests__/jsonReporter.test.ts index 0f8c9068d687..92fb4d6cd97c 100644 --- a/e2e/__tests__/jsonReporter.test.ts +++ b/e2e/__tests__/jsonReporter.test.ts @@ -7,6 +7,7 @@ import * as fs from 'fs'; import * as path from 'path'; +import type {FormattedTestResults} from '@jest/test-result'; import runJest from '../runJest'; describe('JSON Reporter', () => { @@ -22,7 +23,7 @@ describe('JSON Reporter', () => { }); it('writes test result to sum.result.json', () => { - let jsonResult; + let jsonResult: FormattedTestResults; runJest('json-reporter', ['--json', `--outputFile=${outputFileName}`]); const testOutput = fs.readFileSync(outputFilePath, 'utf8'); @@ -45,7 +46,7 @@ describe('JSON Reporter', () => { const noAncestors = jsonResult.testResults[0].assertionResults.find( item => item.title == 'no ancestors', ); - let expected = {ancestorTitles: []}; + let expected = {ancestorTitles: [] as Array}; expect(noAncestors).toEqual(expect.objectContaining(expected)); const addsNumbers = jsonResult.testResults[0].assertionResults.find( @@ -63,7 +64,7 @@ describe('JSON Reporter', () => { it('outputs coverage report', () => { const result = runJest('json-reporter', ['--json']); - let jsonResult; + let jsonResult: FormattedTestResults; expect(result.stderr).toMatch(/1 failed, 2 passed/); expect(result.exitCode).toBe(1); @@ -86,7 +87,7 @@ describe('JSON Reporter', () => { const noAncestors = jsonResult.testResults[0].assertionResults.find( item => item.title == 'no ancestors', ); - let expected = {ancestorTitles: []}; + let expected = {ancestorTitles: [] as Array}; expect(noAncestors).toEqual(expect.objectContaining(expected)); const addsNumbers = jsonResult.testResults[0].assertionResults.find( diff --git a/e2e/__tests__/listTests.test.ts b/e2e/__tests__/listTests.test.ts index 73923613d29c..66ff464681e3 100644 --- a/e2e/__tests__/listTests.test.ts +++ b/e2e/__tests__/listTests.test.ts @@ -11,7 +11,7 @@ import runJest from '../runJest'; const testRootDir = path.resolve(__dirname, '..', '..'); -const normalizePaths = rawPaths => +const normalizePaths = (rawPaths: string) => rawPaths .split(testRootDir) .join(`${path.sep}MOCK_ABOLUTE_PATH`) diff --git a/e2e/__tests__/nodePath.test.ts b/e2e/__tests__/nodePath.test.ts index 8c3bbe8d2b8a..b519f19b8102 100644 --- a/e2e/__tests__/nodePath.test.ts +++ b/e2e/__tests__/nodePath.test.ts @@ -9,7 +9,7 @@ import runJest from '../runJest'; test('supports NODE_PATH', () => { const result = runJest('node-path', [], { - nodePath: ['../node-path/src'], + nodePath: '../node-path/src', }); expect(result.exitCode).toBe(0); }); diff --git a/e2e/__tests__/overrideGlobals.test.ts b/e2e/__tests__/overrideGlobals.test.ts index 19d3391fea37..56dca22dd05d 100644 --- a/e2e/__tests__/overrideGlobals.test.ts +++ b/e2e/__tests__/overrideGlobals.test.ts @@ -18,7 +18,7 @@ test('has a duration even if time is faked', () => { expect(stderr).toMatch(regex); - const [, duration] = stderr.match(regex); + const [, duration] = stderr.match(regex)!; expect(Number(duration)).toBeGreaterThan(0); }); diff --git a/e2e/__tests__/showConfig.test.ts b/e2e/__tests__/showConfig.test.ts index 766406f411af..cf3024d75550 100644 --- a/e2e/__tests__/showConfig.test.ts +++ b/e2e/__tests__/showConfig.test.ts @@ -37,8 +37,8 @@ test('--showConfig outputs config info and exits', () => { .replace(/"name": "(.+)"/g, '"name": "[md5 hash]"') .replace(/"version": "(.+)"/g, '"version": "[version]"') .replace(/"maxWorkers": (\d+)/g, '"maxWorkers": "[maxWorkers]"') - .replace(/\"\S*show-config-test/gm, '"<>') - .replace(/\"\S*\/jest\/packages/gm, '"<>'); + .replace(/"\S*show-config-test/gm, '"<>') + .replace(/"\S*\/jest\/packages/gm, '"<>'); expect(wrap(stdout)).toMatchSnapshot(); }); diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 6783ba921969..da6202fa1e41 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -7,7 +7,7 @@ import {json as runWithJson} from '../runJest'; -import testFixturePackage from '../test-environment/package.json'; +import * as testFixturePackage from '../test-environment/package.json'; it('respects testEnvironment docblock', () => { expect(testFixturePackage.jest.testEnvironment).toEqual('node'); diff --git a/e2e/__tests__/toMatchSnapshotWithStringSerializer.test.ts b/e2e/__tests__/toMatchSnapshotWithStringSerializer.test.ts index b96515af5415..1296b4961a69 100644 --- a/e2e/__tests__/toMatchSnapshotWithStringSerializer.test.ts +++ b/e2e/__tests__/toMatchSnapshotWithStringSerializer.test.ts @@ -16,7 +16,7 @@ const DIR = path.resolve( ); const TESTS_DIR = path.resolve(DIR, '__tests__'); -const readFile = filename => +const readFile = (filename: string) => fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8'); beforeEach(() => cleanup(TESTS_DIR)); diff --git a/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts b/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts index c795a8aeb14f..0dfb598115fd 100644 --- a/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts +++ b/e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts @@ -17,7 +17,7 @@ const DIR = path.resolve( ); const TESTS_DIR = path.resolve(DIR, '__tests__'); -const readFile = filename => +const readFile = (filename: string) => fs.readFileSync(path.join(TESTS_DIR, filename), 'utf8'); beforeEach(() => cleanup(TESTS_DIR)); diff --git a/e2e/__tests__/watchModeOnlyFailed.test.ts b/e2e/__tests__/watchModeOnlyFailed.test.ts index 1d8d1c0cd8bd..be7b80078319 100644 --- a/e2e/__tests__/watchModeOnlyFailed.test.ts +++ b/e2e/__tests__/watchModeOnlyFailed.test.ts @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({ test: val => typeof val === 'string' && val.includes('[s'), }); -const setupFiles = input => { +const setupFiles = (input: Array<{keys: Array}>) => { writeFiles(DIR, { '__tests__/bar.spec.js': ` test('bar 1', () => { expect('bar').toBe('foo'); }); diff --git a/e2e/__tests__/watchModePatterns.test.ts b/e2e/__tests__/watchModePatterns.test.ts index 7d4302f7e8e0..7d39c7045adf 100644 --- a/e2e/__tests__/watchModePatterns.test.ts +++ b/e2e/__tests__/watchModePatterns.test.ts @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({ test: val => typeof val === 'string' && val.includes('[s'), }); -const setupFiles = input => { +const setupFiles = (input: Array<{keys: Array}>) => { writeFiles(DIR, { '__tests__/bar.spec.js': ` test('bar 1', () => { expect('bar').toBe('bar'); }); diff --git a/e2e/__tests__/watchModeUpdateSnapshot.test.ts b/e2e/__tests__/watchModeUpdateSnapshot.test.ts index 30477fe27e1c..27dea938525e 100644 --- a/e2e/__tests__/watchModeUpdateSnapshot.test.ts +++ b/e2e/__tests__/watchModeUpdateSnapshot.test.ts @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({ test: val => typeof val === 'string' && val.includes('[s'), }); -const setupFiles = input => { +const setupFiles = (input: Array<{keys: Array}>) => { writeFiles(DIR, { '__tests__/__snapshots__/bar.spec.js.snap': `// Jest Snapshot v1, https://goo.gl/fbAQLP diff --git a/e2e/__tests__/workerForceExit.test.ts b/e2e/__tests__/workerForceExit.test.ts index 7cdda47f0a96..884b2987c953 100644 --- a/e2e/__tests__/workerForceExit.test.ts +++ b/e2e/__tests__/workerForceExit.test.ts @@ -7,7 +7,7 @@ import {tmpdir} from 'os'; import {resolve} from 'path'; -import findProcess from 'find-process'; +import findProcess = require('find-process'); import { cleanup, @@ -27,7 +27,7 @@ const testFiles = { }`, }; -const verifyNumPassed = stderr => { +const verifyNumPassed = (stderr: string) => { const numberOfTestsPassed = (stderr.match(/\bPASS\b/g) || []).length; // assuming -1 because of package.json, but +1 because of the individual test file expect(numberOfTestsPassed).toBe(Object.keys(testFiles).length); @@ -64,6 +64,11 @@ test('force exits a worker that fails to exit gracefully', async () => { expect(exitCode).toBe(0); verifyNumPassed(stderr); - const [pid] = /pid: \d+/.exec(stderr); + const execRes = /pid: \d+/.exec(stderr); + + expect(execRes).toHaveLength(1); + + const [pid] = execRes!; + expect(await findProcess('pid', pid)).toHaveLength(0); }); diff --git a/e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts b/e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts index 591431f716e4..4b8129acca0d 100644 --- a/e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts +++ b/e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts @@ -6,7 +6,7 @@ * */ -import {Color} from '../types'; +import type {Color} from '../types'; import {color} from '../entry'; jest.mock('some-module', () => ({} as Partial<{}>), {virtual: true}); diff --git a/e2e/runJest.ts b/e2e/runJest.ts index 8f2b0e8d30f8..e35d11ec778c 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -31,7 +31,7 @@ export default function runJest( dir: string, args?: Array, options: RunJestOptions = {}, -) { +): RunJestResult { return normalizeStdoutAndStderr(spawnJest(dir, args, options), options); } @@ -40,7 +40,7 @@ function spawnJest( args?: Array, options?: RunJestOptions, spawnAsync?: false, -): execa.ExecaReturnValue; +): RunJestResult; function spawnJest( dir: string, args?: Array, @@ -51,7 +51,7 @@ function spawnJest( // Spawns Jest and returns either a Promise (if spawnAsync is true) or the completed child process function spawnJest( dir: string, - args?: Array, + args: Array = [], options: RunJestOptions = {}, spawnAsync: boolean = false, ): execa.ExecaSyncReturnValue | execa.ExecaChildProcess { @@ -77,7 +77,7 @@ function spawnJest( if (options.nodeOptions) env['NODE_OPTIONS'] = options.nodeOptions; if (options.nodePath) env['NODE_PATH'] = options.nodePath; - const spawnArgs = [JEST_PATH, ...(args || [])]; + const spawnArgs = [JEST_PATH, ...args]; const spawnOptions = { cwd: dir, env, @@ -92,14 +92,16 @@ function spawnJest( ); } -interface RunJestJsonResult extends execa.ExecaReturnValue { +export type RunJestResult = execa.ExecaReturnValue; + +interface RunJestJsonResult extends RunJestResult { json: FormattedTestResults; } function normalizeStdoutAndStderr( - result: execa.ExecaReturnValue, + result: RunJestResult, options: RunJestOptions, -) { +): RunJestResult { result.stdout = normalizeIcons(result.stdout); if (options.stripAnsi) result.stdout = stripAnsi(result.stdout); result.stderr = normalizeIcons(result.stderr); @@ -114,7 +116,7 @@ function normalizeStdoutAndStderr( // 'numPendingTests', 'testResults' export const json = function ( dir: string, - args: Array | undefined, + args?: Array, options: RunJestOptions = {}, ): RunJestJsonResult { args = [...(args || []), '--json']; diff --git a/e2e/tsconfig.json b/e2e/tsconfig.json new file mode 100644 index 000000000000..a25747da2a2d --- /dev/null +++ b/e2e/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "noEmit": true, + + "target": "es2017", + "module": "commonjs", + "lib": ["dom", "es2017"], + "strict": true, + + /* Additional Checks */ + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + + /* Module Resolution Options */ + "moduleResolution": "node", + "isolatedModules": true, + "importsNotUsedAsValues": "error", + "resolveJsonModule": true + } +}