Skip to content

Commit 2a1cac5

Browse files
committed
fix(testing): Fixed vitest being bundled and hoisted
1 parent 8d3424f commit 2a1cac5

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

apps/nest/src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function bootstrap() {
1313
logger,
1414
});
1515

16-
AuthModule.bootstrap(app, authConfig);
16+
await AuthModule.bootstrap(app, authConfig);
1717

1818
await app.listen(3000);
1919
Logger.log(`Application is running on: http://localhost:3000`, 'Bootstrap');

packages/nest-utils/src/auth/auth.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { DynamicModule, INestApplication, Logger, Module } from '@nestjs/common';
22
import { defaultAuthOptions, type AuthOptions } from '../main';
3-
import { auth } from 'express-openid-connect';
43
import { AuthController } from './controllers/auth.controller';
54
import { AuthService } from './providers/auth.service';
65
import { deepMerge } from '@spuxx/js-utils';
@@ -51,6 +50,7 @@ export class AuthModule {
5150
Logger.warn('Authentication is disabled. All routes will be accessible.', AuthModule.name);
5251
return;
5352
}
53+
const { auth } = await import('express-openid-connect');
5454
app.use(auth(oidc));
5555
Logger.log(`Authentication is enabled and will be handled by issuer at '${oidc.issuerBaseURL}'.`, AuthModule.name);
5656
}

packages/nest-utils/src/testing/container/private/mock-oidc.ts

+9
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ export class MockOidcModule implements NestModule {
3030
consumer.apply(MockOidcMiddleware).forRoutes('*');
3131
}
3232
}
33+
34+
export function mockExpressOidcPackage() {
35+
vitest.doMock('express-openid-connect', () => {
36+
return {
37+
auth: vitest.fn(() => (_req: Request, _res: Response, next: NextFunction) => next()),
38+
requiresAuth: vitest.fn(() => (_req: Request, _res: Response, next: NextFunction) => next()),
39+
};
40+
});
41+
}

packages/nest-utils/src/testing/container/test-container.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import { INestApplication } from '@nestjs/common';
22
import { Test, TestingModule } from '@nestjs/testing';
33
import { type OmitFunctionMembers } from '@spuxx/js-utils';
4-
import { NextFunction, Request, Response } from 'express';
54
import { AuthOptions, SessionResource } from '../../auth';
65
import { AuthModule } from '../../auth/auth.module';
76
import { Supertest } from '../supertest';
87
import { createEndToEndNestApplication } from './private/end-to-end';
9-
import { MockOidcModule } from './private/mock-oidc';
8+
import { mockExpressOidcPackage, MockOidcModule } from './private/mock-oidc';
109
import { TestContainerOptions } from './types';
1110

12-
vitest.mock('express-openid-connect', () => {
13-
return {
14-
auth: vitest.fn(() => (_req: Request, _res: Response, next: NextFunction) => next()),
15-
requiresAuth: vitest.fn(() => (_req: Request, _res: Response, next: NextFunction) => next()),
16-
};
17-
});
18-
1911
/**
2012
* `TestContainer` provides an abstraction of `Nest.createTestContainer()`, offering
2113
* a custom API for easier handling and use. For more information on testing in NestJS,
@@ -84,6 +76,8 @@ export class TestContainer {
8476
...options,
8577
};
8678

79+
mockExpressOidcPackage();
80+
8781
// Auto-add conditional components
8882
if (enableEndToEnd) {
8983
imports.push(AuthModule.forRoot(authOptions as AuthOptions), MockOidcModule);
@@ -111,7 +105,7 @@ export class TestContainer {
111105
if (enableEndToEnd) {
112106
app = await createEndToEndNestApplication(module);
113107
session = { ...options.session } ?? {};
114-
AuthModule.bootstrap(app, authOptions as AuthOptions);
108+
await AuthModule.bootstrap(app, authOptions as AuthOptions);
115109
supertest = new Supertest(app, session);
116110
}
117111
// Return the test container

0 commit comments

Comments
 (0)