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

General Type Maintenance #8462

Merged
merged 5 commits into from
May 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ const IDVisitor = {
blacklist: ['TypeAnnotation', 'TSTypeAnnotation', 'TSTypeReference'],
};

const FUNCTIONS: {
[key: string]: (args: Array<NodePath>) => boolean;
} = Object.create(null);
const FUNCTIONS: Record<
string,
(args: Array<NodePath>) => boolean
> = Object.create(null);

FUNCTIONS.mock = (args: Array<NodePath>) => {
if (args.length === 1) {
Expand Down
6 changes: 3 additions & 3 deletions packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, any>> {
for (const property in this.sample) {
if (
hasProperty(other, property) &&
equals((this.sample as any)[property], other[property]) &&
!emptyObject((this.sample as any)[property]) &&
equals(this.sample[property], other[property]) &&
!emptyObject(this.sample[property]) &&
!emptyObject(other[property])
) {
return false;
Expand All @@ -171,7 +171,7 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, any>> {
for (const property in this.sample) {
if (
!hasProperty(other, property) ||
!equals((this.sample as any)[property], other[property])
!equals(this.sample[property], other[property])
) {
return false;
}
Expand Down
9 changes: 3 additions & 6 deletions packages/expect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ const makeThrowingMatcher = (

const handlError = (error: Error) => {
if (
(matcher as any)[INTERNAL_MATCHER_FLAG] === true &&
matcher[INTERNAL_MATCHER_FLAG] === true &&
!(error instanceof JestAssertionError) &&
error.name !== 'PrettyFormatPluginError' &&
// Guard for some environments (browsers) that do not support this feature.
Expand All @@ -314,10 +314,7 @@ const makeThrowingMatcher = (
let potentialResult: ExpectationResult;

try {
potentialResult = matcher.apply(
matcherContext,
([actual] as any).concat(args),
);
potentialResult = matcher.call(matcherContext, actual, ...args);
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved

if (isPromise(potentialResult)) {
const asyncResult = potentialResult as AsyncExpectationResult;
Expand All @@ -340,7 +337,7 @@ const makeThrowingMatcher = (
};

expect.extend = (matchers: MatchersObject): void =>
setMatchers(matchers, false, expect as any);
setMatchers(matchers, false, expect);

expect.anything = anything;
expect.any = any;
Expand Down
3 changes: 2 additions & 1 deletion packages/expect/src/jasmineUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ function keys(
return keys.concat(
(Object.getOwnPropertySymbols(o) as Array<any>).filter(
symbol =>
(Object.getOwnPropertyDescriptor(o, symbol) as any).enumerable,
(Object.getOwnPropertyDescriptor(o, symbol) as PropertyDescriptor)
.enumerable,
),
);
})(obj);
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/jestMatchersObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
// Jest may override the stack trace of Errors thrown by internal matchers.
export const INTERNAL_MATCHER_FLAG = Symbol.for('$$jest-internal-matcher');

if (!(global as any)[JEST_MATCHERS_OBJECT]) {
if (!global.hasOwnProperty(JEST_MATCHERS_OBJECT)) {
Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
value: {
matchers: Object.create(null),
Expand Down
10 changes: 5 additions & 5 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import {Config} from '@jest/types';
import * as jestMatcherUtils from 'jest-matcher-utils';
import {INTERNAL_MATCHER_FLAG} from './jestMatchersObject';

export type SyncExpectationResult = {
pass: boolean;
Expand All @@ -17,11 +18,10 @@ export type AsyncExpectationResult = Promise<SyncExpectationResult>;

export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;

export type RawMatcherFn = (
received: any,
expected: any,
options?: any,
) => ExpectationResult;
export type RawMatcherFn = {
(received: any, expected: any, options?: any): ExpectationResult;
[INTERNAL_MATCHER_FLAG]?: boolean;
};

export type ThrowingMatcherFn = (actual: any) => void;
export type PromiseMatcherFn = (actual: any) => Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions packages/expect/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const hasOwnProperty = (object: object, key: string) =>
hasGetterFromConstructor(object, key);

export const getPath = (
object: object,
object: Record<string, any>,
propertyPath: string | Array<string>,
): GetPath => {
if (!Array.isArray(propertyPath)) {
Expand All @@ -59,7 +59,7 @@ export const getPath = (
if (propertyPath.length) {
const lastProp = propertyPath.length === 1;
const prop = propertyPath[0];
const newObject = (object as any)[prop];
const newObject = object[prop];

if (!lastProp && (newObject === null || newObject === undefined)) {
// This is not the last prop in the chain. If we keep recursing it will
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ interface AssertionErrorWithStack extends AssertionError {
stack: string;
}

const assertOperatorsMap: {[key: string]: string} = {
const assertOperatorsMap: Record<string, string> = {
'!=': 'notEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'===': 'strictEqual',
};

const humanReadableOperators: {[key: string]: string} = {
const humanReadableOperators: Record<string, string> = {
deepEqual: 'to deeply equal',
deepStrictEqual: 'to deeply and strictly equal',
equal: 'to be equal',
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/init/generate_config_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const stringifyOption = (
);
};

const generateConfigFile = (results: {[key: string]: unknown}): string => {
const generateConfigFile = (results: Record<string, unknown>): string => {
const {coverage, clearMocks, environment} = results;

const overrides: Record<string, any> = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/init/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import {Config} from '@jest/types';

export type ProjectPackageJson = {
jest?: Partial<Config.InitialOptions>;
scripts?: {[key: string]: string};
scripts?: Record<string, string>;
};
2 changes: 1 addition & 1 deletion packages/jest-config/src/setFromArgv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function setFromArgv(
): Config.InitialOptions {
const argvToOptions = Object.keys(argv)
.filter(key => argv[key] !== undefined && specialArgs.indexOf(key) === -1)
.reduce((options: {[key: string]: unknown}, key) => {
.reduce((options: Record<string, unknown>, key) => {
switch (key) {
case 'coverage':
options.collectCoverage = argv[key];
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const _replaceRootDirInObject = <T extends ReplaceRootDirConfigObj>(
};

type OrArray<T> = T | Array<T>;
type ReplaceRootDirConfigObj = {[key: string]: Config.Path};
type ReplaceRootDirConfigObj = Record<string, Config.Path>;
type ReplaceRootDirConfigValues =
| OrArray<ReplaceRootDirConfigObj>
| OrArray<RegExp>
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/FailedTestsCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Test} from 'jest-runner';
import {Config} from '@jest/types';
import {TestResult} from '@jest/test-result';

type TestMap = {[key: string]: {[key: string]: boolean}};
type TestMap = Record<string, Record<string, boolean>>;

export default class FailedTestsCache {
private _enabledTestsMap?: TestMap;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export default class TestScheduler {
}

private _partitionTests(
testRunners: {[key: string]: TestRunner},
testRunners: Record<string, TestRunner>,
tests: Array<Test>,
) {
if (Object.keys(testRunners).length > 1) {
Expand Down Expand Up @@ -327,7 +327,7 @@ export default class TestScheduler {
*/
private _getReporterProps(
reporter: string | Config.ReporterConfig,
): {path: string; options: {[key: string]: unknown}} {
): {path: string; options: Record<string, unknown>} {
if (typeof reporter === 'string') {
return {options: this._options, path: reporter};
} else if (Array.isArray(reporter)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/getNoTestFoundVerbose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function getNoTestFoundVerbose(
if (key === 'roots' && config.roots.length === 1) {
return null;
}
const value = (config as {[key: string]: unknown})[key];
const value = (config as Record<string, unknown>)[key];
if (value) {
const valueAsString = Array.isArray(value)
? value.join(', ')
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-docblock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {EOL} from 'os';
import detectNewline from 'detect-newline';

type Pragmas = {[key: string]: string | Array<string>};
type Pragmas = Record<string, string | Array<string>>;

const commentEndRe = /\*\/$/;
const commentStartRe = /^\/\*\*/;
Expand Down
47 changes: 41 additions & 6 deletions packages/jest-each/src/table/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {isPrimitive} from 'jest-get-type';
import {Global} from '@jest/types';
import {EachTests} from '../bind';

type Template = {[key: string]: unknown};
type Template = Record<string, unknown>;
type Templates = Array<Template>;
type Headings = Array<string>;

Expand Down Expand Up @@ -70,11 +70,46 @@ const replaceKeyPathWithValue = (template: Template) => (
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};

const getPath = (
template: Template | any,
/* eslint import/export: 0*/
export function getPath<
Obj extends Template,
A extends keyof Obj,
B extends keyof Obj[A],
C extends keyof Obj[A][B],
D extends keyof Obj[A][B][C],
E extends keyof Obj[A][B][C][D]
>(obj: Obj, path: [A, B, C, D, E]): Obj[A][B][C][D][E];
export function getPath<
Obj extends Template,
A extends keyof Obj,
B extends keyof Obj[A],
C extends keyof Obj[A][B],
D extends keyof Obj[A][B][C]
>(obj: Obj, path: [A, B, C, D]): Obj[A][B][C][D];
export function getPath<
Obj extends Template,
A extends keyof Obj,
B extends keyof Obj[A],
C extends keyof Obj[A][B]
>(obj: Obj, path: [A, B, C]): Obj[A][B][C];
export function getPath<
Obj extends Template,
A extends keyof Obj,
B extends keyof Obj[A]
>(obj: Obj, path: [A, B]): Obj[A][B];
export function getPath<Obj extends Template, A extends keyof Obj>(
obj: Obj,
path: [A],
): Obj[A];
export function getPath<Obj extends Template>(
obj: Obj,
path: Array<string>,
): unknown;
export function getPath(
template: Template,
[head, ...tail]: Array<string>,
): any => {
): unknown {
if (!head || !template.hasOwnProperty || !template.hasOwnProperty(head))
return template;
return getPath(template[head], tail);
};
return getPath(template[head] as Template, tail);
}
2 changes: 1 addition & 1 deletion packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type JestMockSpyOn = typeof jestMock.spyOn;
// passed, or not. The context itself is optional, not properties within it.
export type EnvironmentContext = Partial<{
console: Console;
docblockPragmas: {[key: string]: string | Array<string>};
docblockPragmas: Record<string, string | Array<string>>;
testPath: Config.Path;
}>;

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-fake-timers/src/jestFakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const setGlobal = (
};

export default class FakeTimers<TimerRef> {
private _cancelledImmediates!: {[key: string]: boolean};
private _cancelledTicks!: {[key: string]: boolean};
private _cancelledImmediates!: Record<string, boolean>;
private _cancelledTicks!: Record<string, boolean>;
private _config: StackTraceConfig;
private _disposed?: boolean;
private _fakeTimerAPIs!: TimerAPI;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/ModuleMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import * as fastPath from './lib/fast_path';
import H from './constants';

const EMPTY_OBJ = {} as {[key: string]: any};
const EMPTY_OBJ = {} as Record<string, any>;
JoshRosenstein marked this conversation as resolved.
Show resolved Hide resolved
const EMPTY_MAP = new Map();

type ValueType<T> = T extends Map<string, infer V> ? V : never;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
import chalk from 'chalk';
import {AssertionErrorWithStack} from './types';

const assertOperatorsMap: {[key: string]: string} = {
const assertOperatorsMap: Record<string, string> = {
'!=': 'notEqual',
'!==': 'notStrictEqual',
'==': 'equal',
'===': 'strictEqual',
};

const humanReadableOperators: {[key: string]: string} = {
const humanReadableOperators: Record<string, string> = {
deepEqual: 'to deeply equal',
deepStrictEqual: 'to deeply and strictly equal',
equal: 'to be equal',
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function(j$: Jasmine) {
) => Promise<void>;
fdescribe: (description: string, specDefinitions: Function) => Suite;
spyOn: (
obj: {[key: string]: any},
obj: Record<string, any>,
methodName: string,
accessType?: keyof PropertyDescriptor,
) => Spy;
Expand Down Expand Up @@ -94,7 +94,7 @@ export default function(j$: Jasmine) {
const realSetTimeout = global.setTimeout;
const realClearTimeout = global.clearTimeout;

const runnableResources: {[key: string]: {spies: Array<Spy>}} = {};
const runnableResources: Record<string, {spies: Array<Spy>}> = {};
const currentlyExecutingSuites: Array<Suite> = [];
let currentSpec: Spec | null = null;
let throwOnExpectationFailure = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/jasmine/JsApiReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class JsApiReporter implements Reporter {
suiteStarted: (result: SuiteResult) => void;
suiteDone: (result: SuiteResult) => void;
suiteResults: (index: number, length: number) => Array<SuiteResult>;
suites: () => {[key: string]: SuiteResult};
suites: () => Record<string, SuiteResult>;

specResults: (index: number, length: number) => Array<SpecResult>;
specDone: (result: SpecResult) => void;
Expand Down Expand Up @@ -95,7 +95,7 @@ export default class JsApiReporter implements Reporter {
};

const suites: Array<SuiteResult> = [];
const suites_hash: {[key: string]: SuiteResult} = {};
const suites_hash: Record<string, SuiteResult> = {};

this.specStarted = function() {};

Expand Down
3 changes: 1 addition & 2 deletions packages/jest-jasmine2/src/jasmine/createSpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ import {Spy} from '../types';
import CallTracker, {Context} from './CallTracker';
import SpyStrategy from './SpyStrategy';

interface Fn {
interface Fn extends Record<string, any> {
(): any;
[key: string]: any;
}

function createSpy(name: string, originalFn: Fn): Spy {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-jasmine2/src/jasmine/spyRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ const getErrorMsg = formatErrorMsg('<spyOn>', 'spyOn(<object>, <methodName>)');
export default class SpyRegistry {
allowRespy: (allow: unknown) => void;
spyOn: (
obj: {[key: string]: any},
obj: Record<string, any>,
methodName: string,
accessType?: keyof PropertyDescriptor,
) => Spy;
clearSpies: () => void;
respy: unknown;

private _spyOnProperty: (
obj: {[key: string]: any},
obj: Record<string, any>,
propertyName: string,
accessType: keyof PropertyDescriptor,
) => Spy;
Expand Down
Loading