Skip to content

Commit

Permalink
chore: migrate some tests to TS (#10073)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostd committed Jul 30, 2020
1 parent c7b1834 commit 01b93d4
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
*
*/

'use strict';

const {alignedAnsiStyleSerializer} = require('@jest/test-utils');
const jestExpect = require('../');
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import jestExpect from '../';

expect.addSnapshotSerializer(alignedAnsiStyleSerializer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@
*
*/

const matcherUtils = require('jest-matcher-utils');
const {alignedAnsiStyleSerializer} = require('@jest/test-utils');
const {iterableEquality, subsetEquality} = require('../utils');
const {equals} = require('../jasmineUtils');
const jestExpect = require('../');
import * as matcherUtils from 'jest-matcher-utils';
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import {iterableEquality, subsetEquality} from '../utils';
import {equals} from '../jasmineUtils';
import jestExpect from '../';

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}`
: () => `expected ${actual} to be divisible by ${expected}`;

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}`
Expand Down Expand Up @@ -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, {
Expand All @@ -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};
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: (win: Window | null) => Error,
) {
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);
iframe.parentElement!.removeChild(iframe);
}
}

Expand All @@ -34,11 +36,11 @@ describe('isError', () => {
});

it('should detect errors from another context', () => {
testErrorFromDifferentContext(win => new win.Error());
testErrorFromDifferentContext((win: Window) => new win.Error());
});

it('should detect DOMException errors from another context', () => {
testErrorFromDifferentContext(win => {
testErrorFromDifferentContext((win: Window) => {
try {
win.document.querySelectorAll('');
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

const Immutable = require('immutable');
const {alignedAnsiStyleSerializer} = require('@jest/test-utils');
const jestExpect = require('../');
import * as Immutable from 'immutable';
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import jestExpect from '../';

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 = {
Expand Down Expand Up @@ -177,7 +177,10 @@ const createSpy = fn => {
'toBeCalledWith',
'toHaveBeenCalledWith',
].forEach(calledWith => {
const caller = function (callee, ...args) {
const caller = function (
callee: (...a: Array<unknown>) => void,
...args: any
) {
if (
calledWith === 'nthCalledWith' ||
calledWith === 'toHaveBeenNthCalledWith'
Expand Down Expand Up @@ -295,8 +298,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);
Expand Down Expand Up @@ -534,7 +537,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.
Expand Down Expand Up @@ -703,7 +706,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 {
Expand Down Expand Up @@ -734,7 +737,10 @@ const createSpy = fn => {
'toReturnWith',
'toHaveReturnedWith',
].forEach(returnedWith => {
const caller = function (callee, ...args) {
const caller = function (
callee: (...a: Array<unknown>) => void,
...args: any
) {
if (
returnedWith === 'nthReturnedWith' ||
returnedWith === 'toHaveNthReturnedWith'
Expand Down Expand Up @@ -850,7 +856,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();

Expand All @@ -862,7 +868,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();

Expand Down Expand Up @@ -944,7 +950,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.
Expand Down Expand Up @@ -1032,7 +1038,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(() => {
Expand All @@ -1041,7 +1047,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(() => {
Expand All @@ -1051,7 +1057,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 {
Expand Down Expand Up @@ -1108,7 +1114,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*
*/

const jestExpect = require('../');
import jestExpect from '../';

jestExpect.extend({
toCustomMatch(callback, expectation) {
toCustomMatch(callback: () => unknown, expectation: unknown) {
const actual = callback();

if (actual !== expectation) {
Expand All @@ -21,7 +21,7 @@ jestExpect.extend({

return {pass: true};
},
toMatchPredicate(received, argument) {
toMatchPredicate(received: unknown, argument: (a: unknown) => void) {
argument(received);
return {
message: () => '',
Expand All @@ -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');
}
});

Expand All @@ -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');
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
*/

'use strict';

describe('Symbol in objects', () => {
test('should compare objects with Symbol keys', () => {
const sym = Symbol('foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>) => ({
childNodes,
nodeType: 1,
tagName: name.toUpperCase(),
});

const createTextNode = data => ({
const createTextNode = (data: unknown) => ({
data,
nodeType: 3,
});

const createDocumentFragment = (...children) => ({
const createDocumentFragment = (...children: Array<unknown>) => ({
children,
nodeType: 11,
});
Expand Down
Loading

0 comments on commit 01b93d4

Please sign in to comment.