Skip to content

Commit 968a1de

Browse files
authored
deps: upgrade jest packages to 29.7.0 (#16147)
1 parent aba2c11 commit 968a1de

File tree

14 files changed

+287
-173
lines changed

14 files changed

+287
-173
lines changed

cli/test/cli/bin-test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
import {pathToFileURL} from 'url';
88

99
import * as td from 'testdouble';
10-
import jestMock from 'jest-mock';
1110

1211
import {LH_ROOT} from '../../../shared/root.js';
13-
import {readJson} from '../../../core/test/test-utils.js';
12+
import {fnAny, readJson} from '../../../core/test/test-utils.js';
1413

15-
const mockRunLighthouse = jestMock.fn();
16-
const mockGetFlags = jestMock.fn();
17-
const mockAskPermission = jestMock.fn();
18-
const mockSentryInit = jestMock.fn();
19-
const mockLoggerSetLevel = jestMock.fn();
14+
const mockRunLighthouse = fnAny();
15+
const mockGetFlags = fnAny();
16+
const mockAskPermission = fnAny();
17+
const mockSentryInit = fnAny();
18+
const mockLoggerSetLevel = fnAny();
2019

2120
/** @type {import('../../bin.js')} */
2221
let bin;

core/test/config/config-test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import jestMock from 'jest-mock';
8-
97
import {Audit as BaseAudit} from '../../audits/audit.js';
108
import BaseGatherer from '../../gather/base-gatherer.js';
119
import {initializeConfig, getConfigDisplayString} from '../../config/config.js';
1210
import {LH_ROOT} from '../../../shared/root.js';
1311
import * as format from '../../../shared/localization/format.js';
1412
import defaultConfig from '../../config/default-config.js';
1513
import {nonSimulatedSettingsOverrides} from '../../config/constants.js';
14+
import {fnAny} from '../test-utils.js';
1615

1716
describe('Config', () => {
1817
/** @type {LH.Gatherer.GatherMode} */
@@ -106,14 +105,14 @@ describe('Config', () => {
106105

107106
it('should throw on invalid artifact definitions', async () => {
108107
const badGatherer = new BaseGatherer();
109-
badGatherer.getArtifact = jestMock.fn();
108+
badGatherer.getArtifact = fnAny();
110109
const config = {artifacts: [{id: 'BadGatherer', gatherer: {instance: badGatherer}}]};
111110
await expect(initializeConfig(gatherMode, config)).rejects.toThrow(/Gatherer for BadGather/);
112111
});
113112

114113
it('should filter configuration by gatherMode', async () => {
115114
const timespanGatherer = new BaseGatherer();
116-
timespanGatherer.getArtifact = jestMock.fn();
115+
timespanGatherer.getArtifact = fnAny();
117116
timespanGatherer.meta = {supportedModes: ['timespan']};
118117

119118
const config = {
@@ -186,11 +185,11 @@ describe('Config', () => {
186185
beforeEach(() => {
187186
const dependencySymbol = Symbol('dependency');
188187
dependencyGatherer = new BaseGatherer();
189-
dependencyGatherer.getArtifact = jestMock.fn();
188+
dependencyGatherer.getArtifact = fnAny();
190189
dependencyGatherer.meta = {symbol: dependencySymbol, supportedModes: ['snapshot']};
191190
// @ts-expect-error - we satisfy the interface on the next line
192191
dependentGatherer = new BaseGatherer();
193-
dependentGatherer.getArtifact = jestMock.fn();
192+
dependentGatherer.getArtifact = fnAny();
194193
dependentGatherer.meta = {
195194
supportedModes: ['snapshot'],
196195
dependencies: {ImageElements: dependencySymbol},
@@ -250,7 +249,7 @@ describe('Config', () => {
250249

251250
beforeEach(() => {
252251
const gatherer = new BaseGatherer();
253-
gatherer.getArtifact = jestMock.fn();
252+
gatherer.getArtifact = fnAny();
254253
gatherer.meta = {supportedModes: ['navigation']};
255254

256255
class ExtraAudit extends BaseAudit {

core/test/gather/driver/execution-context-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ describe('ExecutionContext', () => {
4141
it('should clear context on frame navigations', async () => {
4242
const executionContext = new ExecutionContext(sessionMock);
4343

44-
const frameListener = sessionMock.on.mock.calls.find(call => call[0] === 'Page.frameNavigated');
45-
expect(frameListener).toBeDefined();
44+
const frameListener = sessionMock.on.mock.calls
45+
.find(call => call[0] === 'Page.frameNavigated') ?? [];
46+
expect(frameListener[1]).toBeDefined();
4647

4748
await forceNewContextId(executionContext, 42);
4849
expect(executionContext.getContextId()).toEqual(42);
@@ -54,8 +55,8 @@ describe('ExecutionContext', () => {
5455
const executionContext = new ExecutionContext(sessionMock);
5556

5657
const executionDestroyed = sessionMock.on.mock.calls
57-
.find(call => call[0] === 'Runtime.executionContextDestroyed');
58-
expect(executionDestroyed).toBeDefined();
58+
.find(call => call[0] === 'Runtime.executionContextDestroyed') ?? [];
59+
expect(executionDestroyed[1]).toBeDefined();
5960

6061
await forceNewContextId(executionContext, 42);
6162
expect(executionContext.getContextId()).toEqual(42);

core/test/gather/driver/target-manager-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('TargetManager', () => {
8383
await targetManager.enable();
8484

8585
expect(sessionMock.on).toHaveBeenCalled();
86-
const sessionListener = sessionMock.on.mock.calls.find(c => c[0] === 'sessionattached')[1];
86+
const sessionListener = sessionMock.on.mock.calls.find(c => c[0] === 'sessionattached')?.[1];
8787

8888
// Original, attach.
8989
expect(sendMock.findAllInvocations('Target.getTargetInfo')).toHaveLength(1);

core/test/gather/gatherers/stacks-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import StacksGatherer from '../../../gather/gatherers/stacks.js';
88
import {fnAny} from '../../test-utils.js';
99

1010
describe('StacksGatherer', () => {
11-
/** @type {{executionContext: {evaluate: Mock<any, any>}}} */
11+
/** @type {{executionContext: {evaluate: Mock<any>}}} */
1212
let driver;
1313

1414
beforeEach(() => {

core/test/gather/mock-commands.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ function createMockSendCommandFn() {
8484
*/
8585
findInvocation(command) {
8686
expect(mockFn).toHaveBeenCalledWith(command, expect.anything());
87-
return mockFn.mock.calls.find(
88-
call => call[0] === command
89-
)[1];
87+
const call = mockFn.mock.calls.find(call => call[0] === command);
88+
if (!call) throw new Error(`missing invocation for command: ${command}`);
89+
return call[1];
9090
},
9191
/**
9292
* @param {keyof LH.CrdpCommands} command
@@ -140,7 +140,9 @@ function createMockOnceFn() {
140140
*/
141141
findListener(event) {
142142
expect(mockFn).toHaveBeenCalledWith(event, expect.anything());
143-
return mockFn.mock.calls.find(call => call[0] === event)[1];
143+
const call = mockFn.mock.calls.find(call => call[0] === event);
144+
if (!call) throw new Error(`missing listener for event: ${event}`);
145+
return call[1];
144146
},
145147
/**
146148
* @param {keyof LH.CrdpEvents} event
@@ -193,7 +195,9 @@ function createMockOnFn() {
193195
*/
194196
findListener(event) {
195197
expect(mockFn).toHaveBeenCalledWith(event, expect.anything());
196-
return mockFn.mock.calls.find(call => call[0] === event)[1];
198+
const call = mockFn.mock.calls.find(call => call[0] === event);
199+
if (!call) throw new Error(`missing listener for event: ${event}`);
200+
return call[1];
197201
},
198202
/**
199203
* @param {keyof LH.CrdpEvents} event

core/test/gather/navigation-runner-test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import jestMock from 'jest-mock';
8-
97
import {
108
createMockDriver,
119
createMockBaseArtifacts,
@@ -33,7 +31,7 @@ const DevtoolsLogGatherer = (await import('../../gather/gatherers/devtools-log.j
3331
const TraceGatherer = (await import('../../gather/gatherers/trace.js')).default;
3432
const {initializeConfig} = await import('../../config/config.js');
3533

36-
/** @typedef {{meta: LH.Gatherer.GathererMeta<'Accessibility'>, getArtifact: Mock<any, any>, startInstrumentation: Mock<any, any>, stopInstrumentation: Mock<any, any>, startSensitiveInstrumentation: Mock<any, any>, stopSensitiveInstrumentation: Mock<any, any>}} MockGatherer */
34+
/** @typedef {{meta: LH.Gatherer.GathererMeta<'Accessibility'>, getArtifact: Mock<any>, startInstrumentation: Mock<any>, stopInstrumentation: Mock<any>, startSensitiveInstrumentation: Mock<any>, stopSensitiveInstrumentation: Mock<any>}} MockGatherer */
3735

3836
describe('NavigationRunner', () => {
3937
let requestedUrl = '';
@@ -251,8 +249,7 @@ describe('NavigationRunner', () => {
251249
if (!resolvedConfig.artifacts) throw new Error('No artifacts');
252250

253251
const err = new Error('Error in dependency chain');
254-
resolvedConfig.artifacts[0].gatherer.instance.startInstrumentation = jestMock
255-
.fn()
252+
resolvedConfig.artifacts[0].gatherer.instance.startInstrumentation = fnAny()
256253
.mockRejectedValue(err);
257254
resolvedConfig.artifacts[1].dependencies = {Accessibility: {id: 'Timespan'}};
258255
resolvedConfig.artifacts[2].dependencies = {Accessibility: {id: 'Timespan'}};

core/test/test-env/mocha-setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function getSnapshotState(testFile) {
6363
const snapshotDir = path.join(path.dirname(testFile), '__snapshots__');
6464
const snapshotFile = path.join(snapshotDir, path.basename(testFile) + '.snap');
6565
snapshotState = new SnapshotState(snapshotFile, {
66+
rootDir: snapshotDir,
6667
updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'none',
6768
prettierPath: '',
6869
snapshotFormat: {},
@@ -120,7 +121,6 @@ expect.extend({
120121
const context = {snapshotState, currentTestName: title};
121122
// @ts-expect-error - this is enough for snapshots to work.
122123
const matcher = toMatchInlineSnapshot.bind(context);
123-
// @ts-expect-error - not sure why these types are so wrong
124124
const result = matcher(actual, expected);
125125
// @ts-expect-error - not sure why these types are so wrong
126126
if (!result.pass) snapshotTestFailed = true;

core/test/test-utils.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async function makeMocksForGatherRunner() {
207207
});
208208
await td.replaceEsm('../gather/driver/prepare.js', {
209209
prepareTargetForNavigationMode: jestMock.fn(),
210-
prepareTargetForIndividualNavigation: jestMock.fn().mockResolvedValue({warnings: []}),
210+
prepareTargetForIndividualNavigation: fnAny().mockResolvedValue({warnings: []}),
211211
enableAsyncStacks: jestMock.fn().mockReturnValue(jestMock.fn()),
212212
});
213213
await td.replaceEsm('../gather/driver/storage.js', {
@@ -216,7 +216,7 @@ async function makeMocksForGatherRunner() {
216216
getImportantStorageWarning: jestMock.fn(),
217217
});
218218
await td.replaceEsm('../gather/driver/navigation.js', {
219-
gotoURL: jestMock.fn().mockResolvedValue({
219+
gotoURL: fnAny().mockResolvedValue({
220220
mainDocumentUrl: 'http://example.com',
221221
warnings: [],
222222
}),
@@ -225,9 +225,11 @@ async function makeMocksForGatherRunner() {
225225

226226
/**
227227
* Same as jestMock.fn(), but uses `any` instead of `unknown`.
228+
* This makes it simpler to override existing properties in test files that are
229+
* typechecked.
228230
*/
229231
const fnAny = () => {
230-
return /** @type {Mock<any, any>} */ (jestMock.fn());
232+
return /** @type {Mock<any>} */ (jestMock.fn());
231233
};
232234

233235
/**
@@ -288,7 +290,7 @@ function getURLArtifactFromDevtoolsLog(devtoolsLog) {
288290
*
289291
* @param {string} modulePath
290292
* @param {ImportMeta} importMeta
291-
* @return {Promise<Record<string, Mock<any, any>>>}
293+
* @return {Promise<Record<string, Mock<any>>>}
292294
*/
293295
async function importMock(modulePath, importMeta) {
294296
const mock = await import(new URL(modulePath, importMeta.url).href);
@@ -304,7 +306,7 @@ async function importMock(modulePath, importMeta) {
304306
*
305307
* @param {string} modulePath
306308
* @param {ImportMeta} importMeta
307-
* @return {Record<string, Mock<any, any>>}
309+
* @return {Record<string, Mock<any>>}
308310
*/
309311
function requireMock(modulePath, importMeta) {
310312
const dir = path.dirname(url.fileURLToPath(importMeta.url));

core/test/user-flow-test.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import jestMock from 'jest-mock';
87
import * as td from 'testdouble';
98

109
import {Runner} from '../runner.js';
1110
import {createMockPage, mockRunnerModule} from './gather/mock-driver.js';
11+
import {fnAny} from './test-utils.js';
1212

13-
const snapshotModule = {snapshotGather: jestMock.fn()};
13+
const snapshotModule = {snapshotGather: fnAny()};
1414
await td.replaceEsm('../gather/snapshot-runner.js', snapshotModule);
15-
const navigationModule = {navigationGather: jestMock.fn()};
15+
const navigationModule = {navigationGather: fnAny()};
1616
await td.replaceEsm('../gather/navigation-runner.js', navigationModule);
17-
const timespanModule = {startTimespanGather: jestMock.fn()};
17+
const timespanModule = {startTimespanGather: fnAny()};
1818
await td.replaceEsm('../gather/timespan-runner.js', timespanModule);
1919

2020
const mockRunner = await mockRunnerModule();
@@ -68,7 +68,7 @@ describe('UserFlow', () => {
6868
computedCache: new Map(),
6969
},
7070
};
71-
const timespan = {endTimespanGather: jestMock.fn().mockResolvedValue(timespanGatherResult)};
71+
const timespan = {endTimespanGather: fnAny().mockResolvedValue(timespanGatherResult)};
7272
timespanModule.startTimespanGather.mockReset();
7373
timespanModule.startTimespanGather.mockResolvedValue(timespan);
7474
});
@@ -206,7 +206,6 @@ describe('UserFlow', () => {
206206
let teardownDone = false;
207207
navigationModule.navigationGather.mockImplementation(async (_, cb) => {
208208
setupDone = true;
209-
// @ts-expect-error
210209
await cb();
211210
teardownDone = true;
212211
});
@@ -238,7 +237,6 @@ describe('UserFlow', () => {
238237

239238
it('should throw errors from the teardown phase', async () => {
240239
navigationModule.navigationGather.mockImplementation(async (_, cb) => {
241-
// @ts-expect-error
242240
await cb();
243241
throw new Error('Teardown Error');
244242
});

0 commit comments

Comments
 (0)