Skip to content

Commit

Permalink
fix(test): fixed d.ts typings;
Browse files Browse the repository at this point in the history
refactor(test): refactor typescript tests;
  • Loading branch information
DigitalBrainJS committed Dec 14, 2022
1 parent c069d26 commit a86a669
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 972 deletions.
32 changes: 20 additions & 12 deletions index.d.cts
Expand Up @@ -18,16 +18,9 @@ type AxiosHeaderGetter = ((parser?: RegExp) => RegExpExecArray | null) |

type AxiosHeaderTester = (matcher?: AxiosHeaderMatcher) => boolean;

type MaxUploadRate = number;

type MaxDownloadRate = number;

type Milliseconds = number;

declare class AxiosHeaders {
constructor(
headers?: RawAxiosHeaders | AxiosHeaders,
defaultHeaders?: RawAxiosHeaders | AxiosHeaders
headers?: RawAxiosHeaders | AxiosHeaders
);

set(headerName?: string, value?: AxiosHeaderValue, rewrite?: boolean | AxiosHeaderMatcher): AxiosHeaders;
Expand All @@ -44,12 +37,16 @@ declare class AxiosHeaders {

normalize(format: boolean): AxiosHeaders;

concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string>): AxiosHeaders;

toJSON(asStrings?: boolean): RawAxiosHeaders;

static from(thing?: AxiosHeaders | RawAxiosHeaders | string): AxiosHeaders;

static accessor(header: string | string[]): AxiosHeaders;

static concat(...targets: Array<AxiosHeaders | RawAxiosHeaders | string>): AxiosHeaders;

setContentType: AxiosHeaderSetter;
getContentType: AxiosHeaderGetter;
hasContentType: AxiosHeaderTester;
Expand Down Expand Up @@ -199,7 +196,7 @@ declare namespace axios {

type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;

type AxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders> & AxiosHeaders;
type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders;

type RawAxiosResponseHeaders = Partial<Record<string, string> & {
"set-cookie"?: string[]
Expand Down Expand Up @@ -321,6 +318,10 @@ declare namespace axios {
serialize?: CustomParamsSerializer;
}

type MaxUploadRate = number;

type MaxDownloadRate = number;

type BrowserProgressEvent = any;

interface AxiosProgressEvent {
Expand All @@ -335,20 +336,26 @@ declare namespace axios {
event?: BrowserProgressEvent;
}

type Milliseconds = number;

type AxiosAdapterName = 'xhr' | 'http' | string;

type AxiosAdapterConfig = AxiosAdapter | AxiosAdapterName;

interface AxiosRequestConfig<D = any> {
url?: string;
method?: Method | string;
baseURL?: string;
transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
headers?: RawAxiosRequestHeaders;
headers?: RawAxiosRequestHeaders | AxiosHeaders;
params?: any;
paramsSerializer?: ParamsSerializerOptions;
data?: D;
timeout?: Milliseconds;
timeoutErrorMessage?: string;
withCredentials?: boolean;
adapter?: AxiosAdapter;
adapter?: AxiosAdapterConfig | AxiosAdapterConfig[];
auth?: AxiosBasicCredentials;
responseType?: ResponseType;
responseEncoding?: responseEncoding | string;
Expand Down Expand Up @@ -396,7 +403,7 @@ declare namespace axios {
}

interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
headers?: RawAxiosRequestHeaders | Partial<HeadersDefaults>;
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
}

interface AxiosResponse<T = any, D = any> {
Expand Down Expand Up @@ -485,6 +492,7 @@ declare namespace axios {
isAxiosError<T = any, D = any>(payload: any): payload is AxiosError<T, D>;
toFormData(sourceObj: object, targetFormData?: GenericFormData, options?: FormSerializerOptions): GenericFormData;
formToJSON(form: GenericFormData|GenericHTMLFormElement): object;
AxiosHeaders: typeof AxiosHeaders;
}
}

Expand Down
7 changes: 4 additions & 3 deletions index.d.ts
Expand Up @@ -71,7 +71,7 @@ export class AxiosHeaders {

export type RawAxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders>;

export type AxiosRequestHeaders = Partial<RawAxiosHeaders & MethodsHeaders & CommonHeaders> & AxiosHeaders;
export type AxiosRequestHeaders = RawAxiosRequestHeaders & AxiosHeaders;

export type RawAxiosResponseHeaders = Partial<Record<string, string> & {
"set-cookie"?: string[]
Expand Down Expand Up @@ -289,7 +289,7 @@ export interface AxiosRequestConfig<D = any> {
baseURL?: string;
transformRequest?: AxiosRequestTransformer | AxiosRequestTransformer[];
transformResponse?: AxiosResponseTransformer | AxiosResponseTransformer[];
headers?: RawAxiosRequestHeaders;
headers?: RawAxiosRequestHeaders | AxiosHeaders;
params?: any;
paramsSerializer?: ParamsSerializerOptions;
data?: D;
Expand Down Expand Up @@ -344,7 +344,7 @@ export interface AxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'hea
}

export interface CreateAxiosDefaults<D = any> extends Omit<AxiosRequestConfig<D>, 'headers'> {
headers?: RawAxiosRequestHeaders | Partial<HeadersDefaults>;
headers?: RawAxiosRequestHeaders | AxiosHeaders | Partial<HeadersDefaults>;
}

export interface AxiosResponse<T = any, D = any> {
Expand Down Expand Up @@ -499,6 +499,7 @@ export interface AxiosStatic extends AxiosInstance {
CancelToken: CancelTokenStatic;
Axios: typeof Axios;
AxiosError: typeof AxiosError;
HttpStatusCode: typeof HttpStatusCode;
readonly VERSION: string;
isCancel: typeof isCancel;
all: typeof all;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
"type": "module",
"types": "index.d.ts",
"scripts": {
"test": "npm run test:eslint && npm run test:mocha && npm run test:karma && npm run test:exports && npm run test:dtslint",
"test": "npm run test:eslint && npm run test:mocha && npm run test:karma && npm run test:dtslint && npm run test:exports",
"test:eslint": "node bin/ssl_hotfix.js eslint lib/**/*.js",
"test:dtslint": "node bin/ssl_hotfix.js dtslint",
"test:mocha": "node bin/ssl_hotfix.js mocha test/unit/**/*.js --timeout 30000 --exit",
Expand Down
46 changes: 46 additions & 0 deletions test/module/test.js
Expand Up @@ -14,6 +14,16 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

const exec = util.promisify(cp.exec);

const spawn = (command, args) => new Promise((resolve, reject) => {
cp.spawn(command, args, {
shell : true,
stdio: 'inherit'
}).once('error', reject).on(
'close',
(code) => code ? reject(new Error(`Exit code ${code}`)) : resolve()
);
});

const {Axios} = axiosFactory;

const ignoreList = ['default'];
Expand Down Expand Up @@ -134,4 +144,40 @@ describe('module', function () {
await exec(`npm test --prefix ${pkgPath}`, {});
});
});

describe('typings', () => {
describe('ESM', ()=> {
const pkgPath = path.join(__dirname, './typings/esm');

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});

it('should pass types check', async function () {
this.timeout(30000);

await spawn(`npm test --prefix ${pkgPath}`, [], {
shell : true,
stdio: 'pipe'
});
});
});

describe('CommonJS', ()=> {
const pkgPath = path.join(__dirname, './typings/cjs');

after(async ()=> {
await remove(path.join(pkgPath, './node_modules'));
});

it('should pass types check', async function () {
this.timeout(30000);

await spawn(`npm test --prefix ${pkgPath}`, [], {
shell : true,
stdio: 'pipe'
});
});
});
});
});

0 comments on commit a86a669

Please sign in to comment.