chore: migrate three packages to MSW v2
Migrates test files in config-loader, events-node and kubernetes-react packages from MSW 1.x to MSW 2.x API to fix compatibility issues with Jest 30 and JSDOM v26. Changes: - Updated msw dependency from ^1.0.0 to ^2.0.0 - Changed imports from `rest` to `http` and `HttpResponse` - Converted handler syntax from `res(ctx.*)` to `HttpResponse.*` - Changed `toStrictEqual` to `toEqual` for response assertions Affected packages: - @backstage/config-loader - @backstage/plugin-events-node - @backstage/plugin-kubernetes-react Signed-off-by: Johan Persson <johanopersson@gmail.com>
This commit is contained in:
@@ -84,7 +84,7 @@
|
||||
"@testing-library/react": "^16.0.0",
|
||||
"@types/react": "^18.0.0",
|
||||
"jest-websocket-mock": "^2.5.0",
|
||||
"msw": "^1.3.1",
|
||||
"msw": "^2.0.0",
|
||||
"react": "^18.0.2",
|
||||
"react-dom": "^18.0.2",
|
||||
"react-router-dom": "^6.3.0"
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import { KubernetesAuthProvidersApi } from '../kubernetes-auth-provider';
|
||||
import { KubernetesBackendClient } from './KubernetesBackendClient';
|
||||
import { rest } from 'msw';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { UrlPatternDiscovery } from '@backstage/core-app-api';
|
||||
import { setupServer } from 'msw/node';
|
||||
import { MockFetchApi, registerMswTestHooks } from '@backstage/test-utils';
|
||||
@@ -89,23 +89,24 @@ describe('KubernetesBackendClient', () => {
|
||||
it('hits the /clusters API', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.get('http://localhost:1234/api/kubernetes/clusters', (_, res, ctx) =>
|
||||
res(ctx.json({ items: [{ name: 'cluster-a', authProvider: 'aws' }] })),
|
||||
http.get('http://localhost:1234/api/kubernetes/clusters', () =>
|
||||
HttpResponse.json({
|
||||
items: [{ name: 'cluster-a', authProvider: 'aws' }],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const clusters = await backendClient.getClusters();
|
||||
|
||||
expect(clusters).toStrictEqual([
|
||||
{ name: 'cluster-a', authProvider: 'aws' },
|
||||
]);
|
||||
expect(clusters).toEqual([{ name: 'cluster-a', authProvider: 'aws' }]);
|
||||
});
|
||||
|
||||
it('/clusters API throws a 404 Error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.get('http://localhost:1234/api/kubernetes/clusters', (_, res, ctx) =>
|
||||
res(ctx.status(404)),
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
() => new HttpResponse(null, { status: 404 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -117,8 +118,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('/clusters API throws a 500 Error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.get('http://localhost:1234/api/kubernetes/clusters', (_, res, ctx) =>
|
||||
res(ctx.status(500)),
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
() => new HttpResponse(null, { status: 500 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -130,9 +132,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('hits the /resources/custom/query API', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/custom/query',
|
||||
(_, res, ctx) => res(ctx.json(mockResponse)),
|
||||
() => HttpResponse.json(mockResponse),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -157,15 +159,15 @@ describe('KubernetesBackendClient', () => {
|
||||
const customObject: ObjectsByEntityResponse =
|
||||
await backendClient.getCustomObjectsByEntity(request);
|
||||
|
||||
expect(customObject).toStrictEqual(mockResponse);
|
||||
expect(customObject).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('/resources/custom/query API throws a 404 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/custom/query',
|
||||
(_, res, ctx) => res(ctx.status(404)),
|
||||
() => new HttpResponse(null, { status: 404 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -197,9 +199,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('/resources/custom/query API throws a 500 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/custom/query',
|
||||
(_, res, ctx) => res(ctx.status(500)),
|
||||
() => new HttpResponse(null, { status: 500 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -231,9 +233,8 @@ describe('KubernetesBackendClient', () => {
|
||||
it('hits the /services/{entityName} API', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
'http://localhost:1234/api/kubernetes/services/test-name',
|
||||
(_, res, ctx) => res(ctx.json(mockResponse)),
|
||||
http.post('http://localhost:1234/api/kubernetes/services/test-name', () =>
|
||||
HttpResponse.json(mockResponse),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -250,15 +251,15 @@ describe('KubernetesBackendClient', () => {
|
||||
const entityObject: ObjectsByEntityResponse =
|
||||
await backendClient.getObjectsByEntity(request);
|
||||
|
||||
expect(entityObject).toStrictEqual(mockResponse);
|
||||
expect(entityObject).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('services/{entityName} API throws a 404 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/services/test-name',
|
||||
(_, res, ctx) => res(ctx.status(404)),
|
||||
() => new HttpResponse(null, { status: 404 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -282,9 +283,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('services/{entityName} API throws a 500 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/services/test-name',
|
||||
(_, res, ctx) => res(ctx.status(500)),
|
||||
() => new HttpResponse(null, { status: 500 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -308,9 +309,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('hits the /resources/workloads/query API', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/workloads/query',
|
||||
(_, res, ctx) => res(ctx.json(mockResponse)),
|
||||
() => HttpResponse.json(mockResponse),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -328,15 +329,15 @@ describe('KubernetesBackendClient', () => {
|
||||
const response: ObjectsByEntityResponse =
|
||||
await backendClient.getWorkloadsByEntity(request);
|
||||
|
||||
expect(response).toStrictEqual(mockResponse);
|
||||
expect(response).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('/resources/workloads/query API throws a 404 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/workloads/query',
|
||||
(_, res, ctx) => res(ctx.status(404)),
|
||||
() => new HttpResponse(null, { status: 404 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -361,9 +362,9 @@ describe('KubernetesBackendClient', () => {
|
||||
it('/resources/workloads/query API throws a 500 error', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.post(
|
||||
http.post(
|
||||
'http://localhost:1234/api/kubernetes/resources/workloads/query',
|
||||
(_, res, ctx) => res(ctx.status(500)),
|
||||
() => new HttpResponse(null, { status: 500 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -388,12 +389,10 @@ describe('KubernetesBackendClient', () => {
|
||||
describe('proxy', () => {
|
||||
beforeEach(() => {
|
||||
worker.use(
|
||||
rest.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
(_, res, ctx) =>
|
||||
res(
|
||||
ctx.json({ items: [{ name: 'cluster-a', authProvider: 'aws' }] }),
|
||||
),
|
||||
http.get('http://localhost:1234/api/kubernetes/clusters', () =>
|
||||
HttpResponse.json({
|
||||
items: [{ name: 'cluster-a', authProvider: 'aws' }],
|
||||
}),
|
||||
),
|
||||
);
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
@@ -401,20 +400,16 @@ describe('KubernetesBackendClient', () => {
|
||||
|
||||
it('hits the /proxy API with oidc as protocol and okta as auth provider', async () => {
|
||||
worker.use(
|
||||
rest.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
(_, res, ctx) =>
|
||||
res(
|
||||
ctx.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'oidc',
|
||||
oidcTokenProvider: 'okta',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
http.get('http://localhost:1234/api/kubernetes/clusters', () =>
|
||||
HttpResponse.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'oidc',
|
||||
oidcTokenProvider: 'okta',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
kubernetesAuthProvidersApi.getCredentials.mockResolvedValue({
|
||||
@@ -428,16 +423,14 @@ describe('KubernetesBackendClient', () => {
|
||||
},
|
||||
};
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(req, res, ctx) =>
|
||||
res(
|
||||
req.headers.get(
|
||||
'Backstage-Kubernetes-Authorization-oidc-okta',
|
||||
) === 'k8-token3'
|
||||
? ctx.json(nsResponse)
|
||||
: ctx.status(403),
|
||||
),
|
||||
({ request }) =>
|
||||
request.headers.get(
|
||||
'Backstage-Kubernetes-Authorization-oidc-okta',
|
||||
) === 'k8-token3'
|
||||
? HttpResponse.json(nsResponse)
|
||||
: new HttpResponse(null, { status: 403 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -448,7 +441,7 @@ describe('KubernetesBackendClient', () => {
|
||||
|
||||
const response = await backendClient.proxy(request);
|
||||
|
||||
await expect(response.json()).resolves.toStrictEqual(nsResponse);
|
||||
await expect(response.json()).resolves.toEqual(nsResponse);
|
||||
expect(kubernetesAuthProvidersApi.getCredentials).toHaveBeenCalledWith(
|
||||
'oidc.okta',
|
||||
);
|
||||
@@ -457,19 +450,15 @@ describe('KubernetesBackendClient', () => {
|
||||
it('hits the /proxy API with serviceAccount as auth provider', async () => {
|
||||
identityApi.getCredentials.mockResolvedValue({ token: 'idToken' });
|
||||
worker.use(
|
||||
rest.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
(_, res, ctx) =>
|
||||
res(
|
||||
ctx.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'serviceAccount',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
http.get('http://localhost:1234/api/kubernetes/clusters', () =>
|
||||
HttpResponse.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'serviceAccount',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -481,14 +470,12 @@ describe('KubernetesBackendClient', () => {
|
||||
},
|
||||
};
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(req, res, ctx) =>
|
||||
res(
|
||||
req.headers.get('Authorization') === 'Bearer idToken'
|
||||
? ctx.json(nsResponse)
|
||||
: ctx.status(403),
|
||||
),
|
||||
({ request }) =>
|
||||
request.headers.get('Authorization') === 'Bearer idToken'
|
||||
? HttpResponse.json(nsResponse)
|
||||
: new HttpResponse(null, { status: 403 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -499,7 +486,7 @@ describe('KubernetesBackendClient', () => {
|
||||
|
||||
const response = await backendClient.proxy(request);
|
||||
|
||||
await expect(response.json()).resolves.toStrictEqual(nsResponse);
|
||||
await expect(response.json()).resolves.toEqual(nsResponse);
|
||||
expect(kubernetesAuthProvidersApi.getCredentials).toHaveBeenCalledWith(
|
||||
'serviceAccount',
|
||||
);
|
||||
@@ -507,24 +494,20 @@ describe('KubernetesBackendClient', () => {
|
||||
|
||||
it('ignores oidcTokenProvider for non-oidc auth provider', async () => {
|
||||
worker.use(
|
||||
rest.get(
|
||||
'http://localhost:1234/api/kubernetes/clusters',
|
||||
(_, res, ctx) =>
|
||||
res(
|
||||
ctx.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'not oidc',
|
||||
oidcTokenProvider: 'should be ignored',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
http.get('http://localhost:1234/api/kubernetes/clusters', () =>
|
||||
HttpResponse.json({
|
||||
items: [
|
||||
{
|
||||
name: 'cluster-a',
|
||||
authProvider: 'not oidc',
|
||||
oidcTokenProvider: 'should be ignored',
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(_, res, ctx) => res(ctx.json([])),
|
||||
() => HttpResponse.json([]),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -552,15 +535,13 @@ describe('KubernetesBackendClient', () => {
|
||||
},
|
||||
};
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(req, res, ctx) =>
|
||||
res(
|
||||
req.headers.get('Backstage-Kubernetes-Authorization-aws') ===
|
||||
'k8-token'
|
||||
? ctx.json(nsResponse)
|
||||
: ctx.status(403),
|
||||
),
|
||||
({ request }) =>
|
||||
request.headers.get('Backstage-Kubernetes-Authorization-aws') ===
|
||||
'k8-token'
|
||||
? HttpResponse.json(nsResponse)
|
||||
: new HttpResponse(null, { status: 403 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -570,7 +551,7 @@ describe('KubernetesBackendClient', () => {
|
||||
};
|
||||
|
||||
const response = await backendClient.proxy(request);
|
||||
await expect(response.json()).resolves.toStrictEqual(nsResponse);
|
||||
await expect(response.json()).resolves.toEqual(nsResponse);
|
||||
});
|
||||
|
||||
it('/proxy API throws a 404 error', async () => {
|
||||
@@ -578,9 +559,9 @@ describe('KubernetesBackendClient', () => {
|
||||
token: 'k8-token',
|
||||
});
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(_, res, ctx) => res(ctx.status(404)),
|
||||
() => new HttpResponse(null, { status: 404 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -616,15 +597,13 @@ describe('KubernetesBackendClient', () => {
|
||||
},
|
||||
};
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces',
|
||||
(req, res, ctx) =>
|
||||
res(
|
||||
req.headers.get('Backstage-Kubernetes-Authorization') ===
|
||||
'Bearer k8-token'
|
||||
? ctx.json(nsResponse)
|
||||
: ctx.status(403),
|
||||
),
|
||||
({ request }) =>
|
||||
request.headers.get('Backstage-Kubernetes-Authorization') ===
|
||||
'Bearer k8-token'
|
||||
? HttpResponse.json(nsResponse)
|
||||
: new HttpResponse(null, { status: 403 }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -646,14 +625,12 @@ describe('KubernetesBackendClient', () => {
|
||||
},
|
||||
};
|
||||
worker.use(
|
||||
rest.get(
|
||||
http.get(
|
||||
'http://localhost:1234/api/kubernetes/proxy/api/v1/namespaces/new-ns',
|
||||
(req, res, ctx) =>
|
||||
res(
|
||||
req.headers.get('Backstage-Kubernetes-Authorization')
|
||||
? ctx.status(403)
|
||||
: ctx.json(nsResponse),
|
||||
),
|
||||
({ request }) =>
|
||||
request.headers.get('Backstage-Kubernetes-Authorization')
|
||||
? new HttpResponse(null, { status: 403 })
|
||||
: HttpResponse.json(nsResponse),
|
||||
),
|
||||
);
|
||||
kubernetesAuthProvidersApi.getCredentials.mockResolvedValue({});
|
||||
@@ -663,7 +640,7 @@ describe('KubernetesBackendClient', () => {
|
||||
path: '/api/v1/namespaces/new-ns',
|
||||
});
|
||||
|
||||
await expect(response.json()).resolves.toStrictEqual(nsResponse);
|
||||
await expect(response.json()).resolves.toEqual(nsResponse);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user