diff --git a/Makefile b/Makefile index ea7c84d018..658be09690 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ +CMD_ARGS = $(filter-out $@,$(MAKECMDGOALS)) + +%: + @: + EXTENSIONS_DIR = ./extensions extensions = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir}) extension_node_modules = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir}/node_modules) @@ -16,16 +21,16 @@ node_modules: yarn.lock yarn install --frozen-lockfile yarn check --verify-tree --integrity -static/build/LensDev.html: +static/build/LensDev.html: node_modules yarn compile:renderer .PHONY: compile-dev -compile-dev: +compile-dev: node_modules yarn compile:main --cache yarn compile:renderer --cache .PHONY: dev -dev: node_modules binaries/client build-extensions static/build/LensDev.html +dev: binaries/client build-extensions static/build/LensDev.html yarn dev .PHONY: lint @@ -34,7 +39,7 @@ lint: .PHONY: test test: binaries/client - yarn test + yarn run jest $(or $(CMD_ARGS), "src") .PHONY: integration-linux integration-linux: binaries/client build-extension-types build-extensions @@ -58,10 +63,6 @@ integration-win: binaries/client build-extension-types build-extensions yarn build:win yarn integration -.PHONY: test-app -test-app: - yarn test - .PHONY: build build: node_modules binaries/client build-extensions ifeq "$(DETECTED_OS)" "Windows" @@ -77,7 +78,7 @@ $(extension_dists): src/extensions/npm/extensions/dist cd $(@:/dist=) && npm run build .PHONY: build-extensions -build-extensions: $(extension_node_modules) $(extension_dists) +build-extensions: node_modules $(extension_node_modules) $(extension_dists) .PHONY: test-extensions test-extensions: $(extension_node_modules) diff --git a/integration/__tests__/app.tests.ts b/integration/__tests__/app.tests.ts index af029a23e9..e744a24c74 100644 --- a/integration/__tests__/app.tests.ts +++ b/integration/__tests__/app.tests.ts @@ -1,3 +1,13 @@ +/** + * @jest-environment node + */ + +/* + Cluster tests are run if there is a pre-existing minikube cluster. Before running cluster tests the TEST_NAMESPACE + namespace is removed, if it exists, from the minikube cluster. Resources are created as part of the cluster tests in the + TEST_NAMESPACE namespace. This is done to minimize destructive impact of the cluster tests on an existing minikube + cluster and vice versa. +*/ import { Application } from "spectron"; import * as utils from "../helpers/utils"; import { listHelmRepositories } from "../helpers/utils"; diff --git a/package.json b/package.json index da4956088c..0f43a5310e 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "build:linux": "yarn run compile && electron-builder --linux --dir -c.productName=Lens", "build:mac": "yarn run compile && electron-builder --mac --dir -c.productName=Lens", "build:win": "yarn run compile && electron-builder --win --dir -c.productName=Lens", - "test": "scripts/test.sh", "integration": "jest --runInBand integration", "dist": "yarn run compile && electron-builder --publish onTag", "dist:win": "yarn run compile && electron-builder --publish onTag --x64 --ia32", @@ -36,7 +35,7 @@ "download:kubectl": "yarn run ts-node build/download_kubectl.ts", "download:helm": "yarn run ts-node build/download_helm.ts", "build:tray-icons": "yarn run ts-node build/build_tray_icon.ts", - "lint": "yarn run eslint $@ --ext js,ts,tsx --max-warnings=0 .", + "lint": "yarn run eslint --ext js,ts,tsx --max-warnings=0 .", "lint:fix": "yarn run lint --fix", "mkdocs-serve-local": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -it -p 8000:8000 -v ${PWD}:/docs mkdocs-serve-local:latest", "verify-docs": "docker build -t mkdocs-serve-local:latest mkdocs/ && docker run --rm -v ${PWD}:/docs mkdocs-serve-local:latest build --strict", @@ -52,7 +51,6 @@ "jest": { "collectCoverage": false, "verbose": true, - "testEnvironment": "node", "transform": { "^.+\\.tsx?$": "ts-jest" }, diff --git a/scripts/test.sh b/scripts/test.sh deleted file mode 100755 index 19c1f71c47..0000000000 --- a/scripts/test.sh +++ /dev/null @@ -1 +0,0 @@ -jest --env=jsdom ${1:-src} diff --git a/src/common/__tests__/cluster-store.test.ts b/src/common/__tests__/cluster-store.test.ts index 1183454d7b..e9ee4ee486 100644 --- a/src/common/__tests__/cluster-store.test.ts +++ b/src/common/__tests__/cluster-store.test.ts @@ -35,7 +35,8 @@ jest.mock("electron", () => { app: { getVersion: () => "99.99.99", getPath: () => "tmp", - getLocale: () => "en" + getLocale: () => "en", + setLoginItemSettings: jest.fn(), }, ipcMain: { handle: jest.fn(), diff --git a/src/common/__tests__/search-store.test.ts b/src/common/__tests__/search-store.test.ts index 27d52971de..6193863192 100644 --- a/src/common/__tests__/search-store.test.ts +++ b/src/common/__tests__/search-store.test.ts @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - import { SearchStore } from "../search-store"; let searchStore: SearchStore = null; diff --git a/src/common/__tests__/user-store.test.ts b/src/common/__tests__/user-store.test.ts index b74941a790..1715a1a593 100644 --- a/src/common/__tests__/user-store.test.ts +++ b/src/common/__tests__/user-store.test.ts @@ -1,3 +1,7 @@ +import { Console } from "console"; + +console = new Console(process.stdout, process.stderr); + import mockFs from "mock-fs"; jest.mock("electron", () => { @@ -5,7 +9,8 @@ jest.mock("electron", () => { app: { getVersion: () => "99.99.99", getPath: () => "tmp", - getLocale: () => "en" + getLocale: () => "en", + setLoginItemSettings: jest.fn(), } }; }); diff --git a/src/common/__tests__/workspace-store.test.ts b/src/common/__tests__/workspace-store.test.ts index 355eb8b2ce..ae9538ead3 100644 --- a/src/common/__tests__/workspace-store.test.ts +++ b/src/common/__tests__/workspace-store.test.ts @@ -5,7 +5,8 @@ jest.mock("electron", () => { app: { getVersion: () => "99.99.99", getPath: () => "tmp", - getLocale: () => "en" + getLocale: () => "en", + setLoginItemSettings: jest.fn(), }, ipcMain: { handle: jest.fn(), diff --git a/src/common/base-store.ts b/src/common/base-store.ts index f7ad946bfd..91d3ef8312 100644 --- a/src/common/base-store.ts +++ b/src/common/base-store.ts @@ -19,7 +19,7 @@ export interface BaseStoreParams extends ConfOptions { * Note: T should only contain base JSON serializable types. */ export abstract class BaseStore extends Singleton { - protected storeConfig: Config; + protected storeConfig?: Config; protected syncDisposers: Function[] = []; whenLoaded = when(() => this.isLoaded); @@ -36,7 +36,7 @@ export abstract class BaseStore extends Singleton { } get name() { - return path.basename(this.storeConfig.path); + return path.basename(this.path); } protected get syncRendererChannel() { @@ -48,7 +48,7 @@ export abstract class BaseStore extends Singleton { } get path() { - return this.storeConfig.path; + return this.storeConfig?.path || ""; } protected async init() { @@ -82,10 +82,13 @@ export abstract class BaseStore extends Singleton { protected async saveToFile(model: T) { logger.info(`[STORE]: SAVING ${this.path}`); + // todo: update when fixed https://github.com/sindresorhus/conf/issues/114 - Object.entries(model).forEach(([key, value]) => { - this.storeConfig.set(key, value); - }); + if (this.storeConfig) { + for (const [key, value] of Object.entries(model)) { + this.storeConfig.set(key, value); + } + } } enableSync() { diff --git a/src/jest.setup.ts b/src/jest.setup.ts index ef6565c907..d8c6ce9161 100644 --- a/src/jest.setup.ts +++ b/src/jest.setup.ts @@ -4,3 +4,7 @@ fetchMock.enableMocks(); // Mock __non_webpack_require__ for tests globalThis.__non_webpack_require__ = jest.fn(); + +process.on("unhandledRejection", (err) => { + fail(err); +}); diff --git a/src/renderer/api/__tests__/kube-api-parse.test.ts b/src/renderer/api/__tests__/kube-api-parse.test.ts index c2aec7fd58..bc4528ad4e 100644 --- a/src/renderer/api/__tests__/kube-api-parse.test.ts +++ b/src/renderer/api/__tests__/kube-api-parse.test.ts @@ -1,134 +1,115 @@ +jest.mock("../kube-object"); +jest.mock("../kube-api"); +jest.mock("../api-manager", () => ({ + apiManager() { + return { + registerStore: jest.fn(), + }; + } +})); + import { IKubeApiParsed, parseKubeApi } from "../kube-api-parse"; -interface KubeApiParseTestData { - url: string; - expected: Required; -} +/** + * [, ] + */ +type KubeApiParseTestData = [string, Required]; const tests: KubeApiParseTestData[] = [ - { - url: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/prometheuses.monitoring.coreos.com", - expected: { - apiBase: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions", - apiPrefix: "/apis", - apiGroup: "apiextensions.k8s.io", - apiVersion: "v1beta1", - apiVersionWithGroup: "apiextensions.k8s.io/v1beta1", - namespace: undefined, - resource: "customresourcedefinitions", - name: "prometheuses.monitoring.coreos.com" - }, - }, - { - url: "/api/v1/namespaces/kube-system/pods/coredns-6955765f44-v8p27", - expected: { - apiBase: "/api/v1/pods", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "v1", - apiVersionWithGroup: "v1", - namespace: "kube-system", - resource: "pods", - name: "coredns-6955765f44-v8p27" - }, - }, - { - url: "/apis/stable.example.com/foo1/crontabs", - expected: { - apiBase: "/apis/stable.example.com/foo1/crontabs", - apiPrefix: "/apis", - apiGroup: "stable.example.com", - apiVersion: "foo1", - apiVersionWithGroup: "stable.example.com/foo1", - resource: "crontabs", - name: undefined, - namespace: undefined, - }, - }, - { - url: "/apis/cluster.k8s.io/v1alpha1/clusters", - expected: { - apiBase: "/apis/cluster.k8s.io/v1alpha1/clusters", - apiPrefix: "/apis", - apiGroup: "cluster.k8s.io", - apiVersion: "v1alpha1", - apiVersionWithGroup: "cluster.k8s.io/v1alpha1", - resource: "clusters", - name: undefined, - namespace: undefined, - }, - }, - { - url: "/api/v1/namespaces", - expected: { - apiBase: "/api/v1/namespaces", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "v1", - apiVersionWithGroup: "v1", - resource: "namespaces", - name: undefined, - namespace: undefined, - }, - }, - { - url: "/api/v1/secrets", - expected: { - apiBase: "/api/v1/secrets", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "v1", - apiVersionWithGroup: "v1", - resource: "secrets", - name: undefined, - namespace: undefined, - }, - }, - { - url: "/api/v1/nodes/minikube", - expected: { - apiBase: "/api/v1/nodes", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "v1", - apiVersionWithGroup: "v1", - resource: "nodes", - name: "minikube", - namespace: undefined, - }, - }, - { - url: "/api/foo-bar/nodes/minikube", - expected: { - apiBase: "/api/foo-bar/nodes", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "foo-bar", - apiVersionWithGroup: "foo-bar", - resource: "nodes", - name: "minikube", - namespace: undefined, - }, - }, - { - url: "/api/v1/namespaces/kube-public", - expected: { - apiBase: "/api/v1/namespaces", - apiPrefix: "/api", - apiGroup: "", - apiVersion: "v1", - apiVersionWithGroup: "v1", - resource: "namespaces", - name: "kube-public", - namespace: undefined, - }, - }, + ["/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/prometheuses.monitoring.coreos.com", { + apiBase: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions", + apiPrefix: "/apis", + apiGroup: "apiextensions.k8s.io", + apiVersion: "v1beta1", + apiVersionWithGroup: "apiextensions.k8s.io/v1beta1", + namespace: undefined, + resource: "customresourcedefinitions", + name: "prometheuses.monitoring.coreos.com" + }], + ["/api/v1/namespaces/kube-system/pods/coredns-6955765f44-v8p27", { + apiBase: "/api/v1/pods", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "v1", + apiVersionWithGroup: "v1", + namespace: "kube-system", + resource: "pods", + name: "coredns-6955765f44-v8p27" + }], + ["/apis/stable.example.com/foo1/crontabs", { + apiBase: "/apis/stable.example.com/foo1/crontabs", + apiPrefix: "/apis", + apiGroup: "stable.example.com", + apiVersion: "foo1", + apiVersionWithGroup: "stable.example.com/foo1", + resource: "crontabs", + name: undefined, + namespace: undefined, + }], + ["/apis/cluster.k8s.io/v1alpha1/clusters", { + apiBase: "/apis/cluster.k8s.io/v1alpha1/clusters", + apiPrefix: "/apis", + apiGroup: "cluster.k8s.io", + apiVersion: "v1alpha1", + apiVersionWithGroup: "cluster.k8s.io/v1alpha1", + resource: "clusters", + name: undefined, + namespace: undefined, + }], + ["/api/v1/namespaces", { + apiBase: "/api/v1/namespaces", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "v1", + apiVersionWithGroup: "v1", + resource: "namespaces", + name: undefined, + namespace: undefined, + }], + ["/api/v1/secrets", { + apiBase: "/api/v1/secrets", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "v1", + apiVersionWithGroup: "v1", + resource: "secrets", + name: undefined, + namespace: undefined, + }], + ["/api/v1/nodes/minikube", { + apiBase: "/api/v1/nodes", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "v1", + apiVersionWithGroup: "v1", + resource: "nodes", + name: "minikube", + namespace: undefined, + }], + ["/api/foo-bar/nodes/minikube", { + apiBase: "/api/foo-bar/nodes", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "foo-bar", + apiVersionWithGroup: "foo-bar", + resource: "nodes", + name: "minikube", + namespace: undefined, + }], + ["/api/v1/namespaces/kube-public", { + apiBase: "/api/v1/namespaces", + apiPrefix: "/api", + apiGroup: "", + apiVersion: "v1", + apiVersionWithGroup: "v1", + resource: "namespaces", + name: "kube-public", + namespace: undefined, + }], ]; describe("parseApi unit tests", () => { - for (const { url, expected } of tests) { - test(`testing "${url}"`, () => { - expect(parseKubeApi(url)).toStrictEqual(expected); - }); - } + it.each(tests)("testing %s", (url, expected) => { + expect(parseKubeApi(url)).toStrictEqual(expected); + }); }); diff --git a/src/renderer/api/__tests__/kube-api.test.ts b/src/renderer/api/__tests__/kube-api.test.ts index 7481bd096a..9d3c41869d 100644 --- a/src/renderer/api/__tests__/kube-api.test.ts +++ b/src/renderer/api/__tests__/kube-api.test.ts @@ -28,7 +28,7 @@ describe("KubeApi", () => { }; } }); - + const apiBase = "/apis/networking.k8s.io/v1/ingresses"; const fallbackApiBase = "/apis/extensions/v1beta1/ingresses"; const kubeApi = new KubeApi({ @@ -36,7 +36,7 @@ describe("KubeApi", () => { fallbackApiBases: [fallbackApiBase], checkPreferredVersion: true, }); - + await kubeApi.get(); expect(kubeApi.apiPrefix).toEqual("/apis"); expect(kubeApi.apiGroup).toEqual("networking.k8s.io"); diff --git a/src/renderer/components/+workloads-pods/__tests__/pod-tolerations.test.tsx b/src/renderer/components/+workloads-pods/__tests__/pod-tolerations.test.tsx index dbde813e5a..8071254940 100644 --- a/src/renderer/components/+workloads-pods/__tests__/pod-tolerations.test.tsx +++ b/src/renderer/components/+workloads-pods/__tests__/pod-tolerations.test.tsx @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - import React from "react"; import "@testing-library/jest-dom/extend-expect"; import { fireEvent, render } from "@testing-library/react"; diff --git a/src/renderer/components/dock/__test__/log-resource-selector.test.tsx b/src/renderer/components/dock/__test__/log-resource-selector.test.tsx index 22d97b7216..d373d0fbab 100644 --- a/src/renderer/components/dock/__test__/log-resource-selector.test.tsx +++ b/src/renderer/components/dock/__test__/log-resource-selector.test.tsx @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - import React from "react"; import "@testing-library/jest-dom/extend-expect"; import { render } from "@testing-library/react"; diff --git a/src/renderer/components/dock/__test__/log-tab.store.test.ts b/src/renderer/components/dock/__test__/log-tab.store.test.ts index 79b93af623..e99022070b 100644 --- a/src/renderer/components/dock/__test__/log-tab.store.test.ts +++ b/src/renderer/components/dock/__test__/log-tab.store.test.ts @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - import { podsStore } from "../../+workloads-pods/pods.store"; import { Pod } from "../../../api/endpoints"; import { dockStore } from "../dock.store";