Skip to content

Commit c53bdbc

Browse files
authored
refactor(jest-util): fix type errors in test files (#13464)
1 parent 0674c5d commit c53bdbc

7 files changed

+92
-99
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"test": "yarn lint && yarn jest",
114114
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
115115
"typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit",
116-
"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__",
116+
"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__",
117117
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
118118
"verify-pnp": "node ./scripts/verifyPnP.mjs",
119119
"watch": "yarn build:js && node ./scripts/watch.mjs",

packages/jest-util/src/__tests__/convertDescriptorToString.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ describe(convertDescriptorToString, () => {
2727
['anonymous class expression', class {}],
2828
])('%s', (_, input) => {
2929
expect(() => {
30-
// @ts-expect-error
30+
// @ts-expect-error: Testing runtime error
3131
return convertDescriptorToString(input);
3232
}).toThrow(
33-
`Invalid first argument, ${input}. It must be a named class, named function, number, or string.`,
33+
`Invalid first argument, ${String(
34+
input,
35+
)}. It must be a named class, named function, number, or string.`,
3436
);
3537
});
3638
});

packages/jest-util/src/__tests__/createProcessObject.test.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
import {EventEmitter} from 'events';
99

10-
let createProcessObject;
10+
let createProcessObject: typeof import('../createProcessObject').default;
1111

1212
function requireCreateProcessObject() {
1313
jest.isolateModules(() => {
14-
createProcessObject = require('../createProcessObject').default;
14+
createProcessObject = (
15+
require('../createProcessObject') as typeof import('../createProcessObject')
16+
).default;
1517
});
1618
}
1719

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

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

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

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

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

105-
expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
106-
expect(Object.prototype.hasOwnProperty.call('PROP_string')).toBe(false);
107+
expect(Object.prototype.hasOwnProperty.call(fake, 'PROP_string')).toBe(false);
108+
expect(Object.prototype.hasOwnProperty.call(fake, 'PROP_string')).toBe(false);
107109
});

packages/jest-util/src/__tests__/deepCyclicCopy.test.ts

+43-40
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ it('returns the same value for primitive or function values', () => {
2323
it('does not execute getters/setters, but copies them', () => {
2424
const fn = jest.fn();
2525
const obj = {
26-
// @ts-expect-error
2726
get foo() {
2827
fn();
28+
return;
2929
},
3030
};
3131
const copy = deepCyclicCopy(obj);
@@ -49,8 +49,11 @@ it('copies arrays as array objects', () => {
4949
});
5050

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

54+
const cyclic: Cyclic = {a: 42};
55+
56+
cyclic.subcycle = {};
5457
cyclic.subcycle.baz = cyclic;
5558
cyclic.bar = cyclic;
5659

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

6164
expect(copy.a).toBe(42);
6265
expect(copy.bar).toEqual(copy);
63-
expect(copy.subcycle.baz).toEqual(copy);
66+
expect(copy.subcycle?.baz).toEqual(copy);
6467
});
6568

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

8689
it('does not keep the prototype by default when top level is object', () => {
87-
// @ts-expect-error
90+
// @ts-expect-error: Testing purpose
8891
const sourceObject = new (function () {})();
89-
// @ts-expect-error
92+
// @ts-expect-error: Testing purpose
9093
sourceObject.nestedObject = new (function () {})();
91-
// @ts-expect-error
94+
// @ts-expect-error: Testing purpose
9295
sourceObject.nestedArray = new (function () {
93-
// @ts-expect-error
96+
// @ts-expect-error: Testing purpose
9497
this.length = 0;
9598
})();
9699

97-
const spy = jest
100+
const spyArray = jest
98101
.spyOn(Array, 'isArray')
99102
.mockImplementation(object => object === sourceObject.nestedArray);
100103

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

121-
spy.mockRestore();
124+
spyArray.mockRestore();
122125
});
123126

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

127-
// @ts-expect-error
130+
// @ts-expect-error: Testing purpose
128131
const sourceArray = new (function () {
129-
// @ts-expect-error
132+
// @ts-expect-error: Testing purpose
130133
this.length = 0;
131134
})();
132135

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

138141
expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf([]));
139-
spy.mockRestore();
142+
spyArray.mockRestore();
140143
});
141144

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

145-
// @ts-expect-error
148+
// @ts-expect-error: Testing purpose
146149
const sourceArray = new (function () {
147-
// @ts-expect-error
150+
// @ts-expect-error: Testing purpose
148151
this.length = 0;
149152
})();
150153

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

156159
expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf([]));
157-
spy.mockRestore();
160+
spyArray.mockRestore();
158161
});
159162

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

163-
// @ts-expect-error
166+
// @ts-expect-error: Testing purpose
164167
const sourceArray = new (function () {
165-
// @ts-expect-error
168+
// @ts-expect-error: Testing purpose
166169
this.length = 0;
167170
})();
168171

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

172-
spy.mockRestore();
175+
spyArray.mockRestore();
173176
});
174177

175178
it('does not keep the prototype for objects when keepPrototype = false', () => {
179+
// @ts-expect-error: Testing purpose
180+
const sourceObject = new (function () {})();
176181
// @ts-expect-error
177-
const sourceobject = new (function () {})();
178-
// @ts-expect-error
179-
sourceobject.nestedObject = new (function () {})();
180-
// @ts-expect-error
181-
sourceobject.nestedArray = new (function () {
182-
// @ts-expect-error
182+
sourceObject.nestedObject = new (function () {})();
183+
// @ts-expect-error: Testing purpose
184+
sourceObject.nestedArray = new (function () {
185+
// @ts-expect-error: Testing purpose
183186
this.length = 0;
184187
})();
185188

186-
const spy = jest
189+
const spyArray = jest
187190
.spyOn(Array, 'isArray')
188-
.mockImplementation(object => object === sourceobject.nestedArray);
191+
.mockImplementation(object => object === sourceObject.nestedArray);
189192

190-
const copy = deepCyclicCopy(sourceobject, {keepPrototype: false});
193+
const copy = deepCyclicCopy(sourceObject, {keepPrototype: false});
191194

192195
expect(Object.getPrototypeOf(copy)).not.toBe(
193-
Object.getPrototypeOf(sourceobject),
196+
Object.getPrototypeOf(sourceObject),
194197
);
195198
expect(Object.getPrototypeOf(copy.nestedObject)).not.toBe(
196-
Object.getPrototypeOf(sourceobject.nestedObject),
199+
Object.getPrototypeOf(sourceObject.nestedObject),
197200
);
198201
expect(Object.getPrototypeOf(copy.nestedArray)).not.toBe(
199-
Object.getPrototypeOf(sourceobject.nestedArray),
202+
Object.getPrototypeOf(sourceObject.nestedArray),
200203
);
201204
expect(Object.getPrototypeOf(copy)).toBe(Object.getPrototypeOf({}));
202205
expect(Object.getPrototypeOf(copy.nestedObject)).toBe(
@@ -206,21 +209,21 @@ it('does not keep the prototype for objects when keepPrototype = false', () => {
206209
Object.getPrototypeOf([]),
207210
);
208211

209-
spy.mockRestore();
212+
spyArray.mockRestore();
210213
});
211214

212215
it('keeps the prototype for objects when keepPrototype = true', () => {
213-
// @ts-expect-error
216+
// @ts-expect-error: Testing purpose
214217
const sourceObject = new (function () {})();
215-
// @ts-expect-error
218+
// @ts-expect-error: Testing purpose
216219
sourceObject.nestedObject = new (function () {})();
217-
// @ts-expect-error
220+
// @ts-expect-error: Testing purpose
218221
sourceObject.nestedArray = new (function () {
219-
// @ts-expect-error
222+
// @ts-expect-error: Testing purpose
220223
this.length = 0;
221224
})();
222225

223-
const spy = jest
226+
const spyArray = jest
224227
.spyOn(Array, 'isArray')
225228
.mockImplementation(object => object === sourceObject.nestedArray);
226229

@@ -233,5 +236,5 @@ it('keeps the prototype for objects when keepPrototype = true', () => {
233236
expect(Object.getPrototypeOf(copy.nestedArray)).toBe(
234237
Object.getPrototypeOf(sourceObject.nestedArray),
235238
);
236-
spy.mockRestore();
239+
spyArray.mockRestore();
237240
});

packages/jest-util/src/__tests__/globsToMatcher.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ it('works like micromatch with only negative globs', () => {
5959
});
6060

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

6565
expect(matcher('some-module.js')).toBe(

packages/jest-util/src/__tests__/installCommonGlobals.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ globalThis.DTRACE_NET_SERVER_CONNECTION = fake;
1717
let installCommonGlobals: typeof import('../installCommonGlobals').default;
1818

1919
function getGlobal(): typeof globalThis {
20-
return runInContext('this', createContext());
20+
return runInContext('this', createContext()) as typeof globalThis;
2121
}
2222

2323
beforeEach(() => {
24-
installCommonGlobals = require('../installCommonGlobals').default;
24+
installCommonGlobals = (
25+
require('../installCommonGlobals') as typeof import('../installCommonGlobals')
26+
).default;
2527
});
2628

2729
afterEach(() => {

packages/jest-util/src/__tests__/isInteractive.test.ts

+27-43
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,45 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
let oldIsTTY: typeof process.stdout.isTTY;
9-
let oldTERM: string | undefined;
8+
import * as process from 'process';
109

11-
beforeEach(() => {
12-
oldIsTTY = process.stdout.isTTY;
13-
oldTERM = process.env.TERM;
14-
});
10+
const oldIsTTY = process.stdout.isTTY;
11+
const oldTERM = process.env.TERM;
1512

1613
afterEach(() => {
1714
process.stdout.isTTY = oldIsTTY;
1815
process.env.TERM = oldTERM;
1916
jest.resetModules();
2017
});
2118

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

27-
const isInteractive = require('../isInteractive').default;
24+
const isInteractive = (
25+
require('../isInteractive') as typeof import('../isInteractive')
26+
).default;
27+
2828
expect(isInteractive).toBe(true);
2929
});
3030

31-
it('Returns false when running on a non-interactive environment', () => {
32-
let isInteractive;
33-
const expectedResult = false;
34-
35-
// Test with isCI being true and isTTY false
36-
jest.doMock('ci-info', () => ({isCI: true}));
37-
process.stdout.isTTY = undefined;
38-
process.env.TERM = 'xterm-256color';
39-
isInteractive = require('../isInteractive').default;
40-
expect(isInteractive).toBe(expectedResult);
41-
42-
// Test with isCI being false and isTTY false
43-
jest.resetModules();
44-
jest.doMock('ci-info', () => ({isCI: false}));
45-
process.stdout.isTTY = undefined;
46-
process.env.TERM = 'xterm-256color';
47-
isInteractive = require('../isInteractive').default;
48-
expect(isInteractive).toBe(expectedResult);
49-
50-
// Test with isCI being true and isTTY true
51-
jest.resetModules();
52-
jest.doMock('ci-info', () => ({isCI: true}));
53-
process.stdout.isTTY = true;
54-
process.env.TERM = 'xterm-256color';
55-
isInteractive = require('../isInteractive').default;
56-
expect(isInteractive).toBe(expectedResult);
57-
58-
// Test with dumb terminal
59-
jest.resetModules();
60-
jest.doMock('ci-info', () => ({isCI: false}));
61-
process.stdout.isTTY = undefined;
62-
process.env.TERM = 'dumb';
63-
isInteractive = require('../isInteractive').default;
64-
expect(isInteractive).toBe(expectedResult);
65-
});
31+
it.each([
32+
{isCI: false, isTTY: false, term: 'xterm-256color'},
33+
{isCI: false, isTTY: false, term: 'xterm-256color'},
34+
{isCI: true, isTTY: true, term: 'xterm-256color'},
35+
{isCI: false, isTTY: false, term: 'dumb'},
36+
])(
37+
'Returns false when running in a non-interactive environment',
38+
({isCI, isTTY, term}) => {
39+
jest.doMock('ci-info', () => ({isCI}));
40+
process.stdout.isTTY = isTTY;
41+
process.env.TERM = term;
42+
43+
const isInteractive = (
44+
require('../isInteractive') as typeof import('../isInteractive')
45+
).default;
46+
47+
expect(isInteractive).toBe(false);
48+
},
49+
);

0 commit comments

Comments
 (0)