Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @jest/globals instead of global injection to test upcoming jest-mock-extended release #1013

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/app/api/auth.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/token/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Application, 'type' | 'clientSecret'>, accounts: Pick<Account, 'id'>[] };

Expand All @@ -25,7 +26,7 @@ const mockAuthorization: MockAuthorization = {
clientSecret: 'client-secret',
type: 'Public'
},
accounts: []
accounts: [],
};

describe('/api/token', () => {
Expand Down
5 changes: 0 additions & 5 deletions apps/web/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/(.*)$': '<rootDir>/components/$1',
'^@/lib/(.*)$': '<rootDir>/lib/$1',
Expand Down
9 changes: 5 additions & 4 deletions apps/web/lib/db.mock.ts
Original file line number Diff line number Diff line change
@@ -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<PrismaClient>();

jest.mock('./db', () => ({
__esModule: true,
db: mockDeep<PrismaClient>(),
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<PrismaClient>;
20 changes: 9 additions & 11 deletions apps/web/lib/oauth/to-be-oauth2-error.jest.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down Expand Up @@ -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<R> {
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<R> {
toBeOAuth2Error(code?: OAuth2ErrorCode, description?: string): R;
toThrowOAuth2Error(code?: OAuth2ErrorCode, description?: string): R;
}
}
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
107 changes: 32 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading