From 7a63dc98306cab155b032982b38bbff7df2e583b Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Tue, 19 Mar 2024 10:46:59 +0000 Subject: [PATCH] Making progress on getting the eslint-jest plugin working. --- .eslintrc.json | 10 ++++++- packages/backend-core/scripts/test.sh | 8 ++--- .../src/redis/tests/redis.spec.ts | 11 ------- .../src/api/routes/tests/appImport.spec.ts | 4 +-- .../src/api/routes/tests/application.spec.ts | 4 ++- .../src/integrations/tests/mysql.spec.ts | 2 +- .../src/integrations/tests/oracle.spec.ts | 12 ++++---- .../src/integrations/tests/rest.spec.ts | 8 ++--- .../server/src/integrations/tests/sql.spec.ts | 19 ------------ .../src/integrations/tests/sqlAlias.spec.ts | 2 +- .../src/api/routes/global/tests/auth.spec.ts | 13 +++------ .../routes/global/tests/workspaces.spec.ts | 29 ------------------- 12 files changed, 35 insertions(+), 87 deletions(-) delete mode 100644 packages/worker/src/api/routes/global/tests/workspaces.spec.ts diff --git a/.eslintrc.json b/.eslintrc.json index 5a65cb78e4..ca9f0d0c57 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -34,6 +34,7 @@ }, { "files": ["**/*.ts"], + "excludedFiles": ["qa-core/**"], "parser": "@typescript-eslint/parser", "extends": ["eslint:recommended"], "rules": { @@ -48,6 +49,7 @@ }, { "files": ["**/*.spec.ts"], + "excludedFiles": ["qa-core/**"], "parser": "@typescript-eslint/parser", "plugins": ["jest"], "extends": ["eslint:recommended", "plugin:jest/recommended"], @@ -63,7 +65,13 @@ "no-prototype-builtins": "off", "local-rules/no-test-com": "error", "local-rules/email-domain-example-com": "error", - "no-console": "warn" + "no-console": "warn", + // We have a lot of tests that don't have assertions, they use our test + // API client that does the assertions for them + "jest/expect-expect": "off", + // We do this in some tests where the behaviour of internal tables + // differs to external, but the API is broadly the same + "jest/no-conditional-expect": "off" } }, { diff --git a/packages/backend-core/scripts/test.sh b/packages/backend-core/scripts/test.sh index 7d19ec96cc..b9937e3a4a 100644 --- a/packages/backend-core/scripts/test.sh +++ b/packages/backend-core/scripts/test.sh @@ -4,10 +4,10 @@ set -e if [[ -n $CI ]] then # --runInBand performs better in ci where resources are limited - echo "jest --coverage --runInBand --forceExit" - jest --coverage --runInBand --forceExit + echo "jest --coverage --runInBand --forceExit $@" + jest --coverage --runInBand --forceExit $@ else # --maxWorkers performs better in development - echo "jest --coverage --detectOpenHandles" - jest --coverage --detectOpenHandles + echo "jest --coverage --forceExit --detectOpenHandles $@" + jest --coverage --forceExit --detectOpenHandles $@ fi \ No newline at end of file diff --git a/packages/backend-core/src/redis/tests/redis.spec.ts b/packages/backend-core/src/redis/tests/redis.spec.ts index 22dd4ccdd5..e2076ad698 100644 --- a/packages/backend-core/src/redis/tests/redis.spec.ts +++ b/packages/backend-core/src/redis/tests/redis.spec.ts @@ -147,17 +147,6 @@ describe("redis", () => { expect(results).toEqual([1, 2, 3, 4, 5]) }) - it("can increment on a new key", async () => { - const key1 = structures.uuid() - const key2 = structures.uuid() - - const result1 = await redis.increment(key1) - expect(result1).toBe(1) - - const result2 = await redis.increment(key2) - expect(result2).toBe(1) - }) - it("can increment multiple times in parallel", async () => { const key = structures.uuid() const results = await Promise.all( diff --git a/packages/server/src/api/routes/tests/appImport.spec.ts b/packages/server/src/api/routes/tests/appImport.spec.ts index 51331323de..b2ef39838d 100644 --- a/packages/server/src/api/routes/tests/appImport.spec.ts +++ b/packages/server/src/api/routes/tests/appImport.spec.ts @@ -25,8 +25,8 @@ describe("/applications/:appId/import", () => { .expect(200) const appPackage = await config.api.application.get(appId!) expect(appPackage.navigation?.links?.length).toBe(2) - expect(expect(appPackage.navigation?.links?.[0].url).toBe("/blank")) - expect(expect(appPackage.navigation?.links?.[1].url).toBe("/derp")) + expect(appPackage.navigation?.links?.[0].url).toBe("/blank") + expect(appPackage.navigation?.links?.[1].url).toBe("/derp") const screens = await config.api.screen.list() expect(screens.length).toBe(2) expect(screens[0].routing.route).toBe("/derp") diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index e588f13748..6b0ba89350 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -30,7 +30,9 @@ describe("/applications", () => { beforeEach(async () => { app = await config.api.application.create({ name: utils.newid() }) const deployment = await config.api.application.publish(app.appId) - expect(deployment.status).toBe("SUCCESS") + if (deployment.status !== "SUCCESS") { + throw new Error("Failed to publish app") + } jest.clearAllMocks() }) diff --git a/packages/server/src/integrations/tests/mysql.spec.ts b/packages/server/src/integrations/tests/mysql.spec.ts index 51ab92aaf0..5180645885 100644 --- a/packages/server/src/integrations/tests/mysql.spec.ts +++ b/packages/server/src/integrations/tests/mysql.spec.ts @@ -103,7 +103,7 @@ describe("MySQL Integration", () => { ) }) - it.skip("parses strings matching a valid date format", async () => { + it("parses strings matching a valid date format", async () => { const sql = "select * from users;" await config.integration.read({ sql, diff --git a/packages/server/src/integrations/tests/oracle.spec.ts b/packages/server/src/integrations/tests/oracle.spec.ts index 8c05a881d4..80ad969e3b 100644 --- a/packages/server/src/integrations/tests/oracle.spec.ts +++ b/packages/server/src/integrations/tests/oracle.spec.ts @@ -22,11 +22,6 @@ describe("Oracle Integration", () => { config = new TestConfiguration() }) - afterEach(() => { - expect(oracledb.closeMock).toHaveBeenCalled() - expect(oracledb.closeMock).toHaveBeenCalledTimes(1) - }) - it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({ @@ -34,6 +29,7 @@ describe("Oracle Integration", () => { }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) it("calls the read method with the correct params", async () => { @@ -43,6 +39,7 @@ describe("Oracle Integration", () => { }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) it("calls the update method with the correct params", async () => { @@ -52,6 +49,7 @@ describe("Oracle Integration", () => { }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) it("calls the delete method with the correct params", async () => { @@ -61,6 +59,7 @@ describe("Oracle Integration", () => { }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) describe("no rows returned", () => { @@ -75,6 +74,7 @@ describe("Oracle Integration", () => { }) expect(response).toEqual([{ created: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) it("returns the correct response when the update response has no rows", async () => { @@ -84,6 +84,7 @@ describe("Oracle Integration", () => { }) expect(response).toEqual([{ updated: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) it("returns the correct response when the delete response has no rows", async () => { @@ -93,6 +94,7 @@ describe("Oracle Integration", () => { }) expect(response).toEqual([{ deleted: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) + expect(oracledb.closeMock).toHaveBeenCalledTimes(1) }) }) }) diff --git a/packages/server/src/integrations/tests/rest.spec.ts b/packages/server/src/integrations/tests/rest.spec.ts index bc0c0cac2f..182a6d3f13 100644 --- a/packages/server/src/integrations/tests/rest.spec.ts +++ b/packages/server/src/integrations/tests/rest.spec.ts @@ -418,9 +418,9 @@ describe("REST Integration", () => { }) // @ts-ignore const sentData = fetch.mock.calls[0][1].body - expect(sentData.has(pageParam)) + expect(sentData.has(pageParam)).toEqual(true) expect(sentData.get(pageParam)).toEqual(pageValue.toString()) - expect(sentData.has(sizeParam)) + expect(sentData.has(pageParam)).toEqual(true) expect(sentData.get(sizeParam)).toEqual(sizeValue.toString()) }) }) @@ -551,9 +551,9 @@ describe("REST Integration", () => { }) // @ts-ignore const sentData = fetch.mock.calls[0][1].body - expect(sentData.has(pageParam)) + expect(sentData.has(pageParam)).toEqual(true) expect(sentData.get(pageParam)).toEqual(pageValue.toString()) - expect(sentData.has(sizeParam)) + expect(sentData.has(pageParam)).toEqual(true) expect(sentData.get(sizeParam)).toEqual(sizeValue.toString()) expect(res.pagination.cursor).toEqual(123) }) diff --git a/packages/server/src/integrations/tests/sql.spec.ts b/packages/server/src/integrations/tests/sql.spec.ts index f4eaf2859c..e9e2b1fbb8 100644 --- a/packages/server/src/integrations/tests/sql.spec.ts +++ b/packages/server/src/integrations/tests/sql.spec.ts @@ -342,25 +342,6 @@ describe("SQL query builder", () => { }) }) - it("should use greater than when only low range specified", () => { - const date = new Date() - const query = sql._query( - generateReadJson({ - filters: { - range: { - property: { - low: date, - }, - }, - }, - }) - ) - expect(query).toEqual({ - bindings: [date, limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"`, - }) - }) - it("should use AND like expression for MS-SQL when filter is contains", () => { const query = new Sql(SqlClient.MS_SQL, 10)._query( generateReadJson({ diff --git a/packages/server/src/integrations/tests/sqlAlias.spec.ts b/packages/server/src/integrations/tests/sqlAlias.spec.ts index dd82dadac0..8bef9e59d2 100644 --- a/packages/server/src/integrations/tests/sqlAlias.spec.ts +++ b/packages/server/src/integrations/tests/sqlAlias.spec.ts @@ -294,7 +294,7 @@ describe("Captures of real examples", () => { type: "datasource", isSQL: false, }) - ) + ).toEqual(true) }) it("should disable when no fields", () => { diff --git a/packages/worker/src/api/routes/global/tests/auth.spec.ts b/packages/worker/src/api/routes/global/tests/auth.spec.ts index 3b69122ada..a81d5db435 100644 --- a/packages/worker/src/api/routes/global/tests/auth.spec.ts +++ b/packages/worker/src/api/routes/global/tests/auth.spec.ts @@ -360,7 +360,10 @@ describe("/api/global/auth", () => { const res = await config.api.configs.OIDCCallback(configId, preAuthRes) - expect(events.auth.login).toHaveBeenCalledWith("oidc", "oauth@example.com") + expect(events.auth.login).toHaveBeenCalledWith( + "oidc", + "oauth@example.com" + ) expect(events.auth.login).toHaveBeenCalledTimes(1) expect(res.status).toBe(302) const location: string = res.get("location") @@ -368,13 +371,5 @@ describe("/api/global/auth", () => { expectSetAuthCookie(res) }) }) - - // SINGLE TENANT - - describe("GET /api/global/auth/oidc/callback", () => {}) - - describe("GET /api/global/auth/oidc/callback", () => {}) - - describe("GET /api/admin/auth/oidc/callback", () => {}) }) }) diff --git a/packages/worker/src/api/routes/global/tests/workspaces.spec.ts b/packages/worker/src/api/routes/global/tests/workspaces.spec.ts deleted file mode 100644 index 1a30c6525c..0000000000 --- a/packages/worker/src/api/routes/global/tests/workspaces.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { TestConfiguration } from "../../../../tests" - -// TODO - -describe("/api/global/workspaces", () => { - const config = new TestConfiguration() - - beforeAll(async () => { - await config.beforeAll() - }) - - afterAll(async () => { - await config.afterAll() - }) - - afterEach(() => { - jest.clearAllMocks() - }) - - describe("GET /api/global/workspaces", () => { - it("retrieves workspaces", () => {}) - }) - - describe("DELETE /api/global/workspaces/:id", () => {}) - - describe("GET /api/global/workspaces", () => {}) - - describe("GET /api/global/workspaces/:id", () => {}) -})