Skip to content

Commit

Permalink
perf: faster tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Nov 16, 2023
1 parent ecbc27e commit eefc98c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
9 changes: 4 additions & 5 deletions test/interceptors/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down Expand Up @@ -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);
}
});
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/interceptors/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('Response Interceptor', () => {
process.nextTick(() => {
res(173);
});
}, 50);
}, 20);
});
}
}
Expand Down
23 changes: 15 additions & 8 deletions test/storage/quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

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

Expand Down Expand Up @@ -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
}
});
}
Expand All @@ -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%
}
});

Expand Down
21 changes: 12 additions & 9 deletions test/util/key-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit eefc98c

Please sign in to comment.