Skip to content

Commit e5afea2

Browse files
bmeurerDevtools-frontend LUCI CQ
authored andcommitted
Consistently prefer assert.lengthOf to check array-like lengths.
In our tests, we should stick to ideally just one way of asserting array-like lengths, `assert.lengthOf`, and avoid any kind of combinations `assert.equal`,`assert.strictEqual`, `assert.deepEqual`, or `assert.deepStrictEqual`. Bug: 386335487 Change-Id: I8f88e214acdae0c6e34dbb169c62ebc9a80317af Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6113832 Reviewed-by: Changhao Han <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]>
1 parent af68355 commit e5afea2

File tree

139 files changed

+787
-567
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+787
-567
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,12 @@ module.exports = {
292292
'mocha/no-nested-tests': 'error',
293293

294294
'rulesdir/check-test-definitions' : 'error',
295+
'rulesdir/compare-arrays-with-assert-deepequal' : 'error',
295296
'rulesdir/no-assert-equal' : 'error',
296297
'rulesdir/no-assert-deep-strict-equal' : 'error',
297298
'rulesdir/no-repeated-tests' : 'error',
298-
'rulesdir/compare-arrays-with-assert-deepequal' : 'error',
299299
'rulesdir/no-screenshot-test-outside-perf-panel' : 'error',
300+
'rulesdir/prefer-assert-length-of' : 'error',
300301
'rulesdir/trace-engine-test-timeouts' : 'error',
301302
'@typescript-eslint/no-non-null-assertion' : 'off',
302303
},

front_end/core/common/Console.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Console', () => {
2525
console.addMessage('Bar', Common.Console.MessageLevel.ERROR, true);
2626
console.addMessage('Donkey', Common.Console.MessageLevel.INFO, true);
2727
const messages = console.messages();
28-
assert.strictEqual(messages.length, 4);
28+
assert.lengthOf(messages, 4);
2929
});
3030

3131
it('dispatches events to listeners', done => {

front_end/core/common/SettingRegistration.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ describe('SettingRegistration', () => {
7070
const enableSettingCommands = allCommands.filter(
7171
command => command.title === enableTitle &&
7272
command.category === Common.Settings.getLocalizedSettingsCategory(settingCategory));
73-
assert.strictEqual(
74-
disableSettingCommands.length, 1, 'Commands for changing a setting\'s value were not added correctly');
75-
assert.strictEqual(
76-
enableSettingCommands.length, 1, 'Commands for changing a setting\'s value were not added correctly');
73+
assert.lengthOf(disableSettingCommands, 1, 'Commands for changing a setting\'s value were not added correctly');
74+
assert.lengthOf(enableSettingCommands, 1, 'Commands for changing a setting\'s value were not added correctly');
7775
});
7876

7977
it('triggers a setting\'s change listener when a setting is set', () => {

front_end/core/platform/TypedArrayUtilities.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ describe('TypedArrayUtilities', () => {
88
describe('BigUint32Array', () => {
99
it('can be expandable', () => {
1010
const array = Platform.TypedArrayUtilities.createExpandableBigUint32Array();
11-
assert.strictEqual(array.length, 0);
11+
assert.lengthOf(array, 0);
1212
array.setValue(0, 33);
1313
array.setValue(1, 44);
14-
assert.strictEqual(array.length, 2);
14+
assert.lengthOf(array, 2);
1515
assert.strictEqual(array.getValue(0), 33);
1616
assert.strictEqual(array.getValue(1), 44);
1717
assert.strictEqual(array.asArrayOrFail() as Object, array);
1818
});
1919
it('can act as a Uint32Array', () => {
2020
const array = Platform.TypedArrayUtilities.createFixedBigUint32Array(15);
21-
assert.strictEqual(array.length, 15);
21+
assert.lengthOf(array, 15);
2222
assert.strictEqual(array.getValue(7), 0);
2323
array.setValue(7, 77);
2424
assert.strictEqual(array.getValue(7), 77);
2525
assert.strictEqual(array.asUint32ArrayOrFail() as Object, array);
2626
});
2727
it('can be bigger than a Uint32Array', () => {
2828
const array = Platform.TypedArrayUtilities.createFixedBigUint32Array(12_345_678, /* maxLengthForTesting=*/ 2e6);
29-
assert.strictEqual(array.length, 12_345_678);
29+
assert.lengthOf(array, 12_345_678);
3030
assert.strictEqual(array.getValue(0), 0);
3131
assert.strictEqual(array.getValue(500_000), 0);
3232
assert.strictEqual(array.getValue(5_000_000), 0);

front_end/core/sdk/CSSStyleDeclaration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describeWithMockConnection('CSSStyleDeclaration', () => {
249249

250250
const style = new SDK.CSSStyleDeclaration.CSSStyleDeclaration(
251251
cssModel, null, stubCSSStyle, SDK.CSSStyleDeclaration.Type.Regular);
252-
assert.strictEqual(style.allProperties().length, 1);
252+
assert.lengthOf(style.allProperties(), 1);
253253
assertPropertValues(style.allProperties()[0], [
254254
['name', '-webkit-background-clip'],
255255
['value', 'border-box'],

front_end/core/sdk/ChildTargetManager.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ describeWithMockConnection('ChildTargetManager', () => {
3636
it('adds subtargets', async () => {
3737
const target = createTarget();
3838
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
39-
assert.strictEqual(childTargetManager.childTargets().length, 0);
39+
assert.lengthOf(childTargetManager.childTargets(), 0);
4040
await childTargetManager.attachedToTarget(
4141
{sessionId: createSessionId(), targetInfo: createTargetInfo(TARGET_ID), waitingForDebugger: false});
42-
assert.strictEqual(childTargetManager.childTargets().length, 1);
42+
assert.lengthOf(childTargetManager.childTargets(), 1);
4343
assert.strictEqual(childTargetManager.childTargets()[0].id(), TARGET_ID);
4444
});
4545

4646
it('sets subtarget type', async () => {
4747
const target = createTarget();
4848
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
49-
assert.strictEqual(childTargetManager.childTargets().length, 0);
49+
assert.lengthOf(childTargetManager.childTargets(), 0);
5050
for (const [protocolType, sdkType] of [
5151
['iframe', SDK.Target.Type.FRAME],
5252
['webview', SDK.Target.Type.FRAME],
@@ -73,7 +73,7 @@ describeWithMockConnection('ChildTargetManager', () => {
7373
it('sets subtarget to frame for devtools scheme if type is other', async () => {
7474
const target = createTarget();
7575
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
76-
assert.strictEqual(childTargetManager.childTargets().length, 0);
76+
assert.lengthOf(childTargetManager.childTargets(), 0);
7777
await childTargetManager.attachedToTarget({
7878
sessionId: createSessionId(),
7979
targetInfo: createTargetInfo(undefined, 'other', 'devtools://foo/bar'),
@@ -94,7 +94,7 @@ describeWithMockConnection('ChildTargetManager', () => {
9494
it('sets subtarget to frame for chrome://print/ if type is other', async () => {
9595
const target = createTarget();
9696
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
97-
assert.strictEqual(childTargetManager.childTargets().length, 0);
97+
assert.lengthOf(childTargetManager.childTargets(), 0);
9898
await childTargetManager.attachedToTarget({
9999
sessionId: createSessionId(),
100100
targetInfo: createTargetInfo(undefined, 'other', 'chrome://print/'),
@@ -107,7 +107,7 @@ describeWithMockConnection('ChildTargetManager', () => {
107107
it('sets subtarget to frame for chrome://file-manager/ if type is other', async () => {
108108
const target = createTarget();
109109
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
110-
assert.strictEqual(childTargetManager.childTargets().length, 0);
110+
assert.lengthOf(childTargetManager.childTargets(), 0);
111111
await childTargetManager.attachedToTarget({
112112
sessionId: createSessionId(),
113113
targetInfo: createTargetInfo(undefined, 'other', 'chrome://file-manager/?%7B%22allowedPaths%22:%22anyPathOrUrl'),
@@ -120,7 +120,7 @@ describeWithMockConnection('ChildTargetManager', () => {
120120
it('sets subtarget to frame for sidebar URLs if type is other', async () => {
121121
const target = createTarget();
122122
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
123-
assert.strictEqual(childTargetManager.childTargets().length, 0);
123+
assert.lengthOf(childTargetManager.childTargets(), 0);
124124
await childTargetManager.attachedToTarget({
125125
sessionId: createSessionId(),
126126
targetInfo: createTargetInfo(undefined, 'other', 'chrome://read-later.top-chrome/'),
@@ -141,7 +141,7 @@ describeWithMockConnection('ChildTargetManager', () => {
141141
it('sets worker target name to the target title', async () => {
142142
const target = createTarget();
143143
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
144-
assert.strictEqual(childTargetManager.childTargets().length, 0);
144+
assert.lengthOf(childTargetManager.childTargets(), 0);
145145
await childTargetManager.attachedToTarget({
146146
sessionId: createSessionId(),
147147
targetInfo: createTargetInfo(undefined, 'worker', 'http://example.com/worker.js', TITLE),
@@ -153,7 +153,7 @@ describeWithMockConnection('ChildTargetManager', () => {
153153
it('sets non-frame target name to the last path component if present', async () => {
154154
const target = createTarget();
155155
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
156-
assert.strictEqual(childTargetManager.childTargets().length, 0);
156+
assert.lengthOf(childTargetManager.childTargets(), 0);
157157
await childTargetManager.attachedToTarget({
158158
sessionId: createSessionId(),
159159
targetInfo: createTargetInfo(undefined, 'service_worker', 'http://example.org/service_worker.html', TITLE),
@@ -171,7 +171,7 @@ describeWithMockConnection('ChildTargetManager', () => {
171171
it('sets non-frame target a numbered name if it cannot use URL path', async () => {
172172
const target = createTarget();
173173
const childTargetManager = new SDK.ChildTargetManager.ChildTargetManager(target);
174-
assert.strictEqual(childTargetManager.childTargets().length, 0);
174+
assert.lengthOf(childTargetManager.childTargets(), 0);
175175
await childTargetManager.attachedToTarget({
176176
sessionId: createSessionId(),
177177
targetInfo: createTargetInfo(undefined, 'page', 'data:text/html,<!doctype html>'),

front_end/core/sdk/EnhancedTracesParser.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('EnhancedTracesParser', () => {
143143
assert.deepEqual(target, target2);
144144
}
145145
}
146-
assert.strictEqual(targets.length, 2);
146+
assert.lengthOf(targets, 2);
147147
});
148148

149149
it('captures execution context info', async function() {
@@ -157,7 +157,7 @@ describe('EnhancedTracesParser', () => {
157157
assert.fail('Contexts and Scripts should not be null or undefined');
158158
}
159159
}
160-
assert.strictEqual(executionContexts.length, 3);
160+
assert.lengthOf(executionContexts, 3);
161161
for (const executionContext of executionContexts) {
162162
if (executionContext.id === 1 && executionContext.isolate === '12345') {
163163
assert.deepEqual(executionContext, executionContext1);
@@ -180,7 +180,7 @@ describe('EnhancedTracesParser', () => {
180180
assert.fail('Contexts and Scripts should not be null or undefined');
181181
}
182182
}
183-
assert.strictEqual(scripts.length, 3);
183+
assert.lengthOf(scripts, 3);
184184
for (const script of scripts) {
185185
if (script.scriptId === '1' && script.isolate === '12345') {
186186
assert.deepEqual(script, script1);
@@ -200,7 +200,7 @@ describe('EnhancedTracesParser', () => {
200200
const executionContexts = contextsAndScripts[0];
201201
const scripts = contextsAndScripts[1];
202202
if (target.pid === 8050) {
203-
assert.strictEqual(executionContexts.length, 2);
203+
assert.lengthOf(executionContexts, 2);
204204
for (const executionContext of executionContexts) {
205205
// We should be able to get the correct execution context without specifying isolate
206206
// as the contexts and scripts are grouped under its repsective target already.
@@ -210,7 +210,7 @@ describe('EnhancedTracesParser', () => {
210210
assert.deepEqual(executionContext, executionContext2);
211211
}
212212
}
213-
assert.strictEqual(scripts.length, 2);
213+
assert.lengthOf(scripts, 2);
214214
for (const script of scripts) {
215215
if (script.scriptId === '1') {
216216
assert.deepEqual(script, script1);
@@ -219,8 +219,8 @@ describe('EnhancedTracesParser', () => {
219219
}
220220
}
221221
} else if (target.pid === 8051) {
222-
assert.strictEqual(executionContexts.length, 1);
223-
assert.strictEqual(scripts.length, 1);
222+
assert.lengthOf(executionContexts, 1);
223+
assert.lengthOf(scripts, 1);
224224
assert.deepEqual(executionContexts[0], executionContext3);
225225
assert.deepEqual(scripts[0], script3);
226226
}

front_end/core/sdk/NetworkManager.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describeWithMockConnection('MultitargetNetworkManager', () => {
3737
{requestId: 'mockId', request: {url: 'example.com'}} as Protocol.Network.RequestWillBeSentEvent);
3838

3939
// 3) Check that the resulting NetworkRequest has the Trust Token Event data associated with it.
40-
assert.strictEqual(startedRequests.length, 1);
40+
assert.lengthOf(startedRequests, 1);
4141
assert.strictEqual(startedRequests[0].trustTokenOperationDoneEvent(), mockEvent);
4242
});
4343
});

front_end/core/sdk/NetworkRequest.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('NetworkRequest', () => {
3434
responseHeaders: [{name: 'Set-Cookie', value: 'foo=bar'}, {name: 'Set-Cookie', value: 'baz=qux'}],
3535
resourceIPAddressSpace: 'Public' as Protocol.Network.IPAddressSpace,
3636
} as unknown as SDK.NetworkRequest.ExtraResponseInfo);
37-
assert.strictEqual(request.responseCookies.length, 2);
37+
assert.lengthOf(request.responseCookies, 2);
3838
expectCookie(request.responseCookies[0], {name: 'foo', value: 'bar', size: 8});
3939
expectCookie(request.responseCookies[1], {name: 'baz', value: 'qux', size: 7});
4040
});
@@ -89,7 +89,7 @@ describe('NetworkRequest', () => {
8989
resourceIPAddressSpace: 'Public' as Protocol.Network.IPAddressSpace,
9090
cookiePartitionKey: {topLevelSite: 'partitionKey', hasCrossSiteAncestor: false},
9191
} as unknown as SDK.NetworkRequest.ExtraResponseInfo);
92-
assert.strictEqual(request.responseCookies.length, 2);
92+
assert.lengthOf(request.responseCookies, 2);
9393
expectCookie(request.responseCookies[0], {name: 'foo', value: 'bar', size: 8});
9494
expectCookie(request.responseCookies[1], {
9595
name: 'baz',

front_end/core/sdk/RehydratingConnection.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('RehydratingSession', () => {
136136
method: 'Debugger.enable',
137137
sessionId,
138138
});
139-
assert.strictEqual(mockRehydratingConnection.messageQueue.length, 3);
139+
assert.lengthOf(mockRehydratingConnection.messageQueue, 3);
140140
const scriptParsedMessages = mockRehydratingConnection.messageQueue.slice(0, 2);
141141
const resultMessage = mockRehydratingConnection.messageQueue.slice(2);
142142
for (const scriptParsedMessage of scriptParsedMessages) {
@@ -155,7 +155,7 @@ describe('RehydratingSession', () => {
155155
method: 'Runtime.enable',
156156
sessionId,
157157
});
158-
assert.strictEqual(mockRehydratingConnection.messageQueue.length, 3);
158+
assert.lengthOf(mockRehydratingConnection.messageQueue, 3);
159159
const executionContextCreatedMessages = mockRehydratingConnection.messageQueue.slice(0, 2);
160160
const resultMessage = mockRehydratingConnection.messageQueue.slice(2);
161161
for (const executionContextCreatedMessage of executionContextCreatedMessages) {
@@ -179,7 +179,7 @@ describe('RehydratingSession', () => {
179179
scriptId: mockScript1.scriptId,
180180
},
181181
});
182-
assert.strictEqual(mockRehydratingConnection.messageQueue.length, 1);
182+
assert.lengthOf(mockRehydratingConnection.messageQueue, 1);
183183
const scriptSourceTextMessage = mockRehydratingConnection.messageQueue[0];
184184
assert.isNotNull(scriptSourceTextMessage);
185185
assert.strictEqual(scriptSourceTextMessage.id, messageId);

0 commit comments

Comments
 (0)