1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix unit tests and make tests fail if a promise rejects after the test harness is finishes (#2034)

This commit is contained in:
Sebastian Malton 2021-03-30 10:14:05 -04:00 committed by GitHub
parent a4ba77c58b
commit 503a49261e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 153 additions and 166 deletions

View File

@ -1,3 +1,8 @@
CMD_ARGS = $(filter-out $@,$(MAKECMDGOALS))
%:
@:
EXTENSIONS_DIR = ./extensions EXTENSIONS_DIR = ./extensions
extensions = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir}) extensions = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir})
extension_node_modules = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir}/node_modules) extension_node_modules = $(foreach dir, $(wildcard $(EXTENSIONS_DIR)/*), ${dir}/node_modules)
@ -16,16 +21,16 @@ node_modules: yarn.lock
yarn install --frozen-lockfile yarn install --frozen-lockfile
yarn check --verify-tree --integrity yarn check --verify-tree --integrity
static/build/LensDev.html: static/build/LensDev.html: node_modules
yarn compile:renderer yarn compile:renderer
.PHONY: compile-dev .PHONY: compile-dev
compile-dev: compile-dev: node_modules
yarn compile:main --cache yarn compile:main --cache
yarn compile:renderer --cache yarn compile:renderer --cache
.PHONY: dev .PHONY: dev
dev: node_modules binaries/client build-extensions static/build/LensDev.html dev: binaries/client build-extensions static/build/LensDev.html
yarn dev yarn dev
.PHONY: lint .PHONY: lint
@ -34,7 +39,7 @@ lint:
.PHONY: test .PHONY: test
test: binaries/client test: binaries/client
yarn test yarn run jest $(or $(CMD_ARGS), "src")
.PHONY: integration-linux .PHONY: integration-linux
integration-linux: binaries/client build-extension-types build-extensions 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 build:win
yarn integration yarn integration
.PHONY: test-app
test-app:
yarn test
.PHONY: build .PHONY: build
build: node_modules binaries/client build-extensions build: node_modules binaries/client build-extensions
ifeq "$(DETECTED_OS)" "Windows" ifeq "$(DETECTED_OS)" "Windows"
@ -77,7 +78,7 @@ $(extension_dists): src/extensions/npm/extensions/dist
cd $(@:/dist=) && npm run build cd $(@:/dist=) && npm run build
.PHONY: build-extensions .PHONY: build-extensions
build-extensions: $(extension_node_modules) $(extension_dists) build-extensions: node_modules $(extension_node_modules) $(extension_dists)
.PHONY: test-extensions .PHONY: test-extensions
test-extensions: $(extension_node_modules) test-extensions: $(extension_node_modules)

View File

@ -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 { Application } from "spectron";
import * as utils from "../helpers/utils"; import * as utils from "../helpers/utils";
import { listHelmRepositories } from "../helpers/utils"; import { listHelmRepositories } from "../helpers/utils";

View File

@ -26,7 +26,6 @@
"build:linux": "yarn run compile && electron-builder --linux --dir -c.productName=Lens", "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:mac": "yarn run compile && electron-builder --mac --dir -c.productName=Lens",
"build:win": "yarn run compile && electron-builder --win --dir -c.productName=Lens", "build:win": "yarn run compile && electron-builder --win --dir -c.productName=Lens",
"test": "scripts/test.sh",
"integration": "jest --runInBand integration", "integration": "jest --runInBand integration",
"dist": "yarn run compile && electron-builder --publish onTag", "dist": "yarn run compile && electron-builder --publish onTag",
"dist:win": "yarn run compile && electron-builder --publish onTag --x64 --ia32", "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:kubectl": "yarn run ts-node build/download_kubectl.ts",
"download:helm": "yarn run ts-node build/download_helm.ts", "download:helm": "yarn run ts-node build/download_helm.ts",
"build:tray-icons": "yarn run ts-node build/build_tray_icon.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", "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", "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", "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": { "jest": {
"collectCoverage": false, "collectCoverage": false,
"verbose": true, "verbose": true,
"testEnvironment": "node",
"transform": { "transform": {
"^.+\\.tsx?$": "ts-jest" "^.+\\.tsx?$": "ts-jest"
}, },

View File

@ -1 +0,0 @@
jest --env=jsdom ${1:-src}

View File

@ -35,7 +35,8 @@ jest.mock("electron", () => {
app: { app: {
getVersion: () => "99.99.99", getVersion: () => "99.99.99",
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en" getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
handle: jest.fn(), handle: jest.fn(),

View File

@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/
import { SearchStore } from "../search-store"; import { SearchStore } from "../search-store";
let searchStore: SearchStore = null; let searchStore: SearchStore = null;

View File

@ -1,3 +1,7 @@
import { Console } from "console";
console = new Console(process.stdout, process.stderr);
import mockFs from "mock-fs"; import mockFs from "mock-fs";
jest.mock("electron", () => { jest.mock("electron", () => {
@ -5,7 +9,8 @@ jest.mock("electron", () => {
app: { app: {
getVersion: () => "99.99.99", getVersion: () => "99.99.99",
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en" getLocale: () => "en",
setLoginItemSettings: jest.fn(),
} }
}; };
}); });

View File

@ -5,7 +5,8 @@ jest.mock("electron", () => {
app: { app: {
getVersion: () => "99.99.99", getVersion: () => "99.99.99",
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en" getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
handle: jest.fn(), handle: jest.fn(),

View File

@ -19,7 +19,7 @@ export interface BaseStoreParams<T = any> extends ConfOptions<T> {
* Note: T should only contain base JSON serializable types. * Note: T should only contain base JSON serializable types.
*/ */
export abstract class BaseStore<T = any> extends Singleton { export abstract class BaseStore<T = any> extends Singleton {
protected storeConfig: Config<T>; protected storeConfig?: Config<T>;
protected syncDisposers: Function[] = []; protected syncDisposers: Function[] = [];
whenLoaded = when(() => this.isLoaded); whenLoaded = when(() => this.isLoaded);
@ -36,7 +36,7 @@ export abstract class BaseStore<T = any> extends Singleton {
} }
get name() { get name() {
return path.basename(this.storeConfig.path); return path.basename(this.path);
} }
protected get syncRendererChannel() { protected get syncRendererChannel() {
@ -48,7 +48,7 @@ export abstract class BaseStore<T = any> extends Singleton {
} }
get path() { get path() {
return this.storeConfig.path; return this.storeConfig?.path || "";
} }
protected async init() { protected async init() {
@ -82,10 +82,13 @@ export abstract class BaseStore<T = any> extends Singleton {
protected async saveToFile(model: T) { protected async saveToFile(model: T) {
logger.info(`[STORE]: SAVING ${this.path}`); logger.info(`[STORE]: SAVING ${this.path}`);
// todo: update when fixed https://github.com/sindresorhus/conf/issues/114 // todo: update when fixed https://github.com/sindresorhus/conf/issues/114
Object.entries(model).forEach(([key, value]) => { if (this.storeConfig) {
this.storeConfig.set(key, value); for (const [key, value] of Object.entries(model)) {
}); this.storeConfig.set(key, value);
}
}
} }
enableSync() { enableSync() {

View File

@ -4,3 +4,7 @@ fetchMock.enableMocks();
// Mock __non_webpack_require__ for tests // Mock __non_webpack_require__ for tests
globalThis.__non_webpack_require__ = jest.fn(); globalThis.__non_webpack_require__ = jest.fn();
process.on("unhandledRejection", (err) => {
fail(err);
});

View File

@ -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"; import { IKubeApiParsed, parseKubeApi } from "../kube-api-parse";
interface KubeApiParseTestData { /**
url: string; * [<input-url>, <expected-result>]
expected: Required<IKubeApiParsed>; */
} type KubeApiParseTestData = [string, Required<IKubeApiParsed>];
const tests: KubeApiParseTestData[] = [ const tests: KubeApiParseTestData[] = [
{ ["/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/prometheuses.monitoring.coreos.com", {
url: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/prometheuses.monitoring.coreos.com", apiBase: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions",
expected: { apiPrefix: "/apis",
apiBase: "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions", apiGroup: "apiextensions.k8s.io",
apiPrefix: "/apis", apiVersion: "v1beta1",
apiGroup: "apiextensions.k8s.io", apiVersionWithGroup: "apiextensions.k8s.io/v1beta1",
apiVersion: "v1beta1", namespace: undefined,
apiVersionWithGroup: "apiextensions.k8s.io/v1beta1", resource: "customresourcedefinitions",
namespace: undefined, name: "prometheuses.monitoring.coreos.com"
resource: "customresourcedefinitions", }],
name: "prometheuses.monitoring.coreos.com" ["/api/v1/namespaces/kube-system/pods/coredns-6955765f44-v8p27", {
}, apiBase: "/api/v1/pods",
}, apiPrefix: "/api",
{ apiGroup: "",
url: "/api/v1/namespaces/kube-system/pods/coredns-6955765f44-v8p27", apiVersion: "v1",
expected: { apiVersionWithGroup: "v1",
apiBase: "/api/v1/pods", namespace: "kube-system",
apiPrefix: "/api", resource: "pods",
apiGroup: "", name: "coredns-6955765f44-v8p27"
apiVersion: "v1", }],
apiVersionWithGroup: "v1", ["/apis/stable.example.com/foo1/crontabs", {
namespace: "kube-system", apiBase: "/apis/stable.example.com/foo1/crontabs",
resource: "pods", apiPrefix: "/apis",
name: "coredns-6955765f44-v8p27" apiGroup: "stable.example.com",
}, apiVersion: "foo1",
}, apiVersionWithGroup: "stable.example.com/foo1",
{ resource: "crontabs",
url: "/apis/stable.example.com/foo1/crontabs", name: undefined,
expected: { namespace: undefined,
apiBase: "/apis/stable.example.com/foo1/crontabs", }],
apiPrefix: "/apis", ["/apis/cluster.k8s.io/v1alpha1/clusters", {
apiGroup: "stable.example.com", apiBase: "/apis/cluster.k8s.io/v1alpha1/clusters",
apiVersion: "foo1", apiPrefix: "/apis",
apiVersionWithGroup: "stable.example.com/foo1", apiGroup: "cluster.k8s.io",
resource: "crontabs", apiVersion: "v1alpha1",
name: undefined, apiVersionWithGroup: "cluster.k8s.io/v1alpha1",
namespace: undefined, resource: "clusters",
}, name: undefined,
}, namespace: undefined,
{ }],
url: "/apis/cluster.k8s.io/v1alpha1/clusters", ["/api/v1/namespaces", {
expected: { apiBase: "/api/v1/namespaces",
apiBase: "/apis/cluster.k8s.io/v1alpha1/clusters", apiPrefix: "/api",
apiPrefix: "/apis", apiGroup: "",
apiGroup: "cluster.k8s.io", apiVersion: "v1",
apiVersion: "v1alpha1", apiVersionWithGroup: "v1",
apiVersionWithGroup: "cluster.k8s.io/v1alpha1", resource: "namespaces",
resource: "clusters", name: undefined,
name: undefined, namespace: undefined,
namespace: undefined, }],
}, ["/api/v1/secrets", {
}, apiBase: "/api/v1/secrets",
{ apiPrefix: "/api",
url: "/api/v1/namespaces", apiGroup: "",
expected: { apiVersion: "v1",
apiBase: "/api/v1/namespaces", apiVersionWithGroup: "v1",
apiPrefix: "/api", resource: "secrets",
apiGroup: "", name: undefined,
apiVersion: "v1", namespace: undefined,
apiVersionWithGroup: "v1", }],
resource: "namespaces", ["/api/v1/nodes/minikube", {
name: undefined, apiBase: "/api/v1/nodes",
namespace: undefined, apiPrefix: "/api",
}, apiGroup: "",
}, apiVersion: "v1",
{ apiVersionWithGroup: "v1",
url: "/api/v1/secrets", resource: "nodes",
expected: { name: "minikube",
apiBase: "/api/v1/secrets", namespace: undefined,
apiPrefix: "/api", }],
apiGroup: "", ["/api/foo-bar/nodes/minikube", {
apiVersion: "v1", apiBase: "/api/foo-bar/nodes",
apiVersionWithGroup: "v1", apiPrefix: "/api",
resource: "secrets", apiGroup: "",
name: undefined, apiVersion: "foo-bar",
namespace: undefined, apiVersionWithGroup: "foo-bar",
}, resource: "nodes",
}, name: "minikube",
{ namespace: undefined,
url: "/api/v1/nodes/minikube", }],
expected: { ["/api/v1/namespaces/kube-public", {
apiBase: "/api/v1/nodes", apiBase: "/api/v1/namespaces",
apiPrefix: "/api", apiPrefix: "/api",
apiGroup: "", apiGroup: "",
apiVersion: "v1", apiVersion: "v1",
apiVersionWithGroup: "v1", apiVersionWithGroup: "v1",
resource: "nodes", resource: "namespaces",
name: "minikube", name: "kube-public",
namespace: undefined, 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,
},
},
]; ];
describe("parseApi unit tests", () => { describe("parseApi unit tests", () => {
for (const { url, expected } of tests) { it.each(tests)("testing %s", (url, expected) => {
test(`testing "${url}"`, () => { expect(parseKubeApi(url)).toStrictEqual(expected);
expect(parseKubeApi(url)).toStrictEqual(expected); });
});
}
}); });

View File

@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/
import React from "react"; import React from "react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { fireEvent, render } from "@testing-library/react"; import { fireEvent, render } from "@testing-library/react";

View File

@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/
import React from "react"; import React from "react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { render } from "@testing-library/react"; import { render } from "@testing-library/react";

View File

@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/
import { podsStore } from "../../+workloads-pods/pods.store"; import { podsStore } from "../../+workloads-pods/pods.store";
import { Pod } from "../../../api/endpoints"; import { Pod } from "../../../api/endpoints";
import { dockStore } from "../dock.store"; import { dockStore } from "../dock.store";