diff --git a/packages/requester-utils/test/unit/RequesterUtils.ts b/packages/requester-utils/test/unit/RequesterUtils.ts index 422b8c544..61ebe7890 100644 --- a/packages/requester-utils/test/unit/RequesterUtils.ts +++ b/packages/requester-utils/test/unit/RequesterUtils.ts @@ -1,5 +1,4 @@ -import * as RateLimiter from 'rate-limiter-flexible'; - +import { RateLimiterMemory } from 'rate-limiter-flexible'; import { RequestOptions, ResourceOptions, @@ -11,6 +10,8 @@ import { presetResourceArguments, } from '../../src/RequesterUtils'; +jest.mock('rate-limiter-flexible'); + const methods = ['get', 'put', 'patch', 'delete', 'post']; describe('defaultOptionsHandler', () => { @@ -194,8 +195,6 @@ describe('createInstance', () => { }); it('should pass the rate limiters to the requestHandler function', async () => { - const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterQueue'); - const testEndpoint = 'test endpoint'; const requester = createRequesterFn( optionsHandler, @@ -213,9 +212,6 @@ describe('createInstance', () => { await requester.get(testEndpoint, {}); - expect(rateLimitSpy).toHaveBeenCalledWith(10, { timeUnit: 60000 }); - expect(rateLimitSpy).toHaveBeenCalledWith(40, { timeUnit: 60000 }); - expect(requestHandler).toHaveBeenCalledWith(testEndpoint, { rateLimiters: { '*': expect.toBeFunction(), @@ -230,8 +226,6 @@ describe('createInstance', () => { describe('createRateLimiters', () => { it('should create rate limiter functions when configured', () => { - const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterQueue'); - const limiters = createRateLimiters({ '*': 40, 'projects/*/test': { @@ -240,8 +234,8 @@ describe('createRateLimiters', () => { }, }); - expect(rateLimitSpy).toHaveBeenCalledWith(10, { timeUnit: 60000 }); - expect(rateLimitSpy).toHaveBeenCalledWith(40, { timeUnit: 60000 }); + expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 10, duration: 60 }); + expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 40, duration: 60 }); expect(limiters).toStrictEqual({ '*': expect.toBeFunction(), @@ -339,11 +333,9 @@ describe('getMatchingRateLimiter', () => { }); it('should default the rateLimiters to an empty object if not passed and return the default rate of 3000 rpm', () => { - const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterMemory'); - getMatchingRateLimiter('endpoint'); - expect(rateLimitSpy).toHaveBeenCalledWith({ points: 3000, duration: 60 }); + expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 3000, duration: 60 }); }); it('should return the most specific rate limit', async () => { @@ -359,11 +351,9 @@ describe('getMatchingRateLimiter', () => { }); it('should return a default rate limit of 3000 rpm if nothing matches', () => { - const rateLimitSpy = jest.spyOn(RateLimiter, 'RateLimiterMemory'); - getMatchingRateLimiter('endpoint', { someurl: jest.fn() }); - expect(rateLimitSpy).toHaveBeenCalledWith({ points: 3000, duration: 60 }); + expect(RateLimiterMemory).toHaveBeenCalledWith({ points: 3000, duration: 60 }); }); it('should handle expanded rate limit options with a particular method and limit', async () => {