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

Fix unit tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-10-27 14:53:06 -04:00
parent ebd918fcf7
commit 938f44fd80
18 changed files with 174 additions and 96 deletions

View File

@ -68,23 +68,24 @@ function embed(clusterId: ClusterId, contents: any): string {
return absPath; return absPath;
} }
jest.mock("electron", () => { jest.mock("electron", () => ({
return { app: {
app: { getVersion: () => "99.99.99",
getVersion: () => "99.99.99", getName: () => "lens",
getPath: () => "tmp", setName: jest.fn(),
getLocale: () => "en", setPath: jest.fn(),
setLoginItemSettings: jest.fn(), getPath: () => "tmp",
}, getLocale: () => "en",
ipcMain: { setLoginItemSettings: jest.fn(),
handle: jest.fn(), },
on: jest.fn(), ipcMain: {
removeAllListeners: jest.fn(), handle: jest.fn(),
off: jest.fn(), on: jest.fn(),
send: jest.fn(), removeAllListeners: jest.fn(),
} off: jest.fn(),
}; send: jest.fn(),
}); }
}));
AppPaths.init(); AppPaths.init();

View File

@ -117,20 +117,21 @@ const awsCluster = {
} }
}; };
jest.mock("electron", () => { jest.mock("electron", () => ({
return { app: {
app: { getVersion: () => "99.99.99",
getVersion: () => "99.99.99", getName: () => "lens",
getPath: () => "tmp", setName: jest.fn(),
getLocale: () => "en", setPath: jest.fn(),
setLoginItemSettings: (): void => void 0, getPath: () => "tmp",
}, getLocale: () => "en",
ipcMain: { setLoginItemSettings: jest.fn(),
on: jest.fn(), },
handle: jest.fn(), ipcMain: {
}, on: jest.fn(),
}; handle: jest.fn(),
}); },
}));
AppPaths.init(); AppPaths.init();

View File

@ -21,20 +21,21 @@
import mockFs from "mock-fs"; import mockFs from "mock-fs";
jest.mock("electron", () => { jest.mock("electron", () => ({
return { app: {
app: { getVersion: () => "99.99.99",
getVersion: () => "99.99.99", getName: () => "lens",
getPath: () => "tmp", setName: jest.fn(),
getLocale: () => "en", setPath: jest.fn(),
setLoginItemSettings: (): void => void 0, getPath: () => "tmp",
}, getLocale: () => "en",
ipcMain: { setLoginItemSettings: jest.fn(),
on: jest.fn(), },
handle: jest.fn(), ipcMain: {
}, on: jest.fn(),
}; handle: jest.fn(),
}); },
}));
import { UserStore } from "../user-store"; import { UserStore } from "../user-store";
import { Console } from "console"; import { Console } from "console";

View File

@ -42,7 +42,12 @@ jest.mock("../extension-installer", () => ({
})); }));
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(), setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
@ -51,11 +56,11 @@ jest.mock("electron", () => ({
}, },
})); }));
AppPaths.init();
console = new Console(process.stdout, process.stderr); // fix mockFS console = new Console(process.stdout, process.stderr); // fix mockFS
const mockedWatch = watch as jest.MockedFunction<typeof watch>; const mockedWatch = watch as jest.MockedFunction<typeof watch>;
AppPaths.init();
describe("ExtensionDiscovery", () => { describe("ExtensionDiscovery", () => {
beforeEach(() => { beforeEach(() => {
ExtensionDiscovery.resetInstance(); ExtensionDiscovery.resetInstance();

View File

@ -34,7 +34,13 @@ jest.mock("react-monaco-editor", () => null);
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -27,7 +27,12 @@ import { AppPaths } from "../../common/app-paths";
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(), setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {

View File

@ -44,13 +44,6 @@ jest.mock("winston", () => ({
} }
})); }));
jest.mock("electron", () => ({
app: {
getPath: () => "tmp",
setLoginItemSettings: jest.fn(),
},
}));
jest.mock("../../common/ipc"); jest.mock("../../common/ipc");
jest.mock("child_process"); jest.mock("child_process");
jest.mock("tcp-port-used"); jest.mock("tcp-port-used");
@ -75,21 +68,21 @@ const mockBroadcastIpc = broadcastMessage as jest.MockedFunction<typeof broadcas
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>; const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>; const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>;
jest.mock("electron", () => { jest.mock("electron", () => ({
return { app: {
app: { getVersion: () => "99.99.99",
getVersion: () => "99.99.99", getName: () => "lens",
getPath: () => "tmp", setName: jest.fn(),
getLocale: () => "en", setPath: jest.fn(),
setLoginItemSettings: (): void => void 0, getPath: () => "tmp",
}, getLocale: () => "en",
ipcMain: { setLoginItemSettings: jest.fn(),
on: jest.fn(), },
handle: jest.fn(), ipcMain: {
}, on: jest.fn(),
}; handle: jest.fn(),
}); },
}));
AppPaths.init(); AppPaths.init();
describe("kube auth proxy tests", () => { describe("kube auth proxy tests", () => {

View File

@ -28,12 +28,6 @@ const logger = {
crit: jest.fn(), crit: jest.fn(),
}; };
jest.mock("electron", () => ({
app: {
getPath: () => `/tmp`,
},
}));
jest.mock("winston", () => ({ jest.mock("winston", () => ({
format: { format: {
colorize: jest.fn(), colorize: jest.fn(),
@ -41,7 +35,9 @@ jest.mock("winston", () => ({
simple: jest.fn(), simple: jest.fn(),
label: jest.fn(), label: jest.fn(),
timestamp: jest.fn(), timestamp: jest.fn(),
printf: jest.fn() padLevels: jest.fn(),
ms: jest.fn(),
printf: jest.fn(),
}, },
createLogger: jest.fn().mockReturnValue(logger), createLogger: jest.fn().mockReturnValue(logger),
transports: { transports: {
@ -58,6 +54,25 @@ import fse from "fs-extra";
import { loadYaml } from "@kubernetes/client-node"; import { loadYaml } from "@kubernetes/client-node";
import { Console } from "console"; import { Console } from "console";
import * as path from "path"; import * as path from "path";
import { AppPaths } from "../../common/app-paths";
jest.mock("electron", () => ({
app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
},
ipcMain: {
on: jest.fn(),
handle: jest.fn(),
},
}));
AppPaths.init();
console = new Console(process.stdout, process.stderr); // fix mockFS console = new Console(process.stdout, process.stderr); // fix mockFS
@ -111,7 +126,7 @@ describe("kubeconfig manager tests", () => {
const kubeConfManager = new KubeconfigManager(cluster, contextHandler); const kubeConfManager = new KubeconfigManager(cluster, contextHandler);
expect(logger.error).not.toBeCalled(); expect(logger.error).not.toBeCalled();
expect(await kubeConfManager.getPath()).toBe(`${path.sep}tmp${path.sep}kubeconfig-foo`); expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`);
// this causes an intermittent "ENXIO: no such device or address, read" error // this causes an intermittent "ENXIO: no such device or address, read" error
// const file = await fse.readFile(await kubeConfManager.getPath()); // const file = await fse.readFile(await kubeConfManager.getPath());
const file = fse.readFileSync(await kubeConfManager.getPath()); const file = fse.readFileSync(await kubeConfManager.getPath());

View File

@ -24,7 +24,12 @@ import { Router } from "../router";
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(), setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {

View File

@ -32,7 +32,13 @@ import { AppPaths } from "../../../common/app-paths";
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getPath: () => "/foo", getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -35,7 +35,12 @@ jest.mock("../../../common/ipc");
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(), setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {

View File

@ -31,26 +31,30 @@ import { CatalogEntityRegistry } from "../../../renderer/api/catalog-entity-regi
import { CatalogEntityDetailRegistry } from "../../../extensions/registries"; import { CatalogEntityDetailRegistry } from "../../../extensions/registries";
import { CatalogEntityItem } from "./catalog-entity-item"; import { CatalogEntityItem } from "./catalog-entity-item";
import { CatalogEntityStore } from "./catalog-entity.store"; import { CatalogEntityStore } from "./catalog-entity.store";
import { AppPaths } from "../../../common/app-paths";
mockWindow(); mockWindow();
jest.mock("electron", () => ({
app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
},
ipcMain: {
on: jest.fn(),
handle: jest.fn(),
},
}));
// avoid TypeError: Cannot read property 'getPath' of undefined AppPaths.init();
jest.mock("@electron/remote", () => {
return {
app: {
getPath: () => {
// avoid TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
return "";
},
},
};
});
jest.mock("./hotbar-toggle-menu-item", () => { jest.mock("./hotbar-toggle-menu-item", () => ({
return { HotbarToggleMenuItem: () => <div>menu item</div>
HotbarToggleMenuItem: () => <div>menu item</div> }));
};
});
class MockCatalogEntity extends CatalogEntity { class MockCatalogEntity extends CatalogEntity {
public apiVersion = "api"; public apiVersion = "api";

View File

@ -57,9 +57,12 @@ jest.mock("../../../../common/utils/tar");
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99", getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en", getLocale: () => "en",
setLoginItemSettings: (): void => void 0, setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -31,7 +31,13 @@ import { AppPaths } from "../../../../common/app-paths";
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -42,14 +42,19 @@ jest.mock("react-monaco-editor", () => ({
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),
handle: jest.fn(), handle: jest.fn(),
}, },
})); }));
AppPaths.init(); AppPaths.init();
const initialTabs: DockTab[] = [ const initialTabs: DockTab[] = [

View File

@ -37,7 +37,13 @@ jest.mock("react-monaco-editor", () => null);
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -36,7 +36,13 @@ jest.mock("react-monaco-editor", () => null);
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {
on: jest.fn(), on: jest.fn(),

View File

@ -31,7 +31,12 @@ import { AppPaths } from "../../../../common/app-paths";
jest.mock("electron", () => ({ jest.mock("electron", () => ({
app: { app: {
getVersion: () => "99.99.99",
getName: () => "lens",
setName: jest.fn(),
setPath: jest.fn(),
getPath: () => "tmp", getPath: () => "tmp",
getLocale: () => "en",
setLoginItemSettings: jest.fn(), setLoginItemSettings: jest.fn(),
}, },
ipcMain: { ipcMain: {