Skip to content

Commit ed1f603

Browse files
fix: incorporate Jest 29 compatibility and API updates from PR #2763
- Add jest.setup.js with @actions/core mock for test isolation - Add setupFiles config to jest.config.js - Fix cockatiel retry API order (.exponential().attempts() instead of .attempts().exponential()) - Fix github-api-helpers.ts response handling (check resp.ok before parsing JSON) - Add .sort() to glob results in Alert.ts for deterministic ordering - Update Jest assertions for v29 compatibility: - toBeCalled -> toHaveBeenCalled - toBeCalledTimes -> toHaveBeenCalledTimes - Update fs mock in validate_images.test.js to use jest.requireActual Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5b6df21 commit ed1f603

File tree

9 files changed

+27
-14
lines changed

9 files changed

+27
-14
lines changed

utils/__tests__/set-dashboard-required-datasources.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ describe('set-dashboards-required-datasources', () => {
127127
`Multiple Quickstart data sources detected for Quickstart: Template Quickstart, Dashboards must be updated manually`
128128
);
129129

130-
expect(Dashboard.submitSetRequiredDataSourcesMutation).not.toBeCalled();
130+
expect(Dashboard.submitSetRequiredDataSourcesMutation).not.toHaveBeenCalled();
131131
});
132132
});

utils/__tests__/validate_icon.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('validate Icon tests', () => {
137137

138138
handleErrors(['error1', 'error2', 'error3', 'error4']);
139139

140-
expect(logMock).toBeCalledTimes(4);
140+
expect(logMock).toHaveBeenCalledTimes(4);
141141
});
142142
});
143143
});

utils/__tests__/validate_images.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import * as fs from 'fs';
1010
import * as glob from 'glob';
1111
import * as path from 'path';
1212

13-
jest.mock('@actions/core');
1413
jest.mock('fs', () => ({
14+
...jest.requireActual('fs'),
1515
promises: {
1616
access: jest.fn(),
1717
},

utils/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ module.exports = {
55
testRegex: '(/__tests__/.*\\.(test|spec))\\.(ts|js)$',
66
moduleFileExtensions: ['js', 'ts', 'json'],
77
transformIgnorePatterns: ['.json'],
8+
setupFiles: ['<rootDir>/jest.setup.js'],
89
};

utils/jest.setup.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest setup file for mocking external dependencies
2+
3+
// Mock @actions/core to prevent GitHub Actions side effects during tests
4+
jest.mock('@actions/core', () => ({
5+
setFailed: jest.fn(),
6+
warning: jest.fn(),
7+
info: jest.fn(),
8+
debug: jest.fn(),
9+
error: jest.fn(),
10+
getInput: jest.fn(),
11+
setOutput: jest.fn(),
12+
}));

utils/lib/Alert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ class Alert extends Component<
342342
static getAll() {
343343
const alertPaths = glob.sync(
344344
path.join(__dirname, '..', '..', 'alert-policies', '**', '*.+(yml|yaml)')
345-
);
345+
).sort();
346346
return alertPaths.map((alertPath) => {
347347
// The identifier for alerts is the folder and the file name
348348
// e.g. `node-js/HighCpuUtilization.yml`

utils/lib/__tests__/Dashboard.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Dashboard', () => {
5454
);
5555
expect(dash.isValid).toBe(false);
5656
expect(dash.config).not.toBeDefined();
57-
expect(console.error).toBeCalled();
57+
expect(console.error).toHaveBeenCalled();
5858
});
5959
});
6060

@@ -73,15 +73,15 @@ describe('Dashboard', () => {
7373
);
7474
expect(dash.configPath).toEqual('');
7575
expect(dash.isValid).toBe(false);
76-
expect(console.error).toBeCalled();
76+
expect(console.error).toHaveBeenCalled();
7777
});
7878

7979
test('Fails to create valid config path, basePath is invalid', () => {
8080
jest.spyOn(global.console, 'error').mockImplementation(() => {});
8181
const dash = new Dashboard('mock-dashboard-2', __dirname);
8282
expect(dash.configPath).toEqual('');
8383
expect(dash.isValid).toBe(false);
84-
expect(console.error).toBeCalled();
84+
expect(console.error).toHaveBeenCalled();
8585
});
8686
});
8787

utils/lib/github-api-helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ export const fetchPaginatedGHResults = async (
6666
const resp = await fetch(nextPageLink, {
6767
headers,
6868
});
69-
// TODO: this should happen after the resp.ok check
70-
const responseJson = await resp.json();
7169

7270
if (!resp.ok) {
7371
logger.error(`Error from Github API`, {
7472
url: nextPageLink,
7573
status: resp.statusText,
7674
code: resp.status,
7775
});
78-
throw new Error(`Github API returned: ${responseJson.message}`);
76+
const errorJson = await resp.json();
77+
throw new Error(`Github API returned: ${errorJson.message}`);
7978
}
79+
const responseJson = await resp.json();
8080
nextPageLink = getNextLink(resp.headers.get('Link'));
8181
files = [...files, ...responseJson];
8282
}

utils/lib/nr-graphql-helpers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ export const fetchNRGraphqlResults = async <Variables, ResponseData>(
5252
// To help us ensure that the request hits and is processed by nerdgraph
5353
// This will try the request 3 times, waiting a little longer between each attempt
5454
// It will retry on status codes 400+, 2** would be success and we wouldn't want to retry for a 3**
55-
const retry = Policy.handleResultType(
55+
const retryPolicy = Policy.handleResultType(
5656
Response,
5757
(response) => response.status >= 400
5858
)
5959
.retry()
60-
.attempts(3)
61-
.exponential();
60+
.exponential()
61+
.attempts(3);
6262

6363
try {
6464
const body = buildRequestBody<Variables>(queryBody);
6565

6666
logger.debug(`Running NerdGraph request...`, { url: NR_API_URL, body });
6767

68-
const res = await retry.execute(() =>
68+
const res = await retryPolicy.execute(() =>
6969
fetch(NR_API_URL, {
7070
method: 'POST',
7171
body,

0 commit comments

Comments
 (0)