Skip to content

Commit 0ece4ea

Browse files
Resolve flaky tests (fetch) (#2158)
1 parent 997e9a9 commit 0ece4ea

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

.changeset/empty-ladybugs-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lg-tools/slackbot': patch
3+
---
4+
5+
Internal. Moves fetching logic into a separate `fetchChangelog` module
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fetch from 'node-fetch';
2+
3+
import { ComponentUpdateObject } from '../release-bot.types';
4+
5+
import { generateOutputStrings } from './generateOutputStrings';
6+
7+
export async function fetchChangelogText(
8+
component: ComponentUpdateObject,
9+
): Promise<string> {
10+
const { shortName } = generateOutputStrings(component);
11+
const rawChangelogUrl = `https://raw.githubusercontent.com/mongodb/leafygreen-ui/main/packages/${shortName}/CHANGELOG.md`;
12+
const response = await fetch(rawChangelogUrl);
13+
return await response.text();
14+
}
15+
16+
export async function fetchLatestChangelogText(
17+
component: ComponentUpdateObject,
18+
): Promise<string> {
19+
const fullChangelogText = await fetchChangelogText(component);
20+
const { version } = component;
21+
const startIndex = fullChangelogText.indexOf(`## ${version}`);
22+
const endIndex = fullChangelogText.indexOf(
23+
`\n## `,
24+
startIndex + version.length,
25+
);
26+
const latestChangelog = fullChangelogText.substring(startIndex, endIndex);
27+
return latestChangelog;
28+
}

tools/slackbot/src/release-bot/utils/getSortedUpdates.spec.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,72 @@
1+
import { fetchChangelogText, fetchLatestChangelogText } from './fetchChangelog';
12
import { getSortedUpdates } from './getSortedUpdates';
2-
import { TEST_DATA } from './test.data';
3+
import { TEST_CHANGELOG, TEST_DATA } from './test.data';
4+
5+
jest.mock('./fetchChangelog', () => {
6+
return {
7+
fetchChangelogText: jest.fn(),
8+
fetchLatestChangelogText: jest.fn(),
9+
};
10+
});
311

412
describe('tools/slackbot/release', () => {
13+
beforeEach(() => {
14+
(fetchChangelogText as jest.Mock).mockReturnValue(
15+
new Promise(r => r(TEST_CHANGELOG)),
16+
);
17+
(fetchLatestChangelogText as jest.Mock).mockReturnValue(
18+
new Promise(r => r(TEST_CHANGELOG)),
19+
);
20+
});
21+
522
describe('getSortedUpdates', () => {
623
test('sorts the updates array', async () => {
724
const sortedUpdates = await getSortedUpdates(TEST_DATA);
825
expect(sortedUpdates).toHaveProperty(
926
'major',
10-
expect.objectContaining([
11-
{ name: '@leafygreen-ui/example-major', version: '2.0.0' },
12-
{ name: '@lg-tools/tool-major', version: '2.0.0' },
27+
expect.arrayContaining([
28+
expect.objectContaining({
29+
name: '@leafygreen-ui/example-major',
30+
version: '2.0.0',
31+
}),
32+
expect.objectContaining({
33+
name: '@lg-tools/tool-major',
34+
version: '2.0.0',
35+
}),
1336
]),
1437
);
1538

1639
expect(sortedUpdates).toHaveProperty(
1740
'minor',
18-
expect.objectContaining([
19-
{ name: '@leafygreen-ui/example-minor', version: '1.2.0' },
20-
{ name: '@lg-tools/tool-minor', version: '1.2.0' },
41+
expect.arrayContaining([
42+
expect.objectContaining({
43+
name: '@leafygreen-ui/example-minor',
44+
version: '1.2.0',
45+
}),
46+
expect.objectContaining({
47+
name: '@lg-tools/tool-minor',
48+
version: '1.2.0',
49+
}),
2150
]),
2251
);
2352

2453
expect(sortedUpdates).toHaveProperty(
2554
'patch',
26-
expect.objectContaining([
27-
{ name: '@leafygreen-ui/example-patch', version: '1.0.2' },
28-
{ name: '@lg-tools/tool-patch', version: '1.0.2' },
55+
expect.arrayContaining([
56+
expect.objectContaining({
57+
name: '@leafygreen-ui/example-patch',
58+
version: '1.0.2',
59+
}),
60+
expect.objectContaining({
61+
name: '@lg-tools/tool-patch',
62+
version: '1.0.2',
63+
}),
2964
]),
3065
);
3166

3267
expect(sortedUpdates).toHaveProperty(
3368
'dependency',
34-
expect.objectContaining([]),
69+
expect.arrayContaining([]),
3570
);
3671
});
3772
});

tools/slackbot/src/release-bot/utils/parseChangeLog.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { uniq } from 'lodash';
2-
import fetch from 'node-fetch';
32

43
import { ComponentUpdateObject } from '../release-bot.types';
54

6-
import { generateOutputStrings } from './generateOutputStrings';
5+
import { fetchLatestChangelogText } from './fetchChangelog';
76

87
/**
98
* Given a component update object
@@ -22,29 +21,6 @@ export async function parseChangeLog(component: ComponentUpdateObject) {
2221
};
2322
}
2423

25-
async function fetchChangelogText(
26-
component: ComponentUpdateObject,
27-
): Promise<string> {
28-
const { shortName } = generateOutputStrings(component);
29-
const rawChangelogUrl = `https://raw.githubusercontent.com/mongodb/leafygreen-ui/main/packages/${shortName}/CHANGELOG.md`;
30-
const response = await fetch(rawChangelogUrl);
31-
return await response.text();
32-
}
33-
34-
async function fetchLatestChangelogText(
35-
component: ComponentUpdateObject,
36-
): Promise<string> {
37-
const fullChangelogText = await fetchChangelogText(component);
38-
const { version } = component;
39-
const startIndex = fullChangelogText.indexOf(`## ${version}`);
40-
const endIndex = fullChangelogText.indexOf(
41-
`\n## `,
42-
startIndex + version.length,
43-
);
44-
const latestChangelog = fullChangelogText.substring(startIndex, endIndex);
45-
return latestChangelog;
46-
}
47-
4824
const majorChangeIndex = (changelog: string) =>
4925
changelog.indexOf(`### Major Changes`) >= 0
5026
? changelog.indexOf(`### Major Changes`)

tools/slackbot/src/release-bot/utils/test.data.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ export const TEST_DATA = [
66
{ name: '@lg-tools/tool-minor', version: '1.2.0' },
77
{ name: '@lg-tools/tool-patch', version: '1.0.2' },
88
];
9+
10+
export const TEST_CHANGELOG = `
11+
# example-major
12+
13+
## 2.0.0
14+
15+
### Major Changes
16+
- Major update
17+
`;

0 commit comments

Comments
 (0)