@@ -4,7 +4,7 @@ import { FastifyAdapter } from '@nestjs/platform-fastify';
44import type { NestFastifyApplication } from '@nestjs/platform-fastify' ;
55import { Test } from '@nestjs/testing' ;
66import { ThrottlerGuard , ThrottlerModule } from '@nestjs/throttler' ;
7- import { afterEach , describe , expect , it , vi } from 'vitest' ;
7+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
88
99const MOCK_DEFAULT_LOGIN_REQUEST_THROTTLER_LIMIT = 5 ;
1010
@@ -53,11 +53,16 @@ async function createMockApp() {
5353describe ( 'ThrottleLoginRequest' , ( ) => {
5454 let app : NestFastifyApplication ;
5555
56+ beforeEach ( ( ) => {
57+ vi . useFakeTimers ( ) ;
58+ } ) ;
59+
5660 afterEach ( async ( ) => {
5761 if ( app ) {
5862 await app . close ( ) ;
5963 }
6064 vi . resetModules ( ) ;
65+ vi . restoreAllMocks ( ) ;
6166 } ) ;
6267
6368 it ( 'should use default values when environment variables are not set' , async ( ) => {
@@ -69,6 +74,20 @@ describe('ThrottleLoginRequest', () => {
6974 }
7075 const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
7176 expect ( response . statusCode ) . toBe ( HttpStatus . TOO_MANY_REQUESTS ) ;
77+
78+ vi . advanceTimersByTime ( 40_000 ) ;
79+
80+ {
81+ const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
82+ expect ( response . statusCode ) . toBe ( HttpStatus . TOO_MANY_REQUESTS ) ;
83+ }
84+
85+ vi . advanceTimersByTime ( 40_000 ) ;
86+
87+ {
88+ const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
89+ expect ( response . statusCode ) . toBe ( HttpStatus . OK ) ;
90+ }
7291 } ) ;
7392
7493 it ( 'should use custom values from environment variables' , async ( ) => {
@@ -83,8 +102,24 @@ describe('ThrottleLoginRequest', () => {
83102 expect ( response . statusCode ) . toBe ( HttpStatus . OK ) ;
84103 }
85104
86- const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
87- expect ( response . statusCode ) . toBe ( HttpStatus . TOO_MANY_REQUESTS ) ;
105+ {
106+ const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
107+ expect ( response . statusCode ) . toBe ( HttpStatus . TOO_MANY_REQUESTS ) ;
108+ }
109+
110+ vi . advanceTimersByTime ( 15_000 ) ;
111+
112+ {
113+ const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
114+ expect ( response . statusCode ) . toBe ( HttpStatus . TOO_MANY_REQUESTS ) ;
115+ }
116+
117+ vi . advanceTimersByTime ( 20_000 ) ;
118+
119+ {
120+ const response = await app . inject ( { method : 'GET' , url : '/' } ) ;
121+ expect ( response . statusCode ) . toBe ( HttpStatus . OK ) ;
122+ }
88123
89124 vi . unstubAllEnvs ( ) ;
90125 } ) ;
0 commit comments