Skip to content

Commit 2116cf6

Browse files
committed
test: port dependabot reminder tests to vitest and fix lint
Converts the node:test assertions to vitest, uses async fs in the CLI, and adds the process.env suppression the other scripts use. Coverage thresholds were bumped by the vitest autoUpdate ratchet.
1 parent 8202206 commit 2116cf6

9 files changed

Lines changed: 96 additions & 95 deletions

File tree

‎.github/scripts/dependabot-pr-reminder/cli.mts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { appendFileSync, writeFileSync } from 'node:fs';
1+
import { appendFile, writeFile } from 'node:fs/promises';
22
import { join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44

@@ -96,16 +96,16 @@ export async function main(environment: NodeJS.ProcessEnv): Promise<void> {
9696

9797
if (!payload) {
9898
console.log('No open Dependabot pull requests to report.');
99-
appendFileSync(outputPath, 'has-prs=false\n');
99+
await appendFile(outputPath, 'has-prs=false\n');
100100
return;
101101
}
102102

103103
const payloadPath = join(
104104
environment.RUNNER_TEMP ?? '.',
105105
'dependabot-reminder-payload.json',
106106
);
107-
writeFileSync(payloadPath, JSON.stringify(payload));
108-
appendFileSync(outputPath, `has-prs=true\npayload-path=${payloadPath}\n`);
107+
await writeFile(payloadPath, JSON.stringify(payload));
108+
await appendFile(outputPath, `has-prs=true\npayload-path=${payloadPath}\n`);
109109
console.log(`Wrote Slack payload with ${payload.blocks.length} sections.`);
110110
}
111111

‎.github/scripts/dependabot-pr-reminder/github-api.mts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { execFile } from 'node:child_process';
22
import { promisify } from 'node:util';
33

4-
import type { PullRequest } from './types.mts';
54
import { retry } from '../shared/retry.mts';
5+
import type { PullRequest } from './types.mts';
66

77
const execFileAsync = promisify(execFile);
88

‎.github/scripts/dependabot-pr-reminder/slack-payload.mts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { normalizeTeam } from './teams.mts';
22
import type { PullRequest, Reminder, Topology } from './types.mts';
33

4+
type SlackBlock = { type: string; text: { type: string; text: string } };
5+
46
/**
57
* Escapes characters that Slack mrkdwn treats as control characters.
68
*
@@ -82,7 +84,7 @@ export function buildSlackPayload(
8284
text: string;
8385
// eslint-disable-next-line @typescript-eslint/naming-convention
8486
link_names: boolean;
85-
blocks: { type: string; text: { type: string; text: string } }[];
87+
blocks: SlackBlock[];
8688
}
8789
| undefined {
8890
const teamEntries = Object.entries(reminder.byTeam).filter(
@@ -109,7 +111,7 @@ export function buildSlackPayload(
109111
return id ? `<!subteam^${id}>` : slug;
110112
};
111113

112-
const section = (text: string) => ({
114+
const section = (text: string): SlackBlock => ({
113115
type: 'section',
114116
text: { type: 'mrkdwn', text },
115117
});
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
import assert from 'node:assert/strict';
2-
import { describe, it } from 'node:test';
1+
import { describe, expect, it } from 'vitest';
32

4-
import { pr } from './fixtures.mts';
53
import { classifyPullRequests } from '../../dependabot-pr-reminder/classify.mts';
4+
import { pr } from './fixtures.mts';
65

76
describe('classifyPullRequests', () => {
87
it('groups a PR under each remaining requested team', () => {
98
const pullRequest = pr({ requestedTeams: ['a', 'b'] });
109
const result = classifyPullRequests([pullRequest], { fallbackTeam: 'f' });
11-
assert.deepEqual(result.byTeam.a, [pullRequest]);
12-
assert.deepEqual(result.byTeam.b, [pullRequest]);
10+
expect(result.byTeam.a).toStrictEqual([pullRequest]);
11+
expect(result.byTeam.b).toStrictEqual([pullRequest]);
1312
});
1413

1514
it('lists failing CI PRs under fallback only', () => {
1615
const pullRequest = pr({ ciFailing: true, requestedTeams: ['a'] });
1716
const result = classifyPullRequests([pullRequest], { fallbackTeam: 'f' });
18-
assert.deepEqual(result.fallback.failingCi, [pullRequest]);
19-
assert.deepEqual(result.byTeam, {});
17+
expect(result.fallback.failingCi).toStrictEqual([pullRequest]);
18+
expect(result.byTeam).toStrictEqual({});
2019
});
2120

2221
it('lists approved PRs with no teams left as ready to merge', () => {
2322
const pullRequest = pr({ requestedTeams: [], hasApproval: true });
2423
const result = classifyPullRequests([pullRequest], { fallbackTeam: 'f' });
25-
assert.deepEqual(result.fallback.readyToMerge, [pullRequest]);
24+
expect(result.fallback.readyToMerge).toStrictEqual([pullRequest]);
2625
});
2726

2827
it('lists PRs with no teams and no approval as needs owner', () => {
2928
const pullRequest = pr();
3029
const result = classifyPullRequests([pullRequest], { fallbackTeam: 'f' });
31-
assert.deepEqual(result.fallback.needsOwner, [pullRequest]);
30+
expect(result.fallback.needsOwner).toStrictEqual([pullRequest]);
3231
});
3332

3433
it('filters other teams out when ownerTeams is set', () => {
@@ -37,8 +36,8 @@ describe('classifyPullRequests', () => {
3736
ownerTeams: ['extension-platform'],
3837
fallbackTeam: 'f',
3938
});
40-
assert.deepEqual(Object.keys(result.byTeam), ['extension-platform']);
41-
assert.deepEqual(result.byTeam['extension-platform'], [pullRequest]);
39+
expect(Object.keys(result.byTeam)).toStrictEqual(['extension-platform']);
40+
expect(result.byTeam['extension-platform']).toStrictEqual([pullRequest]);
4241
});
4342

4443
it('lists approved PRs as ready to merge when ownerTeams leaves nothing', () => {
@@ -47,8 +46,8 @@ describe('classifyPullRequests', () => {
4746
ownerTeams: ['extension-platform'],
4847
fallbackTeam: 'f',
4948
});
50-
assert.deepEqual(result.byTeam, {});
51-
assert.deepEqual(result.fallback.readyToMerge, [pullRequest]);
49+
expect(result.byTeam).toStrictEqual({});
50+
expect(result.fallback.readyToMerge).toStrictEqual([pullRequest]);
5251
});
5352

5453
it('normalizes team names before comparing and grouping', () => {
@@ -57,6 +56,6 @@ describe('classifyPullRequests', () => {
5756
ownerTeams: ['@metamask/core'],
5857
fallbackTeam: 'f',
5958
});
60-
assert.deepEqual(Object.keys(result.byTeam), ['core']);
59+
expect(Object.keys(result.byTeam)).toStrictEqual(['core']);
6160
});
6261
});

‎.github/scripts/tests/dependabot-pr-reminder/github-api.test.mts‎

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import assert from 'node:assert/strict';
2-
import { describe, it } from 'node:test';
1+
import { describe, expect, it } from 'vitest';
32

43
import {
54
buildTopologyPath,
@@ -32,48 +31,45 @@ function node(
3231

3332
describe('toPullRequest', () => {
3433
it('returns undefined for drafts', () => {
35-
assert.equal(toPullRequest(node({ isDraft: true }), []), undefined);
34+
expect(toPullRequest(node({ isDraft: true }), [])).toBeUndefined();
3635
});
3736

3837
it('returns undefined for non-Dependabot authors', () => {
39-
assert.equal(
38+
expect(
4039
toPullRequest(
4140
// eslint-disable-next-line @typescript-eslint/naming-convention
4241
node({ author: { __typename: 'Bot', login: 'someone-else' } }),
4342
[],
4443
),
45-
undefined,
46-
);
44+
).toBeUndefined();
4745
});
4846

4947
it('rejects a User named dependabot', () => {
50-
assert.equal(
48+
expect(
5149
toPullRequest(
5250
// eslint-disable-next-line @typescript-eslint/naming-convention
5351
node({ author: { __typename: 'User', login: 'dependabot' } }),
5452
[],
5553
),
56-
undefined,
57-
);
54+
).toBeUndefined();
5855
});
5956

6057
it('accepts a Bot named dependabot[bot]', () => {
61-
assert.equal(
58+
expect(
6259
toPullRequest(
6360
// eslint-disable-next-line @typescript-eslint/naming-convention
6461
node({ author: { __typename: 'Bot', login: 'dependabot[bot]' } }),
6562
[],
6663
)?.number,
67-
7,
68-
);
64+
).toBe(7);
6965
});
7066

7167
it('returns undefined when the author is missing', () => {
72-
assert.equal(toPullRequest(node({ author: null }), []), undefined);
68+
expect(toPullRequest(node({ author: null }), [])).toBeUndefined();
7369
});
7470

7571
it('maps a Dependabot PR', () => {
76-
assert.deepEqual(toPullRequest(node(), []), {
72+
expect(toPullRequest(node(), [])).toStrictEqual({
7773
number: 7,
7874
title: 'Bump foo',
7975
url: 'https://github.com/MetaMask/example/pull/7',
@@ -84,13 +80,13 @@ describe('toPullRequest', () => {
8480
});
8581

8682
it('uses the given requested teams', () => {
87-
assert.deepEqual(toPullRequest(node(), ['wallet'])?.requestedTeams, [
83+
expect(toPullRequest(node(), ['wallet'])?.requestedTeams).toStrictEqual([
8884
'wallet',
8985
]);
9086
});
9187

9288
it('sets hasApproval when approved and nothing requests changes', () => {
93-
const result = (states: string[]) =>
89+
const result = (states: string[]): boolean | undefined =>
9490
toPullRequest(
9591
node({
9692
latestOpinionatedReviews: {
@@ -99,29 +95,35 @@ describe('toPullRequest', () => {
9995
}),
10096
[],
10197
)?.hasApproval;
102-
assert.equal(result(['APPROVED']), true);
103-
assert.equal(result(['APPROVED', 'CHANGES_REQUESTED']), false);
104-
assert.equal(result(['CHANGES_REQUESTED']), false);
105-
assert.equal(result([]), false);
98+
expect(result(['APPROVED'])).toBe(true);
99+
expect(result(['APPROVED', 'CHANGES_REQUESTED'])).toBe(false);
100+
expect(result(['CHANGES_REQUESTED'])).toBe(false);
101+
expect(result([])).toBe(false);
106102
});
107103

108104
it('sets ciFailing only for FAILURE and ERROR', () => {
109-
const rollup = (state: string | null) =>
105+
const rollup = (
106+
statusCheckRollup: { state: string } | null,
107+
): boolean | undefined =>
110108
toPullRequest(
111109
node({
112110
commits: {
113111
nodes: [
114-
{ commit: { statusCheckRollup: state ? { state } : null } },
112+
{
113+
commit: {
114+
statusCheckRollup,
115+
},
116+
},
115117
],
116118
},
117119
}),
118120
[],
119121
)?.ciFailing;
120-
assert.equal(rollup('FAILURE'), true);
121-
assert.equal(rollup('ERROR'), true);
122-
assert.equal(rollup('PENDING'), false);
123-
assert.equal(rollup('SUCCESS'), false);
124-
assert.equal(rollup(null), false);
122+
expect(rollup({ state: 'FAILURE' })).toBe(true);
123+
expect(rollup({ state: 'ERROR' })).toBe(true);
124+
expect(rollup({ state: 'PENDING' })).toBe(false);
125+
expect(rollup({ state: 'SUCCESS' })).toBe(false);
126+
expect(rollup(null)).toBe(false);
125127
});
126128
});
127129

@@ -132,19 +134,18 @@ describe('collectRequestedTeams', () => {
132134
[{ number: 1, requested_teams: [{ slug: 'a' }, { slug: 'b' }] }],
133135
[{ number: 2, requested_teams: [] }, { number: 3 }],
134136
]);
135-
assert.deepEqual(map.get(1), ['a', 'b']);
136-
assert.deepEqual(map.get(2), []);
137-
assert.equal(map.get(3), undefined);
138-
assert.equal(map.get(4), undefined);
137+
expect(map.get(1)).toStrictEqual(['a', 'b']);
138+
expect(map.get(2)).toStrictEqual([]);
139+
expect(map.get(3)).toBeUndefined();
140+
expect(map.get(4)).toBeUndefined();
139141
});
140142
});
141143

142144
/* eslint-enable @typescript-eslint/naming-convention */
143145

144146
describe('buildTopologyPath', () => {
145147
it('encodes the ref', () => {
146-
assert.equal(
147-
buildTopologyPath('feat/a b#c'),
148+
expect(buildTopologyPath('feat/a b#c')).toBe(
148149
'repos/MetaMask/MetaMask-planning/contents/topology.json?ref=feat%2Fa%20b%23c',
149150
);
150151
});

0 commit comments

Comments
 (0)