From fa520b3a3691b948881847d66c0627ff3e810f5e Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Thu, 21 May 2020 22:11:03 +0200 Subject: [PATCH 1/4] Migrate some tests to TS --- ...t.js.snap => assertionCounts.test.ts.snap} | 0 ...xtend.test.js.snap => extend.test.ts.snap} | 0 ....test.js.snap => spyMatchers.test.ts.snap} | 0 ...Counts.test.js => assertionCounts.test.ts} | 2 - .../{extend.test.js => extend.test.ts} | 15 +++--- .../{fakeChalk.test.js => fakeChalk.test.ts} | 1 - .../{isError.test.js => isError.test.ts} | 10 ++-- ...pyMatchers.test.js => spyMatchers.test.ts} | 30 ++++++------ ...{stacktrace.test.js => stacktrace.test.ts} | 12 ++--- ...bjects.test.js => symbolInObjects.test.ts} | 2 - ...oEqual-dom.test.js => toEqual-dom.test.ts} | 6 +-- .../{utils.test.js => utils.test.ts} | 49 +++++++++++-------- 12 files changed, 66 insertions(+), 61 deletions(-) rename packages/expect/src/__tests__/__snapshots__/{assertionCounts.test.js.snap => assertionCounts.test.ts.snap} (100%) rename packages/expect/src/__tests__/__snapshots__/{extend.test.js.snap => extend.test.ts.snap} (100%) rename packages/expect/src/__tests__/__snapshots__/{spyMatchers.test.js.snap => spyMatchers.test.ts.snap} (100%) rename packages/expect/src/__tests__/{assertionCounts.test.js => assertionCounts.test.ts} (98%) rename packages/expect/src/__tests__/{extend.test.js => extend.test.ts} (90%) rename packages/expect/src/__tests__/{fakeChalk.test.js => fakeChalk.test.ts} (97%) rename packages/expect/src/__tests__/{isError.test.js => isError.test.ts} (81%) rename packages/expect/src/__tests__/{spyMatchers.test.js => spyMatchers.test.ts} (97%) rename packages/expect/src/__tests__/{stacktrace.test.js => stacktrace.test.ts} (77%) rename packages/expect/src/__tests__/{symbolInObjects.test.js => symbolInObjects.test.ts} (98%) rename packages/expect/src/__tests__/{toEqual-dom.test.js => toEqual-dom.test.ts} (96%) rename packages/expect/src/__tests__/{utils.test.js => utils.test.ts} (91%) diff --git a/packages/expect/src/__tests__/__snapshots__/assertionCounts.test.js.snap b/packages/expect/src/__tests__/__snapshots__/assertionCounts.test.ts.snap similarity index 100% rename from packages/expect/src/__tests__/__snapshots__/assertionCounts.test.js.snap rename to packages/expect/src/__tests__/__snapshots__/assertionCounts.test.ts.snap diff --git a/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap b/packages/expect/src/__tests__/__snapshots__/extend.test.ts.snap similarity index 100% rename from packages/expect/src/__tests__/__snapshots__/extend.test.js.snap rename to packages/expect/src/__tests__/__snapshots__/extend.test.ts.snap diff --git a/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap similarity index 100% rename from packages/expect/src/__tests__/__snapshots__/spyMatchers.test.js.snap rename to packages/expect/src/__tests__/__snapshots__/spyMatchers.test.ts.snap diff --git a/packages/expect/src/__tests__/assertionCounts.test.js b/packages/expect/src/__tests__/assertionCounts.test.ts similarity index 98% rename from packages/expect/src/__tests__/assertionCounts.test.js rename to packages/expect/src/__tests__/assertionCounts.test.ts index ce9c31d3fff4..0a6792137176 100644 --- a/packages/expect/src/__tests__/assertionCounts.test.js +++ b/packages/expect/src/__tests__/assertionCounts.test.ts @@ -6,8 +6,6 @@ * */ -'use strict'; - const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); const jestExpect = require('../'); diff --git a/packages/expect/src/__tests__/extend.test.js b/packages/expect/src/__tests__/extend.test.ts similarity index 90% rename from packages/expect/src/__tests__/extend.test.js rename to packages/expect/src/__tests__/extend.test.ts index 11b915332fee..ac0cb66b7238 100644 --- a/packages/expect/src/__tests__/extend.test.js +++ b/packages/expect/src/__tests__/extend.test.ts @@ -6,7 +6,7 @@ * */ -const matcherUtils = require('jest-matcher-utils'); +import * as matcherUtils from 'jest-matcher-utils'; const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); const {iterableEquality, subsetEquality} = require('../utils'); const {equals} = require('../jasmineUtils'); @@ -15,7 +15,7 @@ const jestExpect = require('../'); expect.addSnapshotSerializer(alignedAnsiStyleSerializer); jestExpect.extend({ - toBeDivisibleBy(actual, expected) { + toBeDivisibleBy(actual: number, expected: number) { const pass = actual % expected === 0; const message = pass ? () => `expected ${actual} not to be divisible by ${expected}` @@ -23,13 +23,14 @@ jestExpect.extend({ return {message, pass}; }, - toBeSymbol(actual, expected) { + toBeSymbol(actual: symbol, expected: symbol) { const pass = actual === expected; - const message = () => `expected ${actual} to be Symbol ${expected}`; + const message = () => + `expected ${actual.toString()} to be Symbol ${expected.toString()}`; return {message, pass}; }, - toBeWithinRange(actual, floor, ceiling) { + toBeWithinRange(actual: number, floor: number, ceiling: number) { const pass = actual >= floor && actual <= ceiling; const message = pass ? () => `expected ${actual} not to be within range ${floor} - ${ceiling}` @@ -60,7 +61,7 @@ it('is available globally when matcher is variadic', () => { it('exposes matcherUtils in context', () => { jestExpect.extend({ - _shouldNotError(actual, expected) { + _shouldNotError(_actual: unknown, _expected: unknown) { const pass = this.equals( this.utils, Object.assign(matcherUtils, { @@ -81,7 +82,7 @@ it('exposes matcherUtils in context', () => { it('is ok if there is no message specified', () => { jestExpect.extend({ - toFailWithoutMessage(expected) { + toFailWithoutMessage(_expected: unknown) { return {pass: false}; }, }); diff --git a/packages/expect/src/__tests__/fakeChalk.test.js b/packages/expect/src/__tests__/fakeChalk.test.ts similarity index 97% rename from packages/expect/src/__tests__/fakeChalk.test.js rename to packages/expect/src/__tests__/fakeChalk.test.ts index e8dd2ea5c340..4fbb6cebef2f 100644 --- a/packages/expect/src/__tests__/fakeChalk.test.js +++ b/packages/expect/src/__tests__/fakeChalk.test.ts @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict'; const fakeChalk = jest.requireActual('../fakeChalk'); diff --git a/packages/expect/src/__tests__/isError.test.js b/packages/expect/src/__tests__/isError.test.ts similarity index 81% rename from packages/expect/src/__tests__/isError.test.js rename to packages/expect/src/__tests__/isError.test.ts index 44192188aa51..0bdecd6e70f3 100644 --- a/packages/expect/src/__tests__/isError.test.js +++ b/packages/expect/src/__tests__/isError.test.ts @@ -13,14 +13,16 @@ import {isError} from '../utils'; // Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883 describe('isError', () => { - function testErrorFromDifferentContext(createError) { + function testErrorFromDifferentContext(createError: Function) { const iframe = document.createElement('iframe'); document.body.appendChild(iframe); try { const error = createError(iframe.contentWindow); expect(isError(error)).toBe(true); } finally { - iframe.parentElement.removeChild(iframe); + if (iframe && iframe.parentElement) { + iframe.parentElement.removeChild(iframe); + } } } @@ -34,11 +36,11 @@ describe('isError', () => { }); it('should detect errors from another context', () => { - testErrorFromDifferentContext(win => new win.Error()); + testErrorFromDifferentContext((win: any) => new win.Error()); }); it('should detect DOMException errors from another context', () => { - testErrorFromDifferentContext(win => { + testErrorFromDifferentContext((win: Window) => { try { win.document.querySelectorAll(''); } catch (e) { diff --git a/packages/expect/src/__tests__/spyMatchers.test.js b/packages/expect/src/__tests__/spyMatchers.test.ts similarity index 97% rename from packages/expect/src/__tests__/spyMatchers.test.js rename to packages/expect/src/__tests__/spyMatchers.test.ts index 1c3a5c956e65..1398bc0e5979 100644 --- a/packages/expect/src/__tests__/spyMatchers.test.js +++ b/packages/expect/src/__tests__/spyMatchers.test.ts @@ -5,14 +5,14 @@ * LICENSE file in the root directory of this source tree. */ -const Immutable = require('immutable'); +import * as Immutable from 'immutable'; const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); const jestExpect = require('../'); expect.addSnapshotSerializer(alignedAnsiStyleSerializer); // Given a Jest mock function, return a minimal mock of a Jasmine spy. -const createSpy = fn => { +const createSpy = (fn: jest.Mock) => { const spy = function () {}; spy.calls = { @@ -177,7 +177,7 @@ const createSpy = fn => { 'toBeCalledWith', 'toHaveBeenCalledWith', ].forEach(calledWith => { - const caller = function (callee, ...args) { + const caller = function (callee: Function, ...args: any) { if ( calledWith === 'nthCalledWith' || calledWith === 'toHaveBeenNthCalledWith' @@ -295,8 +295,8 @@ const createSpy = fn => { test(`works with Immutable.js objects`, () => { const fn = jest.fn(); - const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]); - const indirectlyCreated = new Immutable.Map().set('a', {b: 'c'}); + const directlyCreated = Immutable.Map([['a', {b: 'c'}]]); + const indirectlyCreated = Immutable.Map().set('a', {b: 'c'}); fn(directlyCreated, indirectlyCreated); caller(jestExpect(fn)[calledWith], indirectlyCreated, directlyCreated); @@ -534,7 +534,7 @@ const createSpy = fn => { test(`incomplete recursive calls are handled properly`, () => { // sums up all integers from 0 -> value, using recursion - const fn = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. @@ -703,7 +703,7 @@ const createSpy = fn => { test(`incomplete recursive calls are handled properly`, () => { // sums up all integers from 0 -> value, using recursion - const fn = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { return 0; } else { @@ -734,7 +734,7 @@ const createSpy = fn => { 'toReturnWith', 'toHaveReturnedWith', ].forEach(returnedWith => { - const caller = function (callee, ...args) { + const caller = function (callee: Function, ...args: any) { if ( returnedWith === 'nthReturnedWith' || returnedWith === 'toHaveNthReturnedWith' @@ -850,7 +850,7 @@ const createSpy = fn => { }); test(`works with Immutable.js objects directly created`, () => { - const directlyCreated = new Immutable.Map([['a', {b: 'c'}]]); + const directlyCreated = Immutable.Map([['a', {b: 'c'}]]); const fn = jest.fn(() => directlyCreated); fn(); @@ -862,7 +862,7 @@ const createSpy = fn => { }); test(`works with Immutable.js objects indirectly created`, () => { - const indirectlyCreated = new Immutable.Map().set('a', {b: 'c'}); + const indirectlyCreated = Immutable.Map().set('a', {b: 'c'}); const fn = jest.fn(() => indirectlyCreated); fn(); @@ -944,7 +944,7 @@ const createSpy = fn => { test(`incomplete recursive calls are handled properly`, () => { // sums up all integers from 0 -> value, using recursion - const fn = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. @@ -1032,7 +1032,7 @@ const createSpy = fn => { }); test('positive throw matcher error for n that is not integer', async () => { - const fn = jest.fn(() => 'foo'); + const fn: jest.Mock = jest.fn(() => 'foo'); fn('foo'); expect(() => { @@ -1041,7 +1041,7 @@ const createSpy = fn => { }); test('negative throw matcher error for n that is not number', async () => { - const fn = jest.fn(() => 'foo'); + const fn: jest.Mock = jest.fn(() => 'foo'); fn('foo'); expect(() => { @@ -1051,7 +1051,7 @@ const createSpy = fn => { test(`incomplete recursive calls are handled properly`, () => { // sums up all integers from 0 -> value, using recursion - const fn = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { return 0; } else { @@ -1108,7 +1108,7 @@ const createSpy = fn => { test(`incomplete recursive calls are handled properly`, () => { // sums up all integers from 0 -> value, using recursion - const fn = jest.fn(value => { + const fn: jest.Mock = jest.fn(value => { if (value === 0) { // Before returning from the base case of recursion, none of the // calls have returned yet. diff --git a/packages/expect/src/__tests__/stacktrace.test.js b/packages/expect/src/__tests__/stacktrace.test.ts similarity index 77% rename from packages/expect/src/__tests__/stacktrace.test.js rename to packages/expect/src/__tests__/stacktrace.test.ts index a2143257945e..6c39df02088f 100644 --- a/packages/expect/src/__tests__/stacktrace.test.js +++ b/packages/expect/src/__tests__/stacktrace.test.ts @@ -9,7 +9,7 @@ const jestExpect = require('../'); jestExpect.extend({ - toCustomMatch(callback, expectation) { + toCustomMatch(callback: Function, expectation: unknown) { const actual = callback(); if (actual !== expectation) { @@ -21,7 +21,7 @@ jestExpect.extend({ return {pass: true}; }, - toMatchPredicate(received, argument) { + toMatchPredicate(received: unknown, argument: Function) { argument(received); return { message: () => '', @@ -34,17 +34,17 @@ it('stack trace points to correct location when using matchers', () => { try { jestExpect(true).toBe(false); } catch (error) { - expect(error.stack).toContain('stacktrace.test.js:35'); + expect(error.stack).toContain('stacktrace.test.ts:35'); } }); it('stack trace points to correct location when using nested matchers', () => { try { - jestExpect(true).toMatchPredicate(value => { + jestExpect(true).toMatchPredicate((value: unknown) => { jestExpect(value).toBe(false); }); } catch (error) { - expect(error.stack).toContain('stacktrace.test.js:44'); + expect(error.stack).toContain('stacktrace.test.ts:44'); } }); @@ -60,6 +60,6 @@ it('stack trace points to correct location when throwing from a custom matcher', foo(); }).toCustomMatch('bar'); } catch (error) { - expect(error.stack).toContain('stacktrace.test.js:57'); + expect(error.stack).toContain('stacktrace.test.ts:57'); } }); diff --git a/packages/expect/src/__tests__/symbolInObjects.test.js b/packages/expect/src/__tests__/symbolInObjects.test.ts similarity index 98% rename from packages/expect/src/__tests__/symbolInObjects.test.js rename to packages/expect/src/__tests__/symbolInObjects.test.ts index 97a85417fc00..352208adbf59 100644 --- a/packages/expect/src/__tests__/symbolInObjects.test.js +++ b/packages/expect/src/__tests__/symbolInObjects.test.ts @@ -6,8 +6,6 @@ * */ -'use strict'; - describe('Symbol in objects', () => { test('should compare objects with Symbol keys', () => { const sym = Symbol('foo'); diff --git a/packages/expect/src/__tests__/toEqual-dom.test.js b/packages/expect/src/__tests__/toEqual-dom.test.ts similarity index 96% rename from packages/expect/src/__tests__/toEqual-dom.test.js rename to packages/expect/src/__tests__/toEqual-dom.test.ts index ed1f32ea6be4..605c7e628537 100644 --- a/packages/expect/src/__tests__/toEqual-dom.test.js +++ b/packages/expect/src/__tests__/toEqual-dom.test.ts @@ -12,18 +12,18 @@ describe('toEqual', () => { describe('duck type', () => { // https://github.com/facebook/jest/issues/7786 - const createElement = (name, ...childNodes) => ({ + const createElement = (name: string, ...childNodes: Array) => ({ childNodes, nodeType: 1, tagName: name.toUpperCase(), }); - const createTextNode = data => ({ + const createTextNode = (data: unknown) => ({ data, nodeType: 3, }); - const createDocumentFragment = (...children) => ({ + const createDocumentFragment = (...children: Array) => ({ children, nodeType: 11, }); diff --git a/packages/expect/src/__tests__/utils.test.js b/packages/expect/src/__tests__/utils.test.ts similarity index 91% rename from packages/expect/src/__tests__/utils.test.js rename to packages/expect/src/__tests__/utils.test.ts index 0eb4bdca4659..1d22d48fe8df 100644 --- a/packages/expect/src/__tests__/utils.test.js +++ b/packages/expect/src/__tests__/utils.test.ts @@ -6,9 +6,7 @@ * */ -'use strict'; - -const {stringify} = require('jest-matcher-utils'); +import {stringify} from 'jest-matcher-utils'; const { emptyObject, getObjectSubset, @@ -79,6 +77,7 @@ describe('getPath()', () => { test('property is inherited', () => { class A {} + // @ts-expect-error A.prototype.a = 'a'; expect(getPath(new A(), 'a')).toEqual({ @@ -120,7 +119,7 @@ describe('hasOwnProperty', () => { it('does not inherit setter from class', () => { class MyClass { - set key(value) {} + set key(_value: unknown) {} } expect(hasOwnProperty(new MyClass(), 'key')).toBe(false); }); @@ -135,6 +134,7 @@ describe('hasOwnProperty', () => { it('does not inherit property from constructor prototype', () => { function MyClass() {} MyClass.prototype.key = 'value'; + // @ts-expect-error expect(hasOwnProperty(new MyClass(), 'key')).toBe(false); }); @@ -208,18 +208,20 @@ describe('getObjectSubset', () => { describe('calculating subsets of objects with circular references', () => { test('simple circular references', () => { + type CircularObj = {a?: string; b?: string; ref?: unknown}; + const nonCircularObj = {a: 'world', b: 'something'}; - const circularObjA = {a: 'hello'}; + const circularObjA: CircularObj = {a: 'hello'}; circularObjA.ref = circularObjA; - const circularObjB = {a: 'world'}; + const circularObjB: CircularObj = {a: 'world'}; circularObjB.ref = circularObjB; - const primitiveInsteadOfRef = {b: 'something'}; + const primitiveInsteadOfRef: CircularObj = {b: 'something'}; primitiveInsteadOfRef.ref = 'not a ref'; - const nonCircularRef = {b: 'something'}; + const nonCircularRef: CircularObj = {b: 'something'}; nonCircularRef.ref = {}; expect(getObjectSubset(circularObjA, nonCircularObj)).toEqual({ @@ -240,18 +242,20 @@ describe('getObjectSubset', () => { }); test('transitive circular references', () => { + type CircularObj = {a?: string; nestedObj?: unknown}; + const nonCircularObj = {a: 'world', b: 'something'}; - const transitiveCircularObjA = {a: 'hello'}; + const transitiveCircularObjA: CircularObj = {a: 'hello'}; transitiveCircularObjA.nestedObj = {parentObj: transitiveCircularObjA}; - const transitiveCircularObjB = {a: 'world'}; + const transitiveCircularObjB: CircularObj = {a: 'world'}; transitiveCircularObjB.nestedObj = {parentObj: transitiveCircularObjB}; - const primitiveInsteadOfRef = {}; + const primitiveInsteadOfRef: CircularObj = {}; primitiveInsteadOfRef.nestedObj = {otherProp: 'not the parent ref'}; - const nonCircularRef = {}; + const nonCircularRef: CircularObj = {}; nonCircularRef.nestedObj = {otherProp: {}}; expect(getObjectSubset(transitiveCircularObjA, nonCircularObj)).toEqual({ @@ -314,16 +318,18 @@ describe('subsetEquality()', () => { describe('matching subsets with circular references', () => { test('simple circular references', () => { - const circularObjA1 = {a: 'hello'}; + type CircularObj = {a?: string; ref?: unknown}; + + const circularObjA1: CircularObj = {a: 'hello'}; circularObjA1.ref = circularObjA1; - const circularObjA2 = {a: 'hello'}; + const circularObjA2: CircularObj = {a: 'hello'}; circularObjA2.ref = circularObjA2; - const circularObjB = {a: 'world'}; + const circularObjB: CircularObj = {a: 'world'}; circularObjB.ref = circularObjB; - const primitiveInsteadOfRef = {}; + const primitiveInsteadOfRef: CircularObj = {}; primitiveInsteadOfRef.ref = 'not a ref'; expect(subsetEquality(circularObjA1, {})).toBe(true); @@ -347,21 +353,22 @@ describe('subsetEquality()', () => { }); test('transitive circular references', () => { - const transitiveCircularObjA1 = {a: 'hello'}; + type CircularObj = {a: string; nestedObj?: unknown}; + + const transitiveCircularObjA1: CircularObj = {a: 'hello'}; transitiveCircularObjA1.nestedObj = {parentObj: transitiveCircularObjA1}; - const transitiveCircularObjA2 = {a: 'hello'}; + const transitiveCircularObjA2: CircularObj = {a: 'hello'}; transitiveCircularObjA2.nestedObj = { parentObj: transitiveCircularObjA2, }; - const transitiveCircularObjB = {a: 'world'}; + const transitiveCircularObjB: CircularObj = {a: 'world'}; transitiveCircularObjB.nestedObj = { parentObj: transitiveCircularObjB, }; - const primitiveInsteadOfRef = {}; - primitiveInsteadOfRef.nestedObj = { + const primitiveInsteadOfRef = { parentObj: 'not the parent ref', }; From 0871a32def1b6e8eeff296de5b9bd29ba9b50ade Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Mon, 25 May 2020 11:40:06 +0200 Subject: [PATCH 2/4] Remove useless undefined check --- packages/expect/src/__tests__/isError.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/expect/src/__tests__/isError.test.ts b/packages/expect/src/__tests__/isError.test.ts index 0bdecd6e70f3..efafc5129305 100644 --- a/packages/expect/src/__tests__/isError.test.ts +++ b/packages/expect/src/__tests__/isError.test.ts @@ -20,9 +20,7 @@ describe('isError', () => { const error = createError(iframe.contentWindow); expect(isError(error)).toBe(true); } finally { - if (iframe && iframe.parentElement) { - iframe.parentElement.removeChild(iframe); - } + iframe.parentElement!.removeChild(iframe); } } From f6b431dbd7423ef9dcfb7541456d03c9b7a3913f Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Sun, 31 May 2020 21:53:24 +0200 Subject: [PATCH 3/4] Replace require with import --- packages/expect/src/__tests__/assertionCounts.test.ts | 4 ++-- packages/expect/src/__tests__/extend.test.ts | 8 ++++---- packages/expect/src/__tests__/isError.test.ts | 2 +- packages/expect/src/__tests__/spyMatchers.test.ts | 4 ++-- packages/expect/src/__tests__/stacktrace.test.ts | 2 +- packages/expect/src/__tests__/utils.test.ts | 6 +++--- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/expect/src/__tests__/assertionCounts.test.ts b/packages/expect/src/__tests__/assertionCounts.test.ts index 0a6792137176..672edbca6756 100644 --- a/packages/expect/src/__tests__/assertionCounts.test.ts +++ b/packages/expect/src/__tests__/assertionCounts.test.ts @@ -6,8 +6,8 @@ * */ -const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); -const jestExpect = require('../'); +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; +import jestExpect from '../'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); diff --git a/packages/expect/src/__tests__/extend.test.ts b/packages/expect/src/__tests__/extend.test.ts index ac0cb66b7238..fb9e30540c8d 100644 --- a/packages/expect/src/__tests__/extend.test.ts +++ b/packages/expect/src/__tests__/extend.test.ts @@ -7,10 +7,10 @@ */ import * as matcherUtils from 'jest-matcher-utils'; -const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); -const {iterableEquality, subsetEquality} = require('../utils'); -const {equals} = require('../jasmineUtils'); -const jestExpect = require('../'); +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; +import {iterableEquality, subsetEquality} from '../utils'; +import {equals} from '../jasmineUtils'; +import jestExpect from '../'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); diff --git a/packages/expect/src/__tests__/isError.test.ts b/packages/expect/src/__tests__/isError.test.ts index efafc5129305..9b21782ae866 100644 --- a/packages/expect/src/__tests__/isError.test.ts +++ b/packages/expect/src/__tests__/isError.test.ts @@ -34,7 +34,7 @@ describe('isError', () => { }); it('should detect errors from another context', () => { - testErrorFromDifferentContext((win: any) => new win.Error()); + testErrorFromDifferentContext((win: Window) => new win.Error()); }); it('should detect DOMException errors from another context', () => { diff --git a/packages/expect/src/__tests__/spyMatchers.test.ts b/packages/expect/src/__tests__/spyMatchers.test.ts index 1398bc0e5979..7bd2ac6b30b0 100644 --- a/packages/expect/src/__tests__/spyMatchers.test.ts +++ b/packages/expect/src/__tests__/spyMatchers.test.ts @@ -6,8 +6,8 @@ */ import * as Immutable from 'immutable'; -const {alignedAnsiStyleSerializer} = require('@jest/test-utils'); -const jestExpect = require('../'); +import {alignedAnsiStyleSerializer} from '@jest/test-utils'; +import jestExpect from '../'; expect.addSnapshotSerializer(alignedAnsiStyleSerializer); diff --git a/packages/expect/src/__tests__/stacktrace.test.ts b/packages/expect/src/__tests__/stacktrace.test.ts index 6c39df02088f..954e679a04ba 100644 --- a/packages/expect/src/__tests__/stacktrace.test.ts +++ b/packages/expect/src/__tests__/stacktrace.test.ts @@ -6,7 +6,7 @@ * */ -const jestExpect = require('../'); +import jestExpect from '../'; jestExpect.extend({ toCustomMatch(callback: Function, expectation: unknown) { diff --git a/packages/expect/src/__tests__/utils.test.ts b/packages/expect/src/__tests__/utils.test.ts index 1d22d48fe8df..d5b31b84a16b 100644 --- a/packages/expect/src/__tests__/utils.test.ts +++ b/packages/expect/src/__tests__/utils.test.ts @@ -7,14 +7,14 @@ */ import {stringify} from 'jest-matcher-utils'; -const { +import { emptyObject, getObjectSubset, getPath, hasOwnProperty, - subsetEquality, iterableEquality, -} = require('../utils'); + subsetEquality, +} from '../utils'; describe('getPath()', () => { test('property exists', () => { From b84ce9742f061806a596e9379ad21ffd19349197 Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Mon, 22 Jun 2020 17:42:50 +0200 Subject: [PATCH 4/4] Replace Function with stricter types --- packages/expect/src/__tests__/isError.test.ts | 4 +++- packages/expect/src/__tests__/spyMatchers.test.ts | 10 ++++++++-- packages/expect/src/__tests__/stacktrace.test.ts | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/expect/src/__tests__/isError.test.ts b/packages/expect/src/__tests__/isError.test.ts index 9b21782ae866..bb6a77c47e6d 100644 --- a/packages/expect/src/__tests__/isError.test.ts +++ b/packages/expect/src/__tests__/isError.test.ts @@ -13,7 +13,9 @@ import {isError} from '../utils'; // Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883 describe('isError', () => { - function testErrorFromDifferentContext(createError: Function) { + function testErrorFromDifferentContext( + createError: (win: Window | null) => Error, + ) { const iframe = document.createElement('iframe'); document.body.appendChild(iframe); try { diff --git a/packages/expect/src/__tests__/spyMatchers.test.ts b/packages/expect/src/__tests__/spyMatchers.test.ts index 7bd2ac6b30b0..e819b2efcfb9 100644 --- a/packages/expect/src/__tests__/spyMatchers.test.ts +++ b/packages/expect/src/__tests__/spyMatchers.test.ts @@ -177,7 +177,10 @@ const createSpy = (fn: jest.Mock) => { 'toBeCalledWith', 'toHaveBeenCalledWith', ].forEach(calledWith => { - const caller = function (callee: Function, ...args: any) { + const caller = function ( + callee: (...a: Array) => void, + ...args: any + ) { if ( calledWith === 'nthCalledWith' || calledWith === 'toHaveBeenNthCalledWith' @@ -734,7 +737,10 @@ const createSpy = (fn: jest.Mock) => { 'toReturnWith', 'toHaveReturnedWith', ].forEach(returnedWith => { - const caller = function (callee: Function, ...args: any) { + const caller = function ( + callee: (...a: Array) => void, + ...args: any + ) { if ( returnedWith === 'nthReturnedWith' || returnedWith === 'toHaveNthReturnedWith' diff --git a/packages/expect/src/__tests__/stacktrace.test.ts b/packages/expect/src/__tests__/stacktrace.test.ts index 954e679a04ba..47cb0cfff155 100644 --- a/packages/expect/src/__tests__/stacktrace.test.ts +++ b/packages/expect/src/__tests__/stacktrace.test.ts @@ -9,7 +9,7 @@ import jestExpect from '../'; jestExpect.extend({ - toCustomMatch(callback: Function, expectation: unknown) { + toCustomMatch(callback: () => unknown, expectation: unknown) { const actual = callback(); if (actual !== expectation) { @@ -21,7 +21,7 @@ jestExpect.extend({ return {pass: true}; }, - toMatchPredicate(received: unknown, argument: Function) { + toMatchPredicate(received: unknown, argument: (a: unknown) => void) { argument(received); return { message: () => '',