perf: faster tests

This commit is contained in:
Arthur Fiorette 2023-11-16 03:33:24 -03:00
parent ecbc27e466
commit eefc98c6c0
No known key found for this signature in database
GPG Key ID: 79FA8EC214FA0233
4 changed files with 32 additions and 23 deletions

View File

@ -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;

View File

@ -197,7 +197,7 @@ describe('Response Interceptor', () => {
process.nextTick(() => {
res(173);
});
}, 50);
}, 20);
});
}
}

View File

@ -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%
}
});

View File

@ -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');