Skip to content

Commit 0fda68d

Browse files
authored
Migrate Jest test suite to Jest 30 + jsdom 26 (#2475)
* test: migrate Jest test suite to Jest 30 + jsdom 26 Follows the core OpenSearch-Dashboards upgrade (opensearch-project/OpenSearch-Dashboards#12381). Full suite green under Jest 30.4.2 / jsdom 26.1.0. Signed-off-by: SuZhou-Joe <suzhou@amazon.com> * test: map jose to its CommonJS build in server jest config Under Jest 30's stricter package 'exports' resolution, 'import { SignJWT } from jose' in the jwt_auth / jwt_multiauth integration tests resolved to jose's pure-ESM browser build (dist/browser/index.js), failing with 'SyntaxError: Unexpected token export'. Pin '^jose$' to the CommonJS build (dist/node/cjs/index.js) the 'require' condition uses. The suites now load correctly (remaining failures require a live OpenSearch cluster). Signed-off-by: SuZhou-Joe <suzhou@amazon.com> * test: wrap jose moduleNameMapper line to satisfy prettier print-width The single-line '^jose$' mapping exceeded the 100-char print width; wrap the value onto its own line so prettier/prettier passes in CI (OSD-core prettier 3.5.x). Signed-off-by: SuZhou-Joe <suzhou@amazon.com> --------- Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
1 parent a3062d1 commit 0fda68d

87 files changed

Lines changed: 350 additions & 332 deletions

File tree

Some content is hidden

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

public/apps/account/account-app.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ export async function setupTopNavButton(coreStart: CoreStart, config: ClientConf
5656
if (accountInfo) {
5757
// Missing role error
5858
if (accountInfo.roles.length === 0 && !window.location.href.includes(CUSTOM_ERROR_PAGE_URI)) {
59-
window.location.href =
60-
coreStart.http.basePath.serverBasePath + CUSTOM_ERROR_PAGE_URI + ERROR_MISSING_ROLE_PATH;
59+
window.location.assign(
60+
coreStart.http.basePath.serverBasePath + CUSTOM_ERROR_PAGE_URI + ERROR_MISSING_ROLE_PATH
61+
);
6162
}
6263

6364
let tenant: string | undefined;

public/apps/account/account-nav-button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,5 @@ export function reloadAfterTenantSwitch(): void {
235235

236236
// rather than just reload when we switch tenants, we set the URL to the pathname. i.e. the portion like: '/app/dashboards'
237237
// therefore, the copied URL will now allow tenancy changes.
238-
window.location.href = window.location.pathname;
238+
window.location.assign(window.location.pathname);
239239
}

public/apps/account/password-reset-panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export function PasswordResetPanel(props: PasswordResetPanelProps) {
178178
<EuiSpacer />
179179

180180
{errorCallOut && (
181-
<EuiCallOut color="danger" iconType="alert">
181+
<EuiCallOut announceOnMount color="danger" iconType="alert">
182182
{errorCallOut}
183183
</EuiCallOut>
184184
)}

public/apps/account/tenant-switch-panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export function TenantSwitchPanel(props: TenantSwitchPanelProps) {
270270
<EuiSpacer />
271271

272272
{errorCallOut && (
273-
<EuiCallOut color="danger" iconType="alert">
273+
<EuiCallOut announceOnMount color="danger" iconType="alert">
274274
{errorCallOut}
275275
</EuiCallOut>
276276
)}

public/apps/account/test/__snapshots__/account-nav-button.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`Account navigation button renders 1`] = `
44
<EuiHeaderSectionItemButton

public/apps/account/test/__snapshots__/log-out-button.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`Account menu - Log out button renders renders when auth type is MultiAuth: basicauth 1`] = `
44
<div>

public/apps/account/test/__snapshots__/role-info-panel.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`Account menu - Role info panel renders 1`] = `
44
<EuiOverlayMask>

public/apps/account/test/__snapshots__/tenant-switch-panel.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`Account menu -tenant switch panel confirm button and renders renders when both global and private tenant enabled 1`] = `
44
<EuiOverlayMask>

public/apps/account/test/account-app.test.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,14 @@ describe('Account app', () => {
7878
});
7979

8080
it('Should skip if auto swich if securitytenant in url', (done) => {
81-
// Trick to mock window.location
82-
const originalLocation = window.location;
83-
delete window.location;
84-
window.location = new URL('http://www.example.com?securitytenant=abc') as any;
81+
// jest-location-mock: assign a URL with securitytenant instead of replacing window.location.
82+
window.location.assign('http://www.example.com?securitytenant=abc');
8583

8684
setupTopNavButton(mockCoreStart, mockConfig as any);
8785

8886
process.nextTick(() => {
89-
expect(setShouldShowTenantPopup).toBeCalledWith(false);
90-
window.location = originalLocation;
87+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(false);
88+
window.location.assign('http://localhost:5601/');
9189
done();
9290
});
9391
});
@@ -98,11 +96,11 @@ describe('Account app', () => {
9896
setupTopNavButton(mockCoreStart, mockConfig as any);
9997

10098
process.nextTick(() => {
101-
expect(getSavedTenant).toBeCalledTimes(1);
99+
expect(getSavedTenant).toHaveBeenCalledTimes(1);
102100
});
103101

104102
process.nextTick(() => {
105-
expect(setShouldShowTenantPopup).toBeCalledWith(false);
103+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(false);
106104
done();
107105
});
108106
});
@@ -113,8 +111,8 @@ describe('Account app', () => {
113111
setupTopNavButton(mockCoreStart, mockConfig as any);
114112

115113
process.nextTick(() => {
116-
expect(getSavedTenant).toBeCalledTimes(1);
117-
expect(setShouldShowTenantPopup).toBeCalledWith(true);
114+
expect(getSavedTenant).toHaveBeenCalledTimes(1);
115+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(true);
118116
done();
119117
});
120118
});
@@ -128,8 +126,8 @@ describe('Account app', () => {
128126
setupTopNavButton(mockCoreStart, mockConfig as any);
129127

130128
process.nextTick(() => {
131-
expect(getSavedTenant).toBeCalledTimes(0);
132-
expect(setShouldShowTenantPopup).toBeCalledWith(false);
129+
expect(getSavedTenant).toHaveBeenCalledTimes(0);
130+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(false);
133131
done();
134132
});
135133
});
@@ -144,8 +142,8 @@ describe('Account app', () => {
144142
setupTopNavButton(mockCoreStart, multiTenancyDisabledConfig as any);
145143

146144
process.nextTick(() => {
147-
expect(getSavedTenant).toBeCalledTimes(0);
148-
expect(setShouldShowTenantPopup).toBeCalledWith(false);
145+
expect(getSavedTenant).toHaveBeenCalledTimes(0);
146+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(false);
149147
done();
150148
});
151149
});
@@ -159,8 +157,8 @@ describe('Account app', () => {
159157
setupTopNavButton(mockCoreStart, mockConfig as any);
160158

161159
process.nextTick(() => {
162-
expect(getSavedTenant).toBeCalledTimes(0);
163-
expect(setShouldShowTenantPopup).toBeCalledWith(false);
160+
expect(getSavedTenant).toHaveBeenCalledTimes(0);
161+
expect(setShouldShowTenantPopup).toHaveBeenCalledWith(false);
164162
done();
165163
});
166164
});

public/apps/account/test/account-nav-button.test.tsx

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ describe('Account navigation button', () => {
109109
currAuthType={'dummy'}
110110
/>
111111
);
112-
expect(setState).toBeCalledTimes(1);
112+
expect(setState).toHaveBeenCalledTimes(1);
113113
});
114114

115115
it('should set modal when click on "View roles and identities" button', () => {
116116
component.find('[data-test-subj="view-roles-and-identities"]').simulate('click');
117-
expect(setState).toBeCalledTimes(1);
117+
expect(setState).toHaveBeenCalledTimes(1);
118118
});
119119

120120
it('should set modal when click on "Switch tenants" button', () => {
121121
component.find('[data-test-subj="switch-tenants"]').simulate('click');
122-
expect(setState).toBeCalledTimes(1);
122+
expect(setState).toHaveBeenCalledTimes(1);
123123
});
124124

125125
it('should set modal when click on "Reset password" button', () => {
126126
component.find('[data-test-subj="reset-password"]').simulate('click');
127-
expect(setState).toBeCalledTimes(1);
127+
expect(setState).toHaveBeenCalledTimes(1);
128128
});
129129

130130
it('should set isPopoverOpen to true when click on Avatar in header section', () => {
131131
component.find('[data-test-subj="account-popover"]').simulate('click');
132-
expect(setState).toBeCalledTimes(1);
132+
expect(setState).toHaveBeenCalledTimes(1);
133133
});
134134
});
135135

@@ -172,7 +172,7 @@ describe('Account navigation button, multitenancy disabled', () => {
172172
currAuthType={'dummy'}
173173
/>
174174
);
175-
expect(setState).toBeCalledTimes(0);
175+
expect(setState).toHaveBeenCalledTimes(0);
176176
});
177177
});
178178

@@ -247,34 +247,12 @@ describe('Shows tenant info when multitenancy enabled, and hides it if disabled'
247247
});
248248

249249
describe('Reload window after tenant switch', () => {
250-
const originalLocation = window.location;
251-
const mockSetWindowHref = jest.fn();
252-
let pathname: string = '';
253-
beforeAll(() => {
254-
pathname = '/app/myapp';
255-
Object.defineProperty(window, 'location', {
256-
value: {
257-
get pathname() {
258-
return pathname;
259-
},
260-
get href() {
261-
return '/app/dashboards?security_tenant=admin_tenant';
262-
},
263-
set href(value: string) {
264-
mockSetWindowHref(value);
265-
},
266-
},
267-
});
268-
});
269-
270-
afterAll(() => {
271-
window.location = originalLocation;
272-
});
273-
274250
it('should remove the tenant query parameter before reloading', () => {
275-
pathname = '/app/pathname-only';
251+
// jest-location-mock: reloadAfterTenantSwitch() calls window.location.assign(pathname).
252+
window.location.assign('http://localhost:5601/app/pathname-only?security_tenant=admin_tenant');
253+
const assignSpy = jest.spyOn(window.location, 'assign');
276254
reloadAfterTenantSwitch();
277-
expect(mockSetWindowHref).toHaveBeenCalledWith(pathname);
255+
expect(assignSpy).toHaveBeenCalledWith('/app/pathname-only');
278256
});
279257
});
280258

0 commit comments

Comments
 (0)