Skip to content

Commit

Permalink
Support for ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjan1999 committed Feb 7, 2022
1 parent 95a4ccc commit 07c50e4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 37 deletions.
26 changes: 20 additions & 6 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{
"presets": [
"@babel/env",
"@babel/typescript"
]
}
{
"env": {
"esm": {
"presets": [
"@babel/typescript"
]
},
"cjs": {
"presets": [
"@babel/typescript",
[
"@babel/env",
{
"modules": "commonjs"
}
]
]
},
}
}
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ end_of_line = lf
insert_final_newline = true
indent_size = 4
indent_style = space

[*.json]
indent_style = space
indent_size = 2
18 changes: 17 additions & 1 deletion lib/mock-axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* @license @license MIT License, http://www.opensource.org/licenses/MIT
*/

import { SynchronousPromise, UnresolvedSynchronousPromise } from "synchronous-promise";
import {jest} from '@jest/globals';

import { SynchronousPromise, UnresolvedSynchronousPromise } from "synchronous-promise";
import Cancel from "./cancel/Cancel";
import CancelToken from "./cancel/CancelToken";
import {
Expand Down Expand Up @@ -67,24 +69,38 @@ const _helperReqNoData = (method: string, url: string, config?: any) => {
const MockAxios: AxiosMockType = (jest.fn(_newReq) as unknown) as AxiosMockType;

// mocking Axios methods
// @ts-ignore
MockAxios.get = jest.fn(_helperReqNoData.bind(null, "get"));
// @ts-ignore
MockAxios.post = jest.fn(_helperReq.bind(null, "post"));
// @ts-ignore
MockAxios.put = jest.fn(_helperReq.bind(null, "put"));
// @ts-ignore
MockAxios.patch = jest.fn(_helperReq.bind(null, "patch"));
// @ts-ignore
MockAxios.delete = jest.fn(_helperReqNoData.bind(null, "delete"));
// @ts-ignore
MockAxios.request = jest.fn(_newReq);
// @ts-ignore
MockAxios.all = jest.fn((values) => Promise.all(values));
// @ts-ignore
MockAxios.head = jest.fn(_helperReqNoData.bind(null, "head"));
// @ts-ignore
MockAxios.options = jest.fn(_helperReqNoData.bind(null, "options"));
// @ts-ignore
MockAxios.create = jest.fn(() => MockAxios);

MockAxios.interceptors = {
request: {
// @ts-ignore
use: jest.fn(),
// @ts-ignore
eject: jest.fn(),
},
response: {
// @ts-ignore
use: jest.fn(),
// @ts-ignore
eject: jest.fn(),
},
};
Expand Down
32 changes: 30 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
{
"name": "jest-mock-axios",
"version": "4.5.0",
"version": "4.6.0-rc1",
"description": "Axios mock for Jest",
"main": "dist/index.js",
"types": "dist/lib/index.d.ts",
"types": "./dist/lib/index.d.ts",
"type": "module",
"exports": {
"node": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/lib/index.d.ts"
},
"default": "./dist/esm/index.js"
},
"scripts": {
"clean": "rimraf ./dist",
"lint": "tslint -c tslint.json 'lib/**/*.ts' 'test/**/*.ts'",
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build": "npm run build:types && npm run build:js:esm && npm run build:js:cjs",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel lib --out-dir dist --extensions \".ts,.tsx\" --source-maps inline",
"build:js:esm": "cross-env BABEL_ENV=esm babel lib --out-dir dist/esm --extensions \".ts,.tsx\" --source-maps inline",
"build:js:cjs": "cross-env BABEL_ENV=cjs babel lib --out-dir dist/cjs --extensions \".ts,.tsx\" --source-maps inline",
"deploy2npm": "npm run build && npm publish",
"test": "jest --watch"
},
Expand All @@ -37,6 +47,7 @@
"@babel/preset-typescript": "^7.16.7",
"@types/jest": "^27.4.0",
"babel-loader": "^8.2.3",
"cross-env": "^7.0.3",
"jest": "^27.5.0",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.3",
Expand Down
45 changes: 22 additions & 23 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{
"compilerOptions": {
// module resolution strategy ('node','classic')
"moduleResolution": "node",
// ECMAScript target version ('ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT')
"target": "es2015",
// module code generation ('commonjs', 'amd', 'system', 'umd' or 'es2015')
"module": "commonjs",
"sourceMap": true,
"declaration": true,
"outDir": "./dist/",
"isolatedModules": false
},
"exclude": [
"node_modules",
"dist"
],
"include": [
"lib/*.ts",
"test",
"__mocks__"
]
}
{
"compilerOptions": {
// module resolution strategy ('node','classic')
"moduleResolution": "node",
// ECMAScript target version ('ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT')
"target": "ES2017",
"sourceMap": true,
"declaration": true,
"outDir": "./dist/",
"isolatedModules": false,
"module": "ES2020"
},
"exclude": [
"node_modules",
"dist"
],
"include": [
"lib/*.ts",
"test",
"__mocks__"
]
}

0 comments on commit 07c50e4

Please sign in to comment.