From eefc98c6c014ac53d507b9228231cb76855ff376 Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Thu, 16 Nov 2023 03:33:24 -0300 Subject: [PATCH] perf: faster tests --- test/interceptors/request.test.ts | 9 ++++----- test/interceptors/response.test.ts | 2 +- test/storage/quota.ts | 23 +++++++++++++++-------- test/util/key-generator.test.ts | 21 ++++++++++++--------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/test/interceptors/request.test.ts b/test/interceptors/request.test.ts index fa1252b..df1854c 100644 --- a/test/interceptors/request.test.ts +++ b/test/interceptors/request.test.ts @@ -232,7 +232,7 @@ describe('Request Interceptor', () => { // Simple adapter that resolves after the deferred is completed. adapter: async (config: InternalCacheRequestConfig) => { - await setTimeout(150); + await setTimeout(10); const response = (await (axios.defaults.adapter as AxiosAdapter)(config)) as AxiosResponse; @@ -248,7 +248,7 @@ describe('Request Interceptor', () => { // Leading to test the intermediate loading state. { - await setTimeout(50); + await setTimeout(5); const c2 = (await axios.storage.get(id)) as LoadingStorageValue; @@ -288,8 +288,7 @@ describe('Request Interceptor', () => { // Simple adapter that resolves after the deferred is completed. adapter: async (config: InternalCacheRequestConfig) => { - await setTimeout(150); - + await setTimeout(10); return (axios.defaults.adapter as AxiosAdapter)(config); } }); @@ -299,7 +298,7 @@ describe('Request Interceptor', () => { // Leading to test the intermediate loading state. { - await setTimeout(50); + await setTimeout(5); const c2 = (await axios.storage.get(id)) as LoadingStorageValue; diff --git a/test/interceptors/response.test.ts b/test/interceptors/response.test.ts index 87a502f..28587d8 100644 --- a/test/interceptors/response.test.ts +++ b/test/interceptors/response.test.ts @@ -197,7 +197,7 @@ describe('Response Interceptor', () => { process.nextTick(() => { res(173); }); - }, 50); + }, 20); }); } } diff --git a/test/storage/quota.ts b/test/storage/quota.ts index 3062eef..6548efa 100644 --- a/test/storage/quota.ts +++ b/test/storage/quota.ts @@ -4,18 +4,25 @@ import { buildWebStorage } from '../../src/storage/web-api'; import { mockAxios } from '../mocks/axios'; import { EMPTY_RESPONSE } from '../utils'; +const MAXIMUM_LIMIT = 5_000_000; + +const MAXIMUM_0 = '0'.repeat(MAXIMUM_LIMIT); +const MAXIMUM_20_0 = '0'.repeat(MAXIMUM_LIMIT * 0.2); +const MAXIMUM_90_0 = '0'.repeat(MAXIMUM_LIMIT * 0.9); + export function testStorageQuota(name: string, storage: Storage): void { - const MAXIMUM_LIMIT = 5_000_000; + + it(`${name} has storage limit`, () => { assert.ok(storage); assert.doesNotThrow(() => { - storage.setItem('key', '0'.repeat(MAXIMUM_LIMIT * 0.9)); + storage.setItem('key', MAXIMUM_90_0); }); assert.throws(() => { - storage.setItem('key', '0'.repeat(MAXIMUM_LIMIT)); + storage.setItem('key', MAXIMUM_0); }); }); @@ -35,7 +42,7 @@ export function testStorageQuota(name: string, storage: Storage): void { state: 'cached', createdAt: Date.now(), ttl: 60_000, - data: { ...EMPTY_RESPONSE, data: '0'.repeat(MAXIMUM_LIMIT) } + data: { ...EMPTY_RESPONSE, data: MAXIMUM_0 } }); // Too big for this storage save @@ -58,7 +65,7 @@ export function testStorageQuota(name: string, storage: Storage): void { ttl: 60_000, data: { ...EMPTY_RESPONSE, - data: '0'.repeat(MAXIMUM_LIMIT * 0.2) // 20% each + data: MAXIMUM_20_0 // 20% each } }); } @@ -69,7 +76,7 @@ export function testStorageQuota(name: string, storage: Storage): void { ttl: 60_000, data: { ...EMPTY_RESPONSE, - data: '0'.repeat(MAXIMUM_LIMIT * 0.9) // 90% + data: MAXIMUM_90_0// 90% } }); @@ -104,7 +111,7 @@ export function testStorageQuota(name: string, storage: Storage): void { ttl: i * 10_000, data: { ...EMPTY_RESPONSE, - data: '0'.repeat(MAXIMUM_LIMIT * 0.2) // 20% each + data: MAXIMUM_20_0 // 20% each } }); } @@ -115,7 +122,7 @@ export function testStorageQuota(name: string, storage: Storage): void { ttl: 10_000, data: { ...EMPTY_RESPONSE, - data: '0'.repeat(MAXIMUM_LIMIT * 0.9) // 90% + data: MAXIMUM_90_0 // 90% } }); diff --git a/test/util/key-generator.test.ts b/test/util/key-generator.test.ts index c0dff39..b60ce03 100644 --- a/test/util/key-generator.test.ts +++ b/test/util/key-generator.test.ts @@ -101,7 +101,10 @@ describe('KeyGeneration', () => { ]; for (const [first, second] of groups) { - assert.equal(defaultKeyGenerator({ url: first }), defaultKeyGenerator({ url: second })); + assert.equal( + defaultKeyGenerator({ url: first }), + defaultKeyGenerator({ url: second }) + ); assert.equal( defaultKeyGenerator({ baseURL: first }), @@ -140,28 +143,28 @@ describe('KeyGeneration', () => { }); it('BuildKeyGenerator & `hash: false`', async () => { - const keyGenerator = buildKeyGenerator(({ headers }) => - String(headers?.['x-req-header'] || 'not-set') - ); - - const axios = mockAxios({ generateKey: keyGenerator }); + const axios = mockAxios({ + generateKey: buildKeyGenerator(({ headers }) => + String(headers?.['x-req-header'] || 'not-set') + ) + }); const { id } = await axios.get('random-url', { - data: Math.random(), + data: 1, headers: { 'x-req-header': 'my-custom-id' } }); const { id: id2 } = await axios.get('other-url', { - data: Math.random() * 2, + data: 2, headers: { 'x-req-header': 'my-custom-id' } }); const { id: id3 } = await axios.get('other-url', { - data: Math.random() * 2 + data: 3 }); assert.equal(id, 'my-custom-id');