Skip to content
/ jest Public
forked from jestjs/jest

Commit

Permalink
refactor(jest-util): fix type errors in test files (jestjs#13464)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Oct 18, 2022
1 parent 0674c5d commit c53bdbc
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 99 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"test": "yarn lint && yarn jest",
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
"typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit",
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect,expect-utils,jest-circus,jest-cli,jest-config,jest-console,jest-snapshot,jest-worker,pretty-format}/**/__tests__",
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect,expect-utils,jest-circus,jest-cli,jest-config,jest-console,jest-snapshot,jest-util,jest-worker,pretty-format}/**/__tests__",
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
"verify-pnp": "node ./scripts/verifyPnP.mjs",
"watch": "yarn build:js && node ./scripts/watch.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ describe(convertDescriptorToString, () => {
['anonymous class expression', class {}],
])('%s', (_, input) => {
expect(() => {
// @ts-expect-error
// @ts-expect-error: Testing runtime error
return convertDescriptorToString(input);
}).toThrow(
`Invalid first argument, ${input}. It must be a named class, named function, number, or string.`,
`Invalid first argument, ${String(
input,
)}. It must be a named class, named function, number, or string.`,
);
});
});
22 changes: 12 additions & 10 deletions packages/jest-util/src/__tests__/createProcessObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import {EventEmitter} from 'events';

let createProcessObject;
let createProcessObject: typeof import('../createProcessObject').default;

function requireCreateProcessObject() {
jest.isolateModules(() => {
createProcessObject = require('../createProcessObject').default;
createProcessObject = (
require('../createProcessObject') as typeof import('../createProcessObject')
).default;
});
}

Expand All @@ -22,13 +24,13 @@ it('creates a process object that looks like the original one', () => {
// "process" inherits from EventEmitter through the prototype chain.
expect(fakeProcess instanceof EventEmitter).toBe(true);

// They look the same, but they are NOT the same (deep copied object). The
// "_events" property is checked to ensure event emitter properties are
// They look the same, but they are NOT the same (deep copied object).
// The `_events` property is checked to ensure event emitter properties are
// properly copied.
['argv', 'env', '_events'].forEach(key => {
// @ts-expect-error
(['argv', 'env', '_events'] as const).forEach(key => {
// @ts-expect-error: Testing internal `_events` property
expect(fakeProcess[key]).toEqual(process[key]);
// @ts-expect-error
// @ts-expect-error: Testing internal `_events` property
expect(fakeProcess[key]).not.toBe(process[key]);
});

Expand All @@ -47,7 +49,7 @@ it('checks that process.env works as expected on Linux platforms', () => {

// Existing properties inside process.env are copied to the fake environment.
process.env.PROP_STRING = 'foo';
// @ts-expect-error
// @ts-expect-error: Type 'number' is not assignable to type 'string'.
process.env.PROP_NUMBER = 3;
process.env.PROP_UNDEFINED = undefined;

Expand Down Expand Up @@ -102,6 +104,6 @@ it('checks that process.env works as expected in Windows platforms', () => {
// You can delete through case-insensitiveness too.
delete fake.prop_string;

expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
expect(Object.prototype.hasOwnProperty.call(fake, 'PROP_string')).toBe(false);
expect(Object.prototype.hasOwnProperty.call(fake, 'PROP_string')).toBe(false);
});
83 changes: 43 additions & 40 deletions packages/jest-util/src/__tests__/deepCyclicCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ it('returns the same value for primitive or function values', () => {
it('does not execute getters/setters, but copies them', () => {
const fn = jest.fn();
const obj = {
// @ts-expect-error
get foo() {
fn();
return;
},
};
const copy = deepCyclicCopy(obj);
Expand All @@ -49,8 +49,11 @@ it('copies arrays as array objects', () => {
});

it('handles cyclic dependencies', () => {
const cyclic: any = {a: 42, subcycle: {}};
type Cyclic = {[key: string]: unknown | Cyclic} & {subcycle?: Cyclic};

const cyclic: Cyclic = {a: 42};

cyclic.subcycle = {};
cyclic.subcycle.baz = cyclic;
cyclic.bar = cyclic;

Expand All @@ -60,7 +63,7 @@ it('handles cyclic dependencies', () => {

expect(copy.a).toBe(42);
expect(copy.bar).toEqual(copy);
expect(copy.subcycle.baz).toEqual(copy);
expect(copy.subcycle?.baz).toEqual(copy);
});

it('uses the blacklist to avoid copying properties on the first level', () => {
Expand All @@ -84,17 +87,17 @@ it('uses the blacklist to avoid copying properties on the first level', () => {
});

it('does not keep the prototype by default when top level is object', () => {
// @ts-expect-error
// @ts-expect-error: Testing purpose
const sourceObject = new (function () {})();
// @ts-expect-error
// @ts-expect-error: Testing purpose
sourceObject.nestedObject = new (function () {})();
// @ts-expect-error
// @ts-expect-error: Testing purpose
sourceObject.nestedArray = new (function () {
// @ts-expect-error
// @ts-expect-error: Testing purpose
this.length = 0;
})();

const spy = jest
const spyArray = jest
.spyOn(Array, 'isArray')
.mockImplementation(object => object === sourceObject.nestedArray);

Expand All @@ -118,15 +121,15 @@ it('does not keep the prototype by default when top level is object', () => {
Object.getPrototypeOf([]),
);

spy.mockRestore();
spyArray.mockRestore();
});

it('does not keep the prototype by default when top level is array', () => {
const spy = jest.spyOn(Array, 'isArray').mockImplementation(() => true);
const spyArray = jest.spyOn(Array, 'isArray').mockImplementation(() => true);

// @ts-expect-error
// @ts-expect-error: Testing purpose
const sourceArray = new (function () {
// @ts-expect-error
// @ts-expect-error: Testing purpose
this.length = 0;
})();

Expand All @@ -136,15 +139,15 @@ it('does not keep the prototype by default when top level is array', () => {
);

expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf([]));
spy.mockRestore();
spyArray.mockRestore();
});

it('does not keep the prototype of arrays when keepPrototype = false', () => {
const spy = jest.spyOn(Array, 'isArray').mockImplementation(() => true);
const spyArray = jest.spyOn(Array, 'isArray').mockImplementation(() => true);

// @ts-expect-error
// @ts-expect-error: Testing purpose
const sourceArray = new (function () {
// @ts-expect-error
// @ts-expect-error: Testing purpose
this.length = 0;
})();

Expand All @@ -154,49 +157,49 @@ it('does not keep the prototype of arrays when keepPrototype = false', () => {
);

expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf([]));
spy.mockRestore();
spyArray.mockRestore();
});

it('keeps the prototype of arrays when keepPrototype = true', () => {
const spy = jest.spyOn(Array, 'isArray').mockImplementation(() => true);
const spyArray = jest.spyOn(Array, 'isArray').mockImplementation(() => true);

// @ts-expect-error
// @ts-expect-error: Testing purpose
const sourceArray = new (function () {
// @ts-expect-error
// @ts-expect-error: Testing purpose
this.length = 0;
})();

const copy = deepCyclicCopy(sourceArray, {keepPrototype: true});
expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf(sourceArray));

spy.mockRestore();
spyArray.mockRestore();
});

it('does not keep the prototype for objects when keepPrototype = false', () => {
// @ts-expect-error: Testing purpose
const sourceObject = new (function () {})();
// @ts-expect-error
const sourceobject = new (function () {})();
// @ts-expect-error
sourceobject.nestedObject = new (function () {})();
// @ts-expect-error
sourceobject.nestedArray = new (function () {
// @ts-expect-error
sourceObject.nestedObject = new (function () {})();
// @ts-expect-error: Testing purpose
sourceObject.nestedArray = new (function () {
// @ts-expect-error: Testing purpose
this.length = 0;
})();

const spy = jest
const spyArray = jest
.spyOn(Array, 'isArray')
.mockImplementation(object => object === sourceobject.nestedArray);
.mockImplementation(object => object === sourceObject.nestedArray);

const copy = deepCyclicCopy(sourceobject, {keepPrototype: false});
const copy = deepCyclicCopy(sourceObject, {keepPrototype: false});

expect(Object.getPrototypeOf(copy)).not.toBe(
Object.getPrototypeOf(sourceobject),
Object.getPrototypeOf(sourceObject),
);
expect(Object.getPrototypeOf(copy.nestedObject)).not.toBe(
Object.getPrototypeOf(sourceobject.nestedObject),
Object.getPrototypeOf(sourceObject.nestedObject),
);
expect(Object.getPrototypeOf(copy.nestedArray)).not.toBe(
Object.getPrototypeOf(sourceobject.nestedArray),
Object.getPrototypeOf(sourceObject.nestedArray),
);
expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf({}));
expect(Object.getPrototypeOf(copy.nestedObject)).toBe(
Expand All @@ -206,21 +209,21 @@ it('does not keep the prototype for objects when keepPrototype = false', () => {
Object.getPrototypeOf([]),
);

spy.mockRestore();
spyArray.mockRestore();
});

it('keeps the prototype for objects when keepPrototype = true', () => {
// @ts-expect-error
// @ts-expect-error: Testing purpose
const sourceObject = new (function () {})();
// @ts-expect-error
// @ts-expect-error: Testing purpose
sourceObject.nestedObject = new (function () {})();
// @ts-expect-error
// @ts-expect-error: Testing purpose
sourceObject.nestedArray = new (function () {
// @ts-expect-error
// @ts-expect-error: Testing purpose
this.length = 0;
})();

const spy = jest
const spyArray = jest
.spyOn(Array, 'isArray')
.mockImplementation(object => object === sourceObject.nestedArray);

Expand All @@ -233,5 +236,5 @@ it('keeps the prototype for objects when keepPrototype = true', () => {
expect(Object.getPrototypeOf(copy.nestedArray)).toBe(
Object.getPrototypeOf(sourceObject.nestedArray),
);
spy.mockRestore();
spyArray.mockRestore();
});
2 changes: 1 addition & 1 deletion packages/jest-util/src/__tests__/globsToMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ it('works like micromatch with only negative globs', () => {
});

it('works like micromatch with empty globs', () => {
const globs = [];
const globs: Array<string> = [];
const matcher = globsToMatcher(globs);

expect(matcher('some-module.js')).toBe(
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-util/src/__tests__/installCommonGlobals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ globalThis.DTRACE_NET_SERVER_CONNECTION = fake;
let installCommonGlobals: typeof import('../installCommonGlobals').default;

function getGlobal(): typeof globalThis {
return runInContext('this', createContext());
return runInContext('this', createContext()) as typeof globalThis;
}

beforeEach(() => {
installCommonGlobals = require('../installCommonGlobals').default;
installCommonGlobals = (
require('../installCommonGlobals') as typeof import('../installCommonGlobals')
).default;
});

afterEach(() => {
Expand Down
70 changes: 27 additions & 43 deletions packages/jest-util/src/__tests__/isInteractive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,45 @@
* LICENSE file in the root directory of this source tree.
*/

let oldIsTTY: typeof process.stdout.isTTY;
let oldTERM: string | undefined;
import * as process from 'process';

beforeEach(() => {
oldIsTTY = process.stdout.isTTY;
oldTERM = process.env.TERM;
});
const oldIsTTY = process.stdout.isTTY;
const oldTERM = process.env.TERM;

afterEach(() => {
process.stdout.isTTY = oldIsTTY;
process.env.TERM = oldTERM;
jest.resetModules();
});

it('Returns true when running on interactive environment', () => {
it('Returns true when running in an interactive environment', () => {
jest.doMock('ci-info', () => ({isCI: false}));
process.stdout.isTTY = true;
process.env.TERM = 'xterm-256color';

const isInteractive = require('../isInteractive').default;
const isInteractive = (
require('../isInteractive') as typeof import('../isInteractive')
).default;

expect(isInteractive).toBe(true);
});

it('Returns false when running on a non-interactive environment', () => {
let isInteractive;
const expectedResult = false;

// Test with isCI being true and isTTY false
jest.doMock('ci-info', () => ({isCI: true}));
process.stdout.isTTY = undefined;
process.env.TERM = 'xterm-256color';
isInteractive = require('../isInteractive').default;
expect(isInteractive).toBe(expectedResult);

// Test with isCI being false and isTTY false
jest.resetModules();
jest.doMock('ci-info', () => ({isCI: false}));
process.stdout.isTTY = undefined;
process.env.TERM = 'xterm-256color';
isInteractive = require('../isInteractive').default;
expect(isInteractive).toBe(expectedResult);

// Test with isCI being true and isTTY true
jest.resetModules();
jest.doMock('ci-info', () => ({isCI: true}));
process.stdout.isTTY = true;
process.env.TERM = 'xterm-256color';
isInteractive = require('../isInteractive').default;
expect(isInteractive).toBe(expectedResult);

// Test with dumb terminal
jest.resetModules();
jest.doMock('ci-info', () => ({isCI: false}));
process.stdout.isTTY = undefined;
process.env.TERM = 'dumb';
isInteractive = require('../isInteractive').default;
expect(isInteractive).toBe(expectedResult);
});
it.each([
{isCI: false, isTTY: false, term: 'xterm-256color'},
{isCI: false, isTTY: false, term: 'xterm-256color'},
{isCI: true, isTTY: true, term: 'xterm-256color'},
{isCI: false, isTTY: false, term: 'dumb'},
])(
'Returns false when running in a non-interactive environment',
({isCI, isTTY, term}) => {
jest.doMock('ci-info', () => ({isCI}));
process.stdout.isTTY = isTTY;
process.env.TERM = term;

const isInteractive = (
require('../isInteractive') as typeof import('../isInteractive')
).default;

expect(isInteractive).toBe(false);
},
);

0 comments on commit c53bdbc

Please sign in to comment.