Skip to content

Commit e865fbd

Browse files
authored
chore: run ESLint with types om more packages (#13372)
1 parent fba6161 commit e865fbd

File tree

10 files changed

+99
-82
lines changed

10 files changed

+99
-82
lines changed

packages/jest-docblock/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function parseWithComments(docblock: string): {
3939
comments: string;
4040
pragmas: Pragmas;
4141
} {
42-
const line = detectNewline(docblock) || EOL;
42+
const line = detectNewline(docblock) ?? EOL;
4343

4444
docblock = docblock
4545
.replace(commentStartRe, '')
@@ -54,7 +54,7 @@ export function parseWithComments(docblock: string): {
5454
}
5555
docblock = docblock.replace(ltrimNewlineRe, '').trimRight();
5656

57-
const result = Object.create(null);
57+
const result = Object.create(null) as Pragmas;
5858
const comments = docblock
5959
.replace(propertyRe, '')
6060
.replace(ltrimNewlineRe, '')
@@ -83,7 +83,7 @@ export function print({
8383
comments?: string;
8484
pragmas?: Pragmas;
8585
}): string {
86-
const line = detectNewline(comments) || EOL;
86+
const line = detectNewline(comments) ?? EOL;
8787
const head = '/**';
8888
const start = ' *';
8989
const tail = ' */';

packages/jest-environment-jsdom/src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type Win = Window &
2626
};
2727
};
2828

29+
function isString(value: unknown): value is string {
30+
return typeof value === 'string';
31+
}
32+
2933
export default class JSDOMEnvironment implements JestEnvironment<number> {
3034
dom: JSDOM | null;
3135
fakeTimers: LegacyFakeTimers<number> | null;
@@ -65,33 +69,33 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
6569
const global = (this.global = this.dom.window.document
6670
.defaultView as unknown as Win);
6771

68-
if (!global) {
72+
if (global == null) {
6973
throw new Error('JSDOM did not return a Window object');
7074
}
7175

72-
// for "universal" code (code should use `globalThis`)
73-
global.global = global as any;
76+
// @ts-expect-error - for "universal" code (code should use `globalThis`)
77+
global.global = global;
7478

7579
// Node's error-message stack size is limited at 10, but it's pretty useful
7680
// to see more than that when a test fails.
7781
this.global.Error.stackTraceLimit = 100;
78-
installCommonGlobals(global as any, projectConfig.globals);
82+
installCommonGlobals(global, projectConfig.globals);
7983

8084
// TODO: remove this ASAP, but it currently causes tests to run really slow
8185
global.Buffer = Buffer;
8286

8387
// Report uncaught errors.
8488
this.errorEventListener = event => {
85-
if (userErrorListenerCount === 0 && event.error) {
89+
if (userErrorListenerCount === 0 && event.error != null) {
8690
process.emit('uncaughtException', event.error);
8791
}
8892
};
8993
global.addEventListener('error', this.errorEventListener);
9094

9195
// However, don't report them as uncaught if the user listens to 'error' event.
9296
// In that case, we assume the might have custom error handling logic.
93-
const originalAddListener = global.addEventListener;
94-
const originalRemoveListener = global.removeEventListener;
97+
const originalAddListener = global.addEventListener.bind(global);
98+
const originalRemoveListener = global.removeEventListener.bind(global);
9599
let userErrorListenerCount = 0;
96100
global.addEventListener = function (
97101
...args: Parameters<typeof originalAddListener>
@@ -114,7 +118,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
114118
const {customExportConditions} = projectConfig.testEnvironmentOptions;
115119
if (
116120
Array.isArray(customExportConditions) &&
117-
customExportConditions.every(item => typeof item === 'string')
121+
customExportConditions.every(isString)
118122
) {
119123
this.customExportConditions = customExportConditions;
120124
} else {
@@ -124,7 +128,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
124128
}
125129
}
126130

127-
this.moduleMocker = new ModuleMocker(global as any);
131+
this.moduleMocker = new ModuleMocker(global);
128132

129133
this.fakeTimers = new LegacyFakeTimers({
130134
config: projectConfig,
@@ -152,7 +156,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
152156
if (this.fakeTimersModern) {
153157
this.fakeTimersModern.dispose();
154158
}
155-
if (this.global) {
159+
if (this.global != null) {
156160
if (this.errorEventListener) {
157161
this.global.removeEventListener('error', this.errorEventListener);
158162
}

packages/jest-environment-node/src/__tests__/node_environment.test.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,60 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import type {EnvironmentContext} from '@jest/environment';
89
import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
910
import NodeEnvironment from '../';
1011

12+
const context: EnvironmentContext = {
13+
console,
14+
docblockPragmas: {},
15+
testPath: __filename,
16+
};
17+
1118
describe('NodeEnvironment', () => {
1219
it('uses a copy of the process object', () => {
1320
const testEnvConfig = {
1421
globalConfig: makeGlobalConfig(),
1522
projectConfig: makeProjectConfig(),
1623
};
17-
const env1 = new NodeEnvironment(testEnvConfig);
18-
const env2 = new NodeEnvironment(testEnvConfig);
24+
const env1 = new NodeEnvironment(testEnvConfig, context);
25+
const env2 = new NodeEnvironment(testEnvConfig, context);
1926

2027
expect(env1.global.process).not.toBe(env2.global.process);
2128
});
2229

2330
it('exposes process.on', () => {
24-
const env1 = new NodeEnvironment({
25-
globalConfig: makeGlobalConfig(),
26-
projectConfig: makeProjectConfig(),
27-
});
31+
const env1 = new NodeEnvironment(
32+
{
33+
globalConfig: makeGlobalConfig(),
34+
projectConfig: makeProjectConfig(),
35+
},
36+
context,
37+
);
2838

2939
expect(env1.global.process.on).not.toBeNull();
3040
});
3141

3242
it('exposes global.global', () => {
33-
const env1 = new NodeEnvironment({
34-
globalConfig: makeGlobalConfig(),
35-
projectConfig: makeProjectConfig(),
36-
});
43+
const env1 = new NodeEnvironment(
44+
{
45+
globalConfig: makeGlobalConfig(),
46+
projectConfig: makeProjectConfig(),
47+
},
48+
context,
49+
);
3750

3851
expect(env1.global.global).toBe(env1.global);
3952
});
4053

4154
it('should configure setTimeout/setInterval to use the node api', () => {
42-
const env1 = new NodeEnvironment({
43-
globalConfig: makeGlobalConfig(),
44-
projectConfig: makeProjectConfig(),
45-
});
55+
const env1 = new NodeEnvironment(
56+
{
57+
globalConfig: makeGlobalConfig(),
58+
projectConfig: makeProjectConfig(),
59+
},
60+
context,
61+
);
4662

4763
env1.fakeTimers!.useFakeTimers();
4864

@@ -57,10 +73,13 @@ describe('NodeEnvironment', () => {
5773
});
5874

5975
it('has modern fake timers implementation', () => {
60-
const env = new NodeEnvironment({
61-
globalConfig: makeGlobalConfig(),
62-
projectConfig: makeProjectConfig(),
63-
});
76+
const env = new NodeEnvironment(
77+
{
78+
globalConfig: makeGlobalConfig(),
79+
projectConfig: makeProjectConfig(),
80+
},
81+
context,
82+
);
6483

6584
expect(env.fakeTimersModern).toBeDefined();
6685
});

packages/jest-environment-node/src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ const nodeGlobals = new Map(
5353
}),
5454
);
5555

56+
function isString(value: unknown): value is string {
57+
return typeof value === 'string';
58+
}
59+
5660
export default class NodeEnvironment implements JestEnvironment<Timer> {
5761
context: Context | null;
5862
fakeTimers: LegacyFakeTimers<Timer> | null;
@@ -65,10 +69,11 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
6569
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
6670
const {projectConfig} = config;
6771
this.context = createContext();
68-
const global = (this.global = runInContext(
72+
const global = runInContext(
6973
'this',
7074
Object.assign(this.context, projectConfig.testEnvironmentOptions),
71-
));
75+
) as Global.Global;
76+
this.global = global;
7277

7378
const contextGlobals = new Set(Object.getOwnPropertyNames(global));
7479
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) {
@@ -78,7 +83,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
7883
enumerable: descriptor.enumerable,
7984
get() {
8085
// @ts-expect-error: no index signature
81-
const val = globalThis[nodeGlobalsKey];
86+
const val = globalThis[nodeGlobalsKey] as unknown;
8287

8388
// override lazy getter
8489
Object.defineProperty(global, nodeGlobalsKey, {
@@ -89,7 +94,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
8994
});
9095
return val;
9196
},
92-
set(val) {
97+
set(val: unknown) {
9398
// override lazy getter
9499
Object.defineProperty(global, nodeGlobalsKey, {
95100
configurable: descriptor.configurable,
@@ -102,6 +107,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
102107
}
103108
}
104109

110+
// @ts-expect-error - Buffer and gc is "missing"
105111
global.global = global;
106112
global.Buffer = Buffer;
107113
global.ArrayBuffer = ArrayBuffer;
@@ -120,7 +126,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
120126
const {customExportConditions} = projectConfig.testEnvironmentOptions;
121127
if (
122128
Array.isArray(customExportConditions) &&
123-
customExportConditions.every(item => typeof item === 'string')
129+
customExportConditions.every(isString)
124130
) {
125131
this.customExportConditions = customExportConditions;
126132
} else {
@@ -142,8 +148,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
142148
},
143149
});
144150

145-
const timerRefToId = (timer: Timer): number | undefined =>
146-
(timer && timer.id) || undefined;
151+
const timerRefToId = (timer: Timer): number | undefined => timer?.id;
147152

148153
this.fakeTimers = new LegacyFakeTimers({
149154
config: projectConfig,

packages/jest-globals/src/__tests__/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
*/
77

88
test('throw when directly imported', () => {
9-
expect(() => require('../')).toThrow(
9+
expect(() => {
10+
require('../');
11+
}).toThrow(
1012
'Do not import `@jest/globals` outside of the Jest test environment',
1113
);
1214
});

packages/jest-resolve-dependencies/src/__tests__/dependency_resolver.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {DependencyResolver} from '../index';
1616
const maxWorkers = 1;
1717
let dependencyResolver: DependencyResolver;
1818
let runtimeContextResolver: Resolver;
19-
let Runtime: typeof import('jest-runtime').default;
2019
let config: Config.ProjectConfig;
2120
const cases: Record<string, (path: string) => boolean> = {
2221
fancyCondition: path => path.length > 10,
@@ -26,7 +25,8 @@ const filter = (path: string) =>
2625
Object.keys(cases).every(key => cases[key](path));
2726

2827
beforeEach(async () => {
29-
Runtime = require('jest-runtime').default;
28+
const Runtime = (require('jest-runtime') as typeof import('jest-runtime'))
29+
.default;
3030
config = makeProjectConfig({
3131
cacheDirectory: path.resolve(tmpdir(), 'jest-resolve-dependencies-test'),
3232
moduleDirectories: ['node_modules'],

packages/jest-resolve-dependencies/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class DependencyResolver {
6161
}
6262
}
6363

64-
if (!resolvedDependency) {
64+
if (resolvedDependency == null) {
6565
return acc;
6666
}
6767

@@ -78,7 +78,7 @@ export class DependencyResolver {
7878
// leave resolvedMockDependency as undefined if nothing can be found
7979
}
8080

81-
if (resolvedMockDependency) {
81+
if (resolvedMockDependency != null) {
8282
const dependencyMockDir = path.resolve(
8383
path.dirname(resolvedDependency),
8484
'__mocks__',

packages/jest-test-result/src/formatTestResults.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
import type {
99
AggregatedResult,
10-
AssertionResult,
1110
CodeCoverageFormatter,
1211
CodeCoverageReporter,
13-
FormattedAssertionResult,
1412
FormattedTestResult,
1513
FormattedTestResults,
1614
TestResult,
@@ -21,55 +19,35 @@ const formatTestResult = (
2119
codeCoverageFormatter?: CodeCoverageFormatter,
2220
reporter?: CodeCoverageReporter,
2321
): FormattedTestResult => {
24-
const assertionResults = testResult.testResults.map(formatTestAssertion);
2522
if (testResult.testExecError) {
2623
const now = Date.now();
2724
return {
28-
assertionResults,
25+
assertionResults: testResult.testResults,
2926
coverage: {},
3027
endTime: now,
31-
message: testResult.failureMessage
32-
? testResult.failureMessage
33-
: testResult.testExecError.message,
28+
message: testResult.failureMessage ?? testResult.testExecError.message,
3429
name: testResult.testFilePath,
3530
startTime: now,
3631
status: 'failed',
3732
summary: '',
3833
};
39-
} else {
40-
const allTestsPassed = testResult.numFailingTests === 0;
41-
return {
42-
assertionResults,
43-
coverage: codeCoverageFormatter
44-
? codeCoverageFormatter(testResult.coverage, reporter)
45-
: testResult.coverage,
46-
endTime: testResult.perfStats.end,
47-
message: testResult.failureMessage || '',
48-
name: testResult.testFilePath,
49-
startTime: testResult.perfStats.start,
50-
status: allTestsPassed ? 'passed' : 'failed',
51-
summary: '',
52-
};
5334
}
54-
};
5535

56-
function formatTestAssertion(
57-
assertion: AssertionResult,
58-
): FormattedAssertionResult {
59-
const result: FormattedAssertionResult = {
60-
ancestorTitles: assertion.ancestorTitles,
61-
duration: assertion.duration,
62-
failureMessages: null,
63-
fullName: assertion.fullName,
64-
location: assertion.location,
65-
status: assertion.status,
66-
title: assertion.title,
36+
const allTestsPassed = testResult.numFailingTests === 0;
37+
return {
38+
assertionResults: testResult.testResults,
39+
coverage:
40+
codeCoverageFormatter != null
41+
? codeCoverageFormatter(testResult.coverage, reporter)
42+
: testResult.coverage,
43+
endTime: testResult.perfStats.end,
44+
message: testResult.failureMessage ?? '',
45+
name: testResult.testFilePath,
46+
startTime: testResult.perfStats.start,
47+
status: allTestsPassed ? 'passed' : 'failed',
48+
summary: '',
6749
};
68-
if (assertion.failureMessages) {
69-
result.failureMessages = assertion.failureMessages;
70-
}
71-
return result;
72-
}
50+
};
7351

7452
export default function formatTestResults(
7553
results: AggregatedResult,

0 commit comments

Comments
 (0)