diff --git a/package-lock.json b/package-lock.json index 50eaafa..bbb1c42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,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" } @@ -9016,7 +9017,6 @@ "url": "https://opencollective.com/fast-check" } ], - "license": "MIT", "dependencies": { "pure-rand": "^8.0.0" }, diff --git a/package.json b/package.json index 2ac1671..c5c1529 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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" } diff --git a/src/__mocks__/kubeObject.ts b/src/__mocks__/kubeObject.ts new file mode 100644 index 0000000..20591f9 --- /dev/null +++ b/src/__mocks__/kubeObject.ts @@ -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) }; + } + 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; + annotations?: Record; + ownerReferences?: Array<{ uid: string; kind: string; name: string }>; + [key: string]: any; + }; + [key: string]: any; +} diff --git a/vitest.config.mts b/vitest.config.mts new file mode 100644 index 0000000..9ec612d --- /dev/null +++ b/vitest.config.mts @@ -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' + ), + }, + }) +);