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: fix some type errors in e2e tests #9720

Merged
merged 2 commits into from
Mar 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/__tests__/coverageReport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapMockChanged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapSha1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
9 changes: 5 additions & 4 deletions e2e/__tests__/jsonReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
Expand All @@ -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<string>};
expect(noAncestors).toEqual(expect.objectContaining(expected));

const addsNumbers = jsonResult.testResults[0].assertionResults.find(
Expand All @@ -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);
Expand All @@ -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<string>};
expect(noAncestors).toEqual(expect.objectContaining(expected));

const addsNumbers = jsonResult.testResults[0].assertionResults.find(
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/listTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/nodePath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
2 changes: 1 addition & 1 deletion e2e/__tests__/overrideGlobals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
4 changes: 2 additions & 2 deletions e2e/__tests__/showConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '"<<REPLACED_ROOT_DIR>>')
.replace(/\"\S*\/jest\/packages/gm, '"<<REPLACED_JEST_PACKAGES_DIR>>');
.replace(/"\S*show-config-test/gm, '"<<REPLACED_ROOT_DIR>>')
.replace(/"\S*\/jest\/packages/gm, '"<<REPLACED_JEST_PACKAGES_DIR>>');

expect(wrap(stdout)).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion e2e/__tests__/testEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/toMatchSnapshotWithStringSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/toThrowErrorMatchingInlineSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/watchModeOnlyFailed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({
test: val => typeof val === 'string' && val.includes('[s'),
});

const setupFiles = input => {
const setupFiles = (input: Array<{keys: Array<string>}>) => {
writeFiles(DIR, {
'__tests__/bar.spec.js': `
test('bar 1', () => { expect('bar').toBe('foo'); });
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/watchModePatterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({
test: val => typeof val === 'string' && val.includes('[s'),
});

const setupFiles = input => {
const setupFiles = (input: Array<{keys: Array<string>}>) => {
writeFiles(DIR, {
'__tests__/bar.spec.js': `
test('bar 1', () => { expect('bar').toBe('bar'); });
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/watchModeUpdateSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ expect.addSnapshotSerializer({
test: val => typeof val === 'string' && val.includes('[s'),
});

const setupFiles = input => {
const setupFiles = (input: Array<{keys: Array<string>}>) => {
writeFiles(DIR, {
'__tests__/__snapshots__/bar.spec.js.snap': `// Jest Snapshot v1, https://goo.gl/fbAQLP

Expand Down
11 changes: 8 additions & 3 deletions e2e/__tests__/workerForceExit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {tmpdir} from 'os';
import {resolve} from 'path';
import findProcess from 'find-process';
import findProcess = require('find-process');

import {
cleanup,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});
2 changes: 1 addition & 1 deletion e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
18 changes: 10 additions & 8 deletions e2e/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function runJest(
dir: string,
args?: Array<string>,
options: RunJestOptions = {},
) {
): RunJestResult {
return normalizeStdoutAndStderr(spawnJest(dir, args, options), options);
}

Expand All @@ -40,7 +40,7 @@ function spawnJest(
args?: Array<string>,
options?: RunJestOptions,
spawnAsync?: false,
): execa.ExecaReturnValue;
): RunJestResult;
function spawnJest(
dir: string,
args?: Array<string>,
Expand All @@ -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<string>,
args: Array<string> = [],
options: RunJestOptions = {},
spawnAsync: boolean = false,
): execa.ExecaSyncReturnValue | execa.ExecaChildProcess {
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -114,7 +116,7 @@ function normalizeStdoutAndStderr(
// 'numPendingTests', 'testResults'
export const json = function (
dir: string,
args: Array<string> | undefined,
args?: Array<string>,
options: RunJestOptions = {},
): RunJestJsonResult {
args = [...(args || []), '--json'];
Expand Down
22 changes: 22 additions & 0 deletions e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}