From 2e389383310954fa56dbdd1bf7855a9af6519a96 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 1 Mar 2021 12:12:35 -0500 Subject: [PATCH] Fix integration test lifecycle fuctions not failing tests - Fail test if an async lifecycle function fails (though a bug in Jest means that the test body is still run) Signed-off-by: Sebastian Malton --- integration/__tests__/app.tests.ts | 9 +++--- integration/__tests__/cluster-pages.tests.ts | 30 ++++++++++--------- .../__tests__/command-palette.tests.ts | 8 +++-- integration/__tests__/workspace.tests.ts | 10 +++---- integration/helpers/utils.ts | 17 +++++++++++ 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/integration/__tests__/app.tests.ts b/integration/__tests__/app.tests.ts index af029a23e9..d1f3b54616 100644 --- a/integration/__tests__/app.tests.ts +++ b/integration/__tests__/app.tests.ts @@ -3,7 +3,6 @@ import * as utils from "../helpers/utils"; import { listHelmRepositories } from "../helpers/utils"; import { fail } from "assert"; - jest.setTimeout(60000); // FIXME (!): improve / simplify all css-selectors + use [data-test-id="some-id"] (already used in some tests below) @@ -11,13 +10,15 @@ describe("Lens integration tests", () => { let app: Application; describe("app start", () => { - beforeAll(async () => app = await utils.appStart(), 20000); + beforeAll(utils.wrapJestLifecycle(async () => { + app = await utils.appStart(); + })); - afterAll(async () => { + afterAll(utils.wrapJestLifecycle(async () => { if (app?.isRunning()) { await utils.tearDown(app); } - }); + })); it('shows "whats new"', async () => { await utils.clickWhatsNew(app); diff --git a/integration/__tests__/cluster-pages.tests.ts b/integration/__tests__/cluster-pages.tests.ts index e73774f86a..a56f71d1b8 100644 --- a/integration/__tests__/cluster-pages.tests.ts +++ b/integration/__tests__/cluster-pages.tests.ts @@ -32,13 +32,15 @@ describe("Lens cluster pages", () => { }; describe("cluster add", () => { - beforeAll(async () => app = await utils.appStart(), 20000); + beforeAll(utils.wrapJestLifecycle(async () => { + app = await utils.appStart(); + })); - afterAll(async () => { + afterAll(utils.wrapJestLifecycle(async () => { if (app && app.isRunning()) { return utils.tearDown(app); } - }); + })); it("allows to add a cluster", async () => { await addCluster(); @@ -55,13 +57,13 @@ describe("Lens cluster pages", () => { describe("cluster pages", () => { - beforeAll(appStartAddCluster, 40000); + beforeAll(utils.wrapJestLifecycle(appStartAddCluster)); - afterAll(async () => { + afterAll(utils.wrapJestLifecycle(async () => { if (app && app.isRunning()) { return utils.tearDown(app); } - }); + })); const tests: { drawer?: string @@ -337,13 +339,13 @@ describe("Lens cluster pages", () => { }); describe("viewing pod logs", () => { - beforeEach(appStartAddCluster, 40000); + beforeEach(utils.wrapJestLifecycle(appStartAddCluster)); - afterEach(async () => { - if (app && app.isRunning()) { + afterEach(utils.wrapJestLifecycle(async () => { + if (app?.isRunning()) { return utils.tearDown(app); } - }); + })); it(`shows a logs for a pod`, async () => { expect(clusterAdded).toBe(true); @@ -387,13 +389,13 @@ describe("Lens cluster pages", () => { }); describe("cluster operations", () => { - beforeEach(appStartAddCluster, 40000); + beforeEach(utils.wrapJestLifecycle(appStartAddCluster)); - afterEach(async () => { - if (app && app.isRunning()) { + afterEach(utils.wrapJestLifecycle(async () => { + if (app?.isRunning()) { return utils.tearDown(app); } - }); + })); it("shows default namespace", async () => { expect(clusterAdded).toBe(true); diff --git a/integration/__tests__/command-palette.tests.ts b/integration/__tests__/command-palette.tests.ts index 789806c445..86aa469656 100644 --- a/integration/__tests__/command-palette.tests.ts +++ b/integration/__tests__/command-palette.tests.ts @@ -7,13 +7,15 @@ describe("Lens command palette", () => { let app: Application; describe("menu", () => { - beforeAll(async () => app = await utils.appStart(), 20000); + beforeAll(utils.wrapJestLifecycle(async () => { + app = await utils.appStart(); + })); - afterAll(async () => { + afterAll(utils.wrapJestLifecycle(async () => { if (app?.isRunning()) { await utils.tearDown(app); } - }); + })); it("opens command dialog from menu", async () => { await utils.clickWhatsNew(app); diff --git a/integration/__tests__/workspace.tests.ts b/integration/__tests__/workspace.tests.ts index 4164151b0f..fd1a0ef15c 100644 --- a/integration/__tests__/workspace.tests.ts +++ b/integration/__tests__/workspace.tests.ts @@ -13,16 +13,16 @@ describe("Lens integration tests", () => { const ready = minikubeReady("workspace-int-tests"); utils.describeIf(ready)("workspaces", () => { - beforeAll(async () => { + beforeAll(utils.wrapJestLifecycle(async () => { app = await utils.appStart(); await utils.clickWhatsNew(app); - }, 20000); + })); - afterAll(async () => { - if (app && app.isRunning()) { + afterAll(utils.wrapJestLifecycle(async () => { + if (app?.isRunning()) { return utils.tearDown(app); } - }); + })); const switchToWorkspace = async (name: string) => { await app.client.click("[data-test-id=current-workspace]"); diff --git a/integration/helpers/utils.ts b/integration/helpers/utils.ts index f96c9124e2..a79885d0f6 100644 --- a/integration/helpers/utils.ts +++ b/integration/helpers/utils.ts @@ -8,6 +8,23 @@ const AppPaths: Partial> = { "darwin": "./dist/mac/Lens.app/Contents/MacOS/Lens", }; +interface DoneCallback { + (...args: any[]): any; + fail(error?: string | { message: string }): any; +} + +/** + * This is necessary because Jest doesn't do this correctly. + * @param fn The function to call + */ +export function wrapJestLifecycle(fn: () => Promise): (done: DoneCallback) => void { + return function (done: DoneCallback) { + fn() + .then(() => done()) + .catch(error => done.fail(error)); + }; +} + export function itIf(condition: boolean) { return condition ? it : it.skip; }