Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"tsc": "headlamp-plugin tsc",
"storybook": "headlamp-plugin storybook",
"storybook-build": "headlamp-plugin storybook-build",
"test": "headlamp-plugin test",
"test": "vitest run -c vitest.config.mts",
"i18n": "headlamp-plugin i18n"
},
"keywords": [
Expand Down Expand Up @@ -43,6 +43,7 @@
"@kinvolk/headlamp-plugin": "^0.14.0",
"@testing-library/dom": "^10.4.1",
"@testing-library/react": "^16.3.2",
"fast-check": "^4.8.0",
"vite": "^6.4.3",
"vite-plugin-svgr": "^4.5.0"
}
Expand Down
70 changes: 70 additions & 0 deletions src/__mocks__/kubeObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Contributors to Agones a Series of LF Projects, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Minimal stand-in for `KubeObject` from `@kinvolk/headlamp-plugin/lib/k8s/cluster`.
*
* Headlamp externalises this import at build time (it is provided by the
* host app at runtime), so the physical path does not exist inside
* `node_modules`. During Vitest runs we redirect the import here via a
* `resolve.alias` in `vitest.config.mts`.
*
* Only the constructor, `metadata`, and `jsonData` are needed for unit and
* property tests — methods like `patch`, `delete`, and `useList` are stubs.
*/
export class KubeObject {
jsonData: any;

constructor(json: any) {
this.jsonData = json;
}

get metadata() {
return this.jsonData.metadata;
}

static useList() {
return [null];
}

static apiVersion = '';
static kind = '';
static apiName = '';
static isNamespaced = true;

async patch(body: unknown) {
if (body && typeof body === 'object') {
this.jsonData = { ...this.jsonData, ...(body as Record<string, unknown>) };
}
return this.jsonData;
}
}

// Re-export the interface (type-only) so that `import { KubeObjectInterface }` works.
export interface KubeObjectInterface {
apiVersion: string;
kind: string;
metadata: {
name: string;
namespace?: string;
uid?: string;
labels?: Record<string, string>;
annotations?: Record<string, string>;
ownerReferences?: Array<{ uid: string; kind: string; name: string }>;
[key: string]: any;
};
[key: string]: any;
}
50 changes: 50 additions & 0 deletions vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Contributors to Agones a Series of LF Projects, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { resolve } from 'path';
import { defineConfig, mergeConfig } from 'vitest/config';
import baseConfig from '@kinvolk/headlamp-plugin/config/vite.config.mjs';

/**
* Extend the upstream Headlamp Vitest/Vite config with resolve aliases for
* paths that are externalised at build time but need stubs during testing.
*
* `@kinvolk/headlamp-plugin/lib/k8s/cluster` is the primary case — it is
* provided by the host app at runtime but does not physically exist in
* `node_modules`. We redirect it to a minimal {@link src/__mocks__/kubeObject.ts}
* so that resource-model tests can instantiate `KubeObject` subclasses.
*/
export default mergeConfig(
baseConfig,
defineConfig({
resolve: {
alias: {
'@kinvolk/headlamp-plugin/lib/k8s/cluster': resolve(
__dirname,
'src/__mocks__/kubeObject.ts'
),
},
},
test: {
// Override setupFiles because the base config uses `import.meta.dirname`
// which resolves to undefined when loaded from an external config file.
setupFiles: resolve(
__dirname,
'node_modules/@kinvolk/headlamp-plugin/config/setupTests.js'
),
},
})
);