Skip to content

Commit

Permalink
feat: debug mode & fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 23, 2022
1 parent f160428 commit 0ba6025
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
/cjs
/esm
/dist
/types

# Random
/ignore
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {
isolatedModules: true,
useESM: true
}
}
},
setupFilesAfterEnv: ['<rootDir>/test/setup.ts'],
};
24 changes: 22 additions & 2 deletions src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,32 @@ export interface CacheInstance {
/**
* The function to parse and interpret response headers. Only used if
* cache.interpretHeader is true.
*
* @default defaultHeaderInterpreter()
*/
headerInterpreter: HeadersInterpreter;

/** The request interceptor that will be used to handle the cache. */
/**
* The request interceptor that will be used to handle the cache.
*
* @default defaultRequestInterceptor()
*/
requestInterceptor: AxiosInterceptor<CacheRequestConfig>;

/** The response interceptor that will be used to handle the cache. */
/**
* The response interceptor that will be used to handle the cache.
*
* @default defaultResponseInterceptor()
*/
responseInterceptor: AxiosInterceptor<CacheAxiosResponse>;

/**
* Logs useful information in the console
*
* **Note**: This is only available with development mode enabled
*
* @default console.log
* @see https://axios-cache-interceptor.js.org/#/pages/development-mode
*/
debug: Console['log'] | undefined;
}
1 change: 1 addition & 0 deletions src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function setupCache(
options.requestInterceptor || defaultRequestInterceptor(axiosCache);
axiosCache.responseInterceptor =
options.responseInterceptor || defaultResponseInterceptor(axiosCache);
axiosCache.debug = options.debug;

// CacheRequestConfig values
axiosCache.defaults.cache = {
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export * from './util/cache-predicate';
export * from './util/key-generator';
export * from './util/types';
export * from './util/update-cache';

/** @internal */
declare global {
/**
* Global variable defined at compile time. Use to write code that will only be executed
* at development time.
*
* @internal
*/
const __DEV__: boolean;
}

if (__DEV__) {
console.error(
'You are using a development build. Make sure to use the correct build in production'
);
console.error('https://axios-cache-interceptor.js.org/#/pages/installing');
}
16 changes: 16 additions & 0 deletions test/dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export {};

describe('tests __DEV__ usage', () => {
it('expects importing with __DEV__ true prints a warning', async () => {
expect(__DEV__).toBeTruthy();

const oldLog = console.error;
console.error = jest.fn();

await import('../src');

expect(console.error).toBeCalled();

console.error = oldLog;
});
});
5 changes: 2 additions & 3 deletions test/mocks/axios.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import Axios from 'axios';
import type { AxiosCacheInstance } from '../../src/cache/axios';
import type { CacheInstance, CacheProperties } from '../../src/cache/cache';
import { setupCache } from '../../src/cache/create';
import { CacheOptions, setupCache } from '../../src/cache/create';
import { Header } from '../../src/header/headers';

export const XMockRandom = 'x-mock-random';

export function mockAxios(
options: Partial<CacheInstance> & Partial<CacheProperties> = {},
options: CacheOptions = {},
responseHeaders: Record<string, string> = {}
): AxiosCacheInstance {
const axios = setupCache(Axios.create(), options);
Expand Down
2 changes: 2 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-expect-error __DEV__ is declared as const
global.__DEV__ = true;
2 changes: 1 addition & 1 deletion test/storage/is-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Axios } from 'axios';
import type { AxiosStorage } from '../../src';
import { isStorage } from '../../src/storage/build';
import { buildMemoryStorage } from '../../src/storage/memory';
import type { AxiosStorage } from '../../src/storage/types';
import { mockAxios } from '../mocks/axios';

it('tests isStorage function', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testCachePredicate } from '../../src';
import type { CachedStorageValue } from '../../src/storage/types';
import { testCachePredicate } from '../../src/util/cache-predicate';
import { mockAxios } from '../mocks/axios';
import { createResponse } from '../utils';

Expand Down

0 comments on commit 0ba6025

Please sign in to comment.