From 32df39cec50aa6bc5f6791337d7ae7ece0b0c14f Mon Sep 17 00:00:00 2001 From: Mark McDowell Date: Tue, 21 Apr 2020 18:09:10 +0100 Subject: [PATCH] tests(core): simplifying tests Using json-mock-extended to make the tests easier to write. --- package-lock.json | 15 +++++++ package.json | 1 + .../directoryConfigurationLoader.test.ts | 11 ++--- .../localFileConfigurationLoader.test.ts | 15 ++++--- .../remoteFileConfigurationLoader.test.ts | 11 ++--- .../compositeConfigurationWriter.test.ts | 43 ++++++------------- .../writers/localConfigurationWriter.test.ts | 10 ++--- .../applicationLauncherService.test.ts | 10 +++-- .../electronServiceLauncherService.test.ts | 16 ++++--- ...iorityConfigurationRegistryService.test.ts | 9 ++-- 10 files changed, 72 insertions(+), 69 deletions(-) diff --git a/package-lock.json b/package-lock.json index e45b1102..1255177d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10718,6 +10718,15 @@ "@jest/types": "^25.4.0" } }, + "jest-mock-extended": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-1.0.8.tgz", + "integrity": "sha512-lwRgbnngx6Woh0OOapes3uJQzngCj69OIhKnXYybEWwE0O1w7UyBJwLciHH6D/MCedogA90qxhEkEANrA20Sfw==", + "dev": true, + "requires": { + "ts-essentials": "^4.0.0" + } + }, "jest-pnp-resolver": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.1.tgz", @@ -15942,6 +15951,12 @@ "utf8-byte-length": "^1.0.1" } }, + "ts-essentials": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-4.0.0.tgz", + "integrity": "sha512-uQJX+SRY9mtbKU+g9kl5Fi7AEMofPCvHfJkQlaygpPmHPZrtgaBqbWFOYyiA47RhnSwwnXdepUJrgqUYxoUyhQ==", + "dev": true + }, "ts-jest": { "version": "25.4.0", "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-25.4.0.tgz", diff --git a/package.json b/package.json index 14e69e9a..3f8f421c 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "helmet": "^3.22.0", "ix": "^2.5.5", "jest": "^25.4.0", + "jest-mock-extended": "^1.0.8", "js-yaml": "^3.13.1", "lerna": "^3.20.2", "npm-run-all": "^4.1.5", diff --git a/packages/desktop-core/__tests__/configuration/loaders/directoryConfigurationLoader.test.ts b/packages/desktop-core/__tests__/configuration/loaders/directoryConfigurationLoader.test.ts index fa6b1ffa..34f0eba4 100644 --- a/packages/desktop-core/__tests__/configuration/loaders/directoryConfigurationLoader.test.ts +++ b/packages/desktop-core/__tests__/configuration/loaders/directoryConfigurationLoader.test.ts @@ -1,14 +1,15 @@ +import { mock } from "jest-mock-extended"; + import { DirectoryConfigurationLoader } from "../../../src/configuration/loaders/directoryConfigurationLoader"; import { IConfigurationLoader } from "../../../src/configuration/loaders/iConfigurationLoader"; -// tslint:disable:variable-name describe("canLoad", () => { describe("can load from", () => { test("directory", () => { - const fileLoader = new (jest.fn, string[]>())(); + const fileLoader = mock>(); const loader = new DirectoryConfigurationLoader(fileLoader); @@ -21,7 +22,7 @@ describe("canLoad", () => { test("http", () => { - const fileLoader = new (jest.fn, string[]>())(); + const fileLoader = mock>(); const loader = new DirectoryConfigurationLoader(fileLoader); @@ -31,7 +32,7 @@ describe("canLoad", () => { test("https", () => { - const fileLoader = new (jest.fn, string[]>())(); + const fileLoader = mock>(); const loader = new DirectoryConfigurationLoader(fileLoader); @@ -41,7 +42,7 @@ describe("canLoad", () => { test("an extension", () => { - const fileLoader = new (jest.fn, string[]>())(); + const fileLoader = mock>(); const loader = new DirectoryConfigurationLoader(fileLoader); diff --git a/packages/desktop-core/__tests__/configuration/loaders/localFileConfigurationLoader.test.ts b/packages/desktop-core/__tests__/configuration/loaders/localFileConfigurationLoader.test.ts index d8a0eb5f..2f1a9652 100644 --- a/packages/desktop-core/__tests__/configuration/loaders/localFileConfigurationLoader.test.ts +++ b/packages/desktop-core/__tests__/configuration/loaders/localFileConfigurationLoader.test.ts @@ -1,14 +1,15 @@ +import { mock } from "jest-mock-extended"; + import { LocalFileConfigurationLoader } from "../../../src/configuration/loaders/localFileConfigurationLoader"; import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser"; -// tslint:disable:variable-name describe("canLoad", () => { describe("can load from", () => { test("extension", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "yaml"); @@ -18,7 +19,7 @@ describe("canLoad", () => { test("a local file", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "yaml"); @@ -28,7 +29,7 @@ describe("canLoad", () => { test("a network file", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "yaml"); @@ -41,7 +42,7 @@ describe("canLoad", () => { test("http", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "json"); @@ -51,7 +52,7 @@ describe("canLoad", () => { test("https", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "yaml"); @@ -61,7 +62,7 @@ describe("canLoad", () => { test("an unknown extension", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new LocalFileConfigurationLoader(parser, "yaml"); diff --git a/packages/desktop-core/__tests__/configuration/loaders/remoteFileConfigurationLoader.test.ts b/packages/desktop-core/__tests__/configuration/loaders/remoteFileConfigurationLoader.test.ts index 90f4861a..aa4b983f 100644 --- a/packages/desktop-core/__tests__/configuration/loaders/remoteFileConfigurationLoader.test.ts +++ b/packages/desktop-core/__tests__/configuration/loaders/remoteFileConfigurationLoader.test.ts @@ -1,14 +1,15 @@ +import { mock } from "jest-mock-extended"; + import { RemoteFileConfigurationLoader } from "../../../src/configuration/loaders/remoteFileConfigurationLoader"; import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser"; -// tslint:disable:variable-name describe("canLoad", () => { describe("can load from", () => { test("http", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new RemoteFileConfigurationLoader(parser, "yaml"); @@ -18,7 +19,7 @@ describe("canLoad", () => { test("https", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new RemoteFileConfigurationLoader(parser, "yaml"); @@ -31,7 +32,7 @@ describe("canLoad", () => { test("a local file", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new RemoteFileConfigurationLoader(parser, "yaml"); @@ -40,7 +41,7 @@ describe("canLoad", () => { }); test("a network file", () => { - const parser = new (jest.fn, string[]>())(); + const parser = mock>(); const loader = new RemoteFileConfigurationLoader(parser, "yaml"); diff --git a/packages/desktop-core/__tests__/configuration/writers/compositeConfigurationWriter.test.ts b/packages/desktop-core/__tests__/configuration/writers/compositeConfigurationWriter.test.ts index 3f9836d1..1c387bfa 100644 --- a/packages/desktop-core/__tests__/configuration/writers/compositeConfigurationWriter.test.ts +++ b/packages/desktop-core/__tests__/configuration/writers/compositeConfigurationWriter.test.ts @@ -1,16 +1,15 @@ +import { mock } from "jest-mock-extended"; + import { CompositeConfigurationWriter } from "../../../src/configuration/writers/compositeConfigurationWriter"; +import { IConfigurationWriter } from "../../../src/configuration/writers/iConfigurationWriter"; describe("canWrite", () => { describe("can write", () => { test("when 1 writer can", () => { - const canWrite = jest.fn(() => ({ - canWrite: jest.fn(() => true), - write: jest.fn(), - })); - - const jsonWriter = new canWrite(); + const jsonWriter = mock>(); + jsonWriter.canWrite.mockReturnValue(true); const writer = new CompositeConfigurationWriter(jsonWriter); @@ -19,19 +18,11 @@ describe("canWrite", () => { }); test("when at least 1 writer can", () => { - const cantWrite = jest.fn(() => ({ - canWrite: jest.fn(() => false), - write: jest.fn(), - })); - - const yamlWriter = new cantWrite(); + const yamlWriter = mock>(); + yamlWriter.canWrite.mockReturnValue(false); - const canWrite = jest.fn(() => ({ - canWrite: jest.fn(() => true), - write: jest.fn(), - })); - - const jsonWriter = new canWrite(); + const jsonWriter = mock>(); + jsonWriter.canWrite.mockReturnValue(true); const writer = new CompositeConfigurationWriter(yamlWriter, jsonWriter); @@ -43,19 +34,11 @@ describe("canWrite", () => { describe("can't write", () => { test("when all writers can't", () => { - const cantWrite = jest.fn(() => ({ - canWrite: jest.fn(() => false), - write: jest.fn(), - })); - - const yamlWriter = new cantWrite(); - - const canWrite = jest.fn(() => ({ - canWrite: jest.fn(() => false), - write: jest.fn(), - })); + const yamlWriter = mock>(); + yamlWriter.canWrite.mockReturnValue(false); - const jsonWriter = new canWrite(); + const jsonWriter = mock>(); + jsonWriter.canWrite.mockReturnValue(false); const writer = new CompositeConfigurationWriter(yamlWriter, jsonWriter); diff --git a/packages/desktop-core/__tests__/configuration/writers/localConfigurationWriter.test.ts b/packages/desktop-core/__tests__/configuration/writers/localConfigurationWriter.test.ts index 483fa8d2..c0d209ab 100644 --- a/packages/desktop-core/__tests__/configuration/writers/localConfigurationWriter.test.ts +++ b/packages/desktop-core/__tests__/configuration/writers/localConfigurationWriter.test.ts @@ -1,3 +1,5 @@ +import { mock } from "jest-mock-extended"; + import { IConfigurationParser } from "../../../src/configuration/parsers/iConfigurationParser"; import { LocalConfigurationWriter } from "../../../src/configuration/writers/localConfigurationWriter"; @@ -6,9 +8,7 @@ describe("canWrite", () => { describe("can write to", () => { test("specified extension", () => { - const mockParser = jest.fn, string[]>(); - - const parser = new mockParser(); + const parser = mock>(); const writer = new LocalConfigurationWriter(parser, "yaml"); @@ -20,9 +20,7 @@ describe("canWrite", () => { describe("can't write to", () => { test("unspecified extension", () => { - const mockParser = jest.fn, string[]>(); - - const parser = new mockParser(); + const parser = mock>(); const writer = new LocalConfigurationWriter(parser, "yaml"); diff --git a/packages/desktop-core/__tests__/launcher/applicationLauncherService.test.ts b/packages/desktop-core/__tests__/launcher/applicationLauncherService.test.ts index 17777385..0f39848e 100644 --- a/packages/desktop-core/__tests__/launcher/applicationLauncherService.test.ts +++ b/packages/desktop-core/__tests__/launcher/applicationLauncherService.test.ts @@ -1,3 +1,5 @@ +import { mock } from "jest-mock-extended"; + import { ConfigurationKind } from "../../src/configuration/configurationKind"; import { IConfiguration } from "../../src/configuration/iConfiguration"; import { ServiceHost } from "../../src/configuration/serviceHost"; @@ -11,9 +13,9 @@ describe("canLaunch", () => { test("when kind is application", () => { - const windowFactory = new (jest.fn())(); + const windowFactory = mock(); - const logger = new (jest.fn())(); + const logger = mock(); const launcher = new ApplicationLauncherService(logger, windowFactory); @@ -35,9 +37,9 @@ describe("canLaunch", () => { describe("can't launch", () => { test("when kind is service", () => { - const windowFactory = new (jest.fn())(); + const windowFactory = mock(); - const logger = new (jest.fn())(); + const logger = mock(); const launcher = new ApplicationLauncherService(logger, windowFactory); diff --git a/packages/desktop-core/__tests__/launcher/electronServiceLauncherService.test.ts b/packages/desktop-core/__tests__/launcher/electronServiceLauncherService.test.ts index 0de96048..05e59ce5 100644 --- a/packages/desktop-core/__tests__/launcher/electronServiceLauncherService.test.ts +++ b/packages/desktop-core/__tests__/launcher/electronServiceLauncherService.test.ts @@ -1,3 +1,5 @@ +import { mock } from "jest-mock-extended"; + import { ConfigurationKind } from "../../src/configuration/configurationKind"; import { IConfiguration } from "../../src/configuration/iConfiguration"; import { ServiceHost } from "../../src/configuration/serviceHost"; @@ -7,13 +9,13 @@ import { IWindowFactory } from "../../src/windowing/iWindowFactory"; describe("canLaunch", () => { - describe("can launcher", () => { + describe("can launch", () => { test("when kind is service and host is electron", () => { - const windowFactory = new (jest.fn())(); + const windowFactory = mock(); - const logger = new (jest.fn())(); + const logger = mock(); const launcher = new ElectronServiceLauncherService(logger, windowFactory); @@ -36,9 +38,9 @@ describe("canLaunch", () => { describe("can't launch", () => { test("when host is node", () => { - const windowFactory = new (jest.fn())(); + const windowFactory = mock(); - const logger = new (jest.fn())(); + const logger = mock(); const launcher = new ElectronServiceLauncherService(logger, windowFactory); @@ -58,9 +60,9 @@ describe("canLaunch", () => { }); test("when kind is application", () => { - const windowFactory = new (jest.fn())(); + const windowFactory = mock(); - const logger = new (jest.fn())(); + const logger = mock(); const launcher = new ElectronServiceLauncherService(logger, windowFactory); diff --git a/packages/desktop-core/__tests__/registry/priorityConfigurationRegistryService.test.ts b/packages/desktop-core/__tests__/registry/priorityConfigurationRegistryService.test.ts index 89dedec0..92ab7572 100644 --- a/packages/desktop-core/__tests__/registry/priorityConfigurationRegistryService.test.ts +++ b/packages/desktop-core/__tests__/registry/priorityConfigurationRegistryService.test.ts @@ -1,3 +1,5 @@ +import { mock } from "jest-mock-extended"; + import { ConfigurationKind } from "../../src/configuration/configurationKind"; import { IConfiguration } from "../../src/configuration/iConfiguration"; import { IRegistryService } from "../../src/registry/iRegistryService"; @@ -27,11 +29,8 @@ describe("getRegistry", () => { }, }; - const unsortedRegistry = new (jest.fn(() => ({ - getRegistry: jest.fn(() => Promise.resolve([application, session])), - registerConfig: jest.fn(), - registerUrl: jest.fn(), - })))(); + const unsortedRegistry = mock(); + unsortedRegistry.getRegistry.mockReturnValue(Promise.resolve([application, session])); const service = new PriorityConfigurationRegistryService(unsortedRegistry);