From 43c7b40b70d9b5d10090d207a7f423b1aaef6e62 Mon Sep 17 00:00:00 2001 From: awadhana Date: Mon, 27 Dec 2021 16:04:23 -0500 Subject: [PATCH] test(plugin-keychain-memory): jestify plugin-keychain-memory test Migrated test from Tap to Jest. File Path: packages/cactus-plugin-keychain-memory/src/ test/typescript/unit/plugin-keychain-memory.test.ts This is a PARTIAL resolution to issue hyperledger#238 Signed-off-by: awadhana Signed-off-by: Peter Somogyvari --- .github/workflows/ci.yml | 3 +- .taprc | 1 - jest.config.js | 1 - .../unit/plugin-keychain-memory.test.ts | 105 ++++++++---------- 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c410e7a859..d7b8e9c19a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -572,8 +572,7 @@ jobs: FULL_BUILD_DISABLED: true JEST_TEST_PATTERN: packages/cactus-plugin-keychain-memory/src/test/typescript/(unit|integration|benchmark)/.*/*.test.ts JEST_TEST_RUNNER_DISABLED: false - TAPE_TEST_PATTERN: ./packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts - TAPE_TEST_RUNNER_DISABLED: false + TAPE_TEST_RUNNER_DISABLED: true needs: build-dev runs-on: ubuntu-20.04 steps: diff --git a/.taprc b/.taprc index e4e8a4f2fd..51c7f9da71 100644 --- a/.taprc +++ b/.taprc @@ -74,7 +74,6 @@ files: - ./packages/cactus-common/src/test/typescript/unit/key-converter.test.ts - ./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts - ./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-keychain-aws-sm.test.ts - - ./packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts - ./packages/cactus-api-client/src/test/typescript/integration/default-consortium-provider.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts - ./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts diff --git a/jest.config.js b/jest.config.js index 11f90901e2..da86a1599b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -78,7 +78,6 @@ module.exports = { `./packages/cactus-common/src/test/typescript/unit/key-converter.test.ts`, `./packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts`, `./packages/cactus-plugin-keychain-aws-sm/src/test/typescript/integration/plugin-keychain-aws-sm.test.ts`, - `./packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts`, `./packages/cactus-api-client/src/test/typescript/integration/default-consortium-provider.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/get-balance-endpoint.test.ts`, `./packages/cactus-test-plugin-ledger-connector-besu/src/test/typescript/integration/plugin-validator-besu/v21-get-past-logs-endpoint.test.ts`, diff --git a/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts b/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts index ca12f6cc0f..fd4eddaa1f 100644 --- a/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts +++ b/packages/cactus-plugin-keychain-memory/src/test/typescript/unit/plugin-keychain-memory.test.ts @@ -1,6 +1,5 @@ -import test, { Test } from "tape-promise/tape"; - import express from "express"; +import "jest-extended"; import bodyParser from "body-parser"; import http from "http"; import { AddressInfo } from "net"; @@ -18,99 +17,95 @@ import { K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT } from "../../../main/typescri import { DefaultApi as KeychainMemoryApi } from "../../../main/typescript/public-api"; import { Configuration } from "@hyperledger/cactus-core-api"; -test("PluginKeychainMemory", (t1: Test) => { - t1.doesNotThrow( +const testcase = "PluginKeychainMemory"; +describe(testcase, () => { + const expressApp = express(); + expressApp.use(bodyParser.json({ limit: "250mb" })); + const server = http.createServer(expressApp); + const listenOptions: IListenOptions = { + hostname: "localhost", + port: 0, + server, + }; + afterAll(async () => await Servers.shutdown(server)); + + expect( () => new PluginKeychainMemory({ instanceId: "a", keychainId: "a" }), - ); + ).not.toThrow(); - test("Validates constructor arg instanceId", (t: Test) => { - t.throws( + test("Validates constructor arg instanceId", () => { + expect( () => new PluginKeychainMemory({ instanceId: null as any, keychainId: "valid-value", }), - ); - t.throws( + ).toThrow(); + expect( () => new PluginKeychainMemory({ instanceId: "", keychainId: "valid-value", }), - ); - t.end(); + ).toThrow(); }); - test("Validates constructor arg keychainId", (t: Test) => { - t.throws( + test("Validates constructor arg keychainId", () => { + expect( () => new PluginKeychainMemory({ instanceId: "valid-value", keychainId: null as any, }), - ); - t.throws( + ).toThrow(); + expect( () => new PluginKeychainMemory({ instanceId: "valid-value", keychainId: "", }), - ); - t.end(); + ).toThrow(); }); - test("get,set,has,delete alters state as expected", async (t: Test) => { + test("get,set,has,delete alters state as expected", async () => { const options: IPluginKeychainMemoryOptions = { instanceId: uuidv4(), keychainId: uuidv4(), }; const plugin = new PluginKeychainMemory(options); - const expressApp = express(); - expressApp.use(bodyParser.json({ limit: "250mb" })); - const server = http.createServer(expressApp); - const listenOptions: IListenOptions = { - hostname: "localhost", - port: 0, - server, - }; const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; - test.onFinish(async () => await Servers.shutdown(server)); const { address, port } = addressInfo; const apiHost = `http://${address}:${port}`; - t.comment( - `Metrics URL: ${apiHost}/api/v1/plugins/@hyperledger/cactus-plugin-keychain-memory/get-prometheus-exporter-metrics`, - ); - const config = new Configuration({ basePath: apiHost }); const apiClient = new KeychainMemoryApi(config); await plugin.getOrCreateWebServices(expressApp); - t.equal(plugin.getKeychainId(), options.keychainId, "Keychain ID set OK"); - t.equal(plugin.getInstanceId(), options.instanceId, "Instance ID set OK"); + expect(plugin.getKeychainId()).toBe(options.keychainId); + expect(plugin.getInstanceId()).toBe(options.instanceId); const key1 = uuidv4(); const value1 = uuidv4(); const hasPrior = await plugin.has(key1); - t.false(hasPrior, "hasPrior === false OK"); + expect(hasPrior).toBe(false); await plugin.set(key1, value1); const hasAfter1 = await plugin.has(key1); - t.true(hasAfter1, "hasAfter === true OK"); + expect(hasAfter1).toBe(true); const valueAfter1 = await plugin.get(key1); - t.ok(valueAfter1, "valueAfter truthy OK"); - t.equal(valueAfter1, value1, "valueAfter === value OK"); + expect(valueAfter1).toBeTruthy(); + expect(valueAfter1).toBe(value1); await plugin.delete(key1); const hasAfterDelete1 = await plugin.has(key1); - t.false(hasAfterDelete1, "hasAfterDelete === false OK"); + expect(hasAfterDelete1).not.toBeTruthy(); + await expect(plugin.get(key1)).not.toResolve(); - await t.rejects(plugin.get(key1), key1); { const res = await apiClient.getPrometheusMetricsV1(); const promMetricsOutput = @@ -124,13 +119,10 @@ test("PluginKeychainMemory", (t1: Test) => { '{type="' + K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT + '"} 0'; - t.ok(res); - t.ok(res.data); - t.equal(res.status, 200); - t.true( - res.data.includes(promMetricsOutput), - "Total Key Count 0 recorded as expected. RESULT OK", - ); + expect(res); + expect(res.data); + expect(res.status).toEqual(200); + expect(res.data.includes(promMetricsOutput)).toBe(true); } const key2 = uuidv4(); @@ -139,11 +131,11 @@ test("PluginKeychainMemory", (t1: Test) => { await plugin.set(key2, value2); const hasAfter = await plugin.has(key2); - t.true(hasAfter, "hasAfter === true OK"); + expect(hasAfter).toBe(true); const valueAfter2 = await plugin.get(key2); - t.ok(valueAfter2, "valueAfter truthy OK"); - t.equal(valueAfter2, value2, "valueAfter === value OK"); + expect(valueAfter2).toBeTruthy(); + expect(valueAfter2).toEqual(value2); { const res = await apiClient.getPrometheusMetricsV1(); const promMetricsOutput = @@ -157,17 +149,10 @@ test("PluginKeychainMemory", (t1: Test) => { '{type="' + K_CACTUS_KEYCHAIN_MEMORY_TOTAL_KEY_COUNT + '"} 1'; - t.ok(res); - t.ok(res.data); - t.equal(res.status, 200); - t.true( - res.data.includes(promMetricsOutput), - "Total Key Count 1 recorded as expected. RESULT OK", - ); + expect(res); + expect(res.data); + expect(res.status).toEqual(200); + expect(res.data.includes(promMetricsOutput)).toBe(true); } - - t.end(); }); - - t1.end(); });