From 970dec5a27aed3ff5a85488b28ee169a00b59b33 Mon Sep 17 00:00:00 2001 From: darthmaim Date: Fri, 6 Sep 2024 17:20:33 +0200 Subject: [PATCH] Use `@jest/globals` instead of global injection to test upcoming `jest-mock-extended` release --- apps/web/app/api/auth.test.ts | 1 + apps/web/app/api/token/token.test.ts | 3 +- apps/web/jest.config.ts | 5 - apps/web/lib/db.mock.ts | 9 +- apps/web/lib/oauth/to-be-oauth2-error.jest.ts | 20 ++-- apps/web/package.json | 4 +- package-lock.json | 107 ++++++------------ 7 files changed, 51 insertions(+), 98 deletions(-) diff --git a/apps/web/app/api/auth.test.ts b/apps/web/app/api/auth.test.ts index a1e906a0..c6a22e9f 100644 --- a/apps/web/app/api/auth.test.ts +++ b/apps/web/app/api/auth.test.ts @@ -1,6 +1,7 @@ /** @jest-environment node */ import { Scope } from '@gw2me/client'; import { verifyScopes } from './auth'; +import { describe, expect, it } from '@jest/globals'; describe('API authorization', () => { describe('verifyScopes', () => { diff --git a/apps/web/app/api/token/token.test.ts b/apps/web/app/api/token/token.test.ts index 029360e4..e45e13de 100644 --- a/apps/web/app/api/token/token.test.ts +++ b/apps/web/app/api/token/token.test.ts @@ -5,6 +5,7 @@ import { dbMock } from '@/lib/db.mock'; import { Account, Application, Authorization } from '@gw2me/database'; import { expiresAt } from '@/lib/date'; import { Scope } from '@gw2me/client'; +import { describe, expect, it } from '@jest/globals'; type MockAuthorization = Authorization & { application: Pick, accounts: Pick[] }; @@ -25,7 +26,7 @@ const mockAuthorization: MockAuthorization = { clientSecret: 'client-secret', type: 'Public' }, - accounts: [] + accounts: [], }; describe('/api/token', () => { diff --git a/apps/web/jest.config.ts b/apps/web/jest.config.ts index 301f1d4f..9df04ec0 100644 --- a/apps/web/jest.config.ts +++ b/apps/web/jest.config.ts @@ -17,11 +17,6 @@ const config: Config = { testEnvironment: 'jest-environment-jsdom', - // required for `jest-mock-extended` - // See https://github.com/marchaos/jest-mock-extended/issues/116 - // TODO: replace with @jest/globals once that issue is fixed - injectGlobals: true, - moduleNameMapper: { '^@/components/(.*)$': '/components/$1', '^@/lib/(.*)$': '/lib/$1', diff --git a/apps/web/lib/db.mock.ts b/apps/web/lib/db.mock.ts index eedcdb65..34d44366 100644 --- a/apps/web/lib/db.mock.ts +++ b/apps/web/lib/db.mock.ts @@ -1,18 +1,19 @@ import { PrismaClient } from '@gw2me/database'; -import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended'; +import { mockDeep, mockReset } from 'jest-mock-extended'; +import { beforeEach, jest } from '@jest/globals'; -import { db } from './db'; +export const dbMock = mockDeep(); jest.mock('./db', () => ({ __esModule: true, - db: mockDeep(), + db: dbMock, })); beforeEach(() => { mockReset(dbMock); dbMock.$transaction.mockImplementation( + // @ts-expect-error any (arrayOrCallback) => typeof arrayOrCallback === 'function' ? arrayOrCallback(dbMock) : Promise.all(arrayOrCallback) ); }); -export const dbMock = db as unknown as DeepMockProxy; diff --git a/apps/web/lib/oauth/to-be-oauth2-error.jest.ts b/apps/web/lib/oauth/to-be-oauth2-error.jest.ts index 2502fb20..19c3600e 100644 --- a/apps/web/lib/oauth/to-be-oauth2-error.jest.ts +++ b/apps/web/lib/oauth/to-be-oauth2-error.jest.ts @@ -1,5 +1,6 @@ import type { MatcherFunction } from 'expect'; import { OAuth2Error, OAuth2ErrorCode } from './error'; +import { expect } from '@jest/globals'; const toBeOAuth2Error: MatcherFunction<[code?: OAuth2ErrorCode, description?: string]> = function (actual, code, description) { if(!(actual instanceof OAuth2Error)) { @@ -32,16 +33,13 @@ const toThrowOAuth2Error: MatcherFunction<[code?: OAuth2ErrorCode, description?: expect.extend({ toBeOAuth2Error, toThrowOAuth2Error }); -declare global { - // eslint-disable-next-line @typescript-eslint/no-namespace - namespace jest { - interface AsymmetricMatchers { - toBeOAuth2Error(code?: OAuth2ErrorCode, description?: string): void; - toThrowOAuth2Error(code?: OAuth2ErrorCode, description?: string): void; - } - interface Matchers { - toBeOAuth2Error(code?: OAuth2ErrorCode, description?: string): R; - toThrowOAuth2Error(code?: OAuth2ErrorCode, description?: string): R; - } +declare module 'expect' { + interface AsymmetricMatchers { + toBeOAuth2Error(code?: OAuth2ErrorCode, description?: string): void; + toThrowOAuth2Error(code?: OAuth2ErrorCode, description?: string): void; + } + interface Matchers { + toBeOAuth2Error(code?: OAuth2ErrorCode, description?: string): R; + toThrowOAuth2Error(code?: OAuth2ErrorCode, description?: string): R; } } diff --git a/apps/web/package.json b/apps/web/package.json index 970adbd4..40de1898 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -31,17 +31,17 @@ "@gw2treasures/eslint-config": "0.0.4", "@gw2treasures/eslint-plugin-nextjs": "0.0.2", "@gw2treasures/tsconfig": "0.0.1", + "@jest/globals": "29.7.0", "@simplewebauthn/types": "10.0.0", "@testing-library/jest-dom": "6.5.0", "@testing-library/react": "16.0.1", - "@types/jest": "29.5.12", "@types/node": "20.16.5", "@types/react": "18.3.5", "eslint": "8.57.0", "eslint-config-next": "14.2.8", "jest": "29.7.0", "jest-environment-jsdom": "29.7.0", - "jest-mock-extended": "3.0.7", + "jest-mock-extended": "https://github.com/macko911/jest-mock-extended.git#stop-using-jest-globals", "ts-node": "10.9.2", "typescript": "5.5.4" } diff --git a/package-lock.json b/package-lock.json index 57e1e5ef..8909c2be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -90,21 +90,50 @@ "@gw2treasures/eslint-config": "0.0.4", "@gw2treasures/eslint-plugin-nextjs": "0.0.2", "@gw2treasures/tsconfig": "0.0.1", + "@jest/globals": "29.7.0", "@simplewebauthn/types": "10.0.0", "@testing-library/jest-dom": "6.5.0", "@testing-library/react": "16.0.1", - "@types/jest": "29.5.12", "@types/node": "20.16.5", "@types/react": "18.3.5", "eslint": "8.57.0", "eslint-config-next": "14.2.8", "jest": "29.7.0", "jest-environment-jsdom": "29.7.0", - "jest-mock-extended": "3.0.7", + "jest-mock-extended": "https://github.com/macko911/jest-mock-extended.git#stop-using-jest-globals", "ts-node": "10.9.2", "typescript": "5.5.4" } }, + "apps/web/node_modules/jest-mock-extended": { + "version": "3.0.7", + "resolved": "git+ssh://git@github.com/macko911/jest-mock-extended.git#fd463abadb20bf593ae4d6023e3ceb76204bac71", + "dev": true, + "license": "MIT", + "dependencies": { + "ts-essentials": "^10.0.2" + }, + "peerDependencies": { + "@jest/globals": "^28.0.0 || ^29.0.0", + "jest": "^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0", + "typescript": "^3.0.0 || ^4.0.0 || ^5.0.0" + } + }, + "apps/web/node_modules/ts-essentials": { + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-10.0.2.tgz", + "integrity": "sha512-Xwag0TULqriaugXqVdDiGZ5wuZpqABZlpwQ2Ho4GDyiu/R2Xjkp/9+zcFxL7uzeLl/QCPrflnvpVYyS3ouT7Zw==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "typescript": ">=4.5.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, "node_modules/@aashutoshrathi/word-wrap": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz", @@ -2164,6 +2193,7 @@ "resolved": "https://registry.npmjs.org/@jest/globals/-/globals-29.7.0.tgz", "integrity": "sha512-mpiz3dutLbkW2MNFubUGUEVLkTGiqW6yLVTA+JbP6fI6J5iL9Y0Nlg8k95pcF8ctKwCS7WVxteBs29hhfAotzQ==", "dev": true, + "license": "MIT", "dependencies": { "@jest/environment": "^29.7.0", "@jest/expect": "^29.7.0", @@ -3541,52 +3571,6 @@ "@types/istanbul-lib-report": "*" } }, - "node_modules/@types/jest": { - "version": "29.5.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", - "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", - "dev": true, - "license": "MIT", - "dependencies": { - "expect": "^29.0.0", - "pretty-format": "^29.0.0" - } - }, - "node_modules/@types/jest/node_modules/ansi-styles": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-5.2.0.tgz", - "integrity": "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, - "node_modules/@types/jest/node_modules/pretty-format": { - "version": "29.7.0", - "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", - "integrity": "sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@jest/schemas": "^29.6.3", - "ansi-styles": "^5.0.0", - "react-is": "^18.0.0" - }, - "engines": { - "node": "^14.15.0 || ^16.10.0 || >=18.0.0" - } - }, - "node_modules/@types/jest/node_modules/react-is": { - "version": "18.3.1", - "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", - "integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==", - "dev": true, - "license": "MIT" - }, "node_modules/@types/jsdom": { "version": "20.0.1", "resolved": "https://registry.npmjs.org/@types/jsdom/-/jsdom-20.0.1.tgz", @@ -8704,19 +8688,6 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, - "node_modules/jest-mock-extended": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/jest-mock-extended/-/jest-mock-extended-3.0.7.tgz", - "integrity": "sha512-7lsKdLFcW9B9l5NzZ66S/yTQ9k8rFtnwYdCNuRU/81fqDWicNDVhitTSPnrGmNeNm0xyw0JHexEOShrIKRCIRQ==", - "dev": true, - "dependencies": { - "ts-essentials": "^10.0.0" - }, - "peerDependencies": { - "jest": "^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0", - "typescript": "^3.0.0 || ^4.0.0 || ^5.0.0" - } - }, "node_modules/jest-pnp-resolver": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", @@ -11914,20 +11885,6 @@ "typescript": ">=4.2.0" } }, - "node_modules/ts-essentials": { - "version": "10.0.1", - "resolved": "https://registry.npmjs.org/ts-essentials/-/ts-essentials-10.0.1.tgz", - "integrity": "sha512-HPH+H2bkkO8FkMDau+hFvv7KYozzned9Zr1Urn7rRPXMF4mZmCKOq+u4AI1AAW+2bofIOXTuSdKo9drQuni2dQ==", - "dev": true, - "peerDependencies": { - "typescript": ">=4.5.0" - }, - "peerDependenciesMeta": { - "typescript": { - "optional": true - } - } - }, "node_modules/ts-interface-checker": { "version": "0.1.13", "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz",