├── .babelrc ├── .github └── FUNDING.yml ├── .gitignore ├── .prettierrc ├── .travis.yml ├── README.md ├── package-lock.json ├── package.json └── src ├── index.js └── test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["@babel/preset-env", { "targets": { "node": "current" } }] 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: rwieruch 4 | patreon: # rwieruch 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with a single custom sponsorship URL 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logfile 2 | 3 | .env 4 | 5 | node_modules/ -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "trailingComma": "all", 4 | "singleQuote": true, 5 | "printWidth": 70, 6 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - stable 5 | 6 | install: 7 | - npm install 8 | 9 | script: 10 | - npm test 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to mock Axios in Jest Example 2 | 3 | [![Build Status](https://travis-ci.org/the-road-to-javascript/axios-jest-example.svg?branch=master)](https://travis-ci.org/the-road-to-javascript/axios-jest-example) [![Slack](https://slack-the-road-to-learn-react.wieruch.com/badge.svg)](https://slack-the-road-to-learn-react.wieruch.com/) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-javascript/axios-jest-example.svg)](https://greenkeeper.io/) 4 | 5 | How to mock Axios with Jest by Example. [Read more about it](https://www.robinwieruch.de/axios-jest). 6 | 7 | [![Edit axios-jest-example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/the-road-to-javascript/axios-jest-example/tree/master/?fontsize=14) 8 | 9 | ## Installation 10 | 11 | * `git clone git@github.com:the-road-to-javascript/axios-jest-example.git` 12 | * `cd axios-jest-example` 13 | * `npm install` 14 | * `npm start` 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "axios-jest-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "nodemon --exec babel-node src/index.js", 8 | "test": "jest", 9 | "test:watch": "npm run test -- --watch" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "devDependencies": { 15 | "@babel/core": "^7.2.2", 16 | "@babel/node": "^7.2.2", 17 | "@babel/preset-env": "^7.2.3", 18 | "nodemon": "^1.18.9" 19 | }, 20 | "dependencies": { 21 | "axios": "^0.19.0", 22 | "jest": "^24.9.0" 23 | } 24 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | 3 | export const API = 'https://hn.algolia.com/api/v3'; 4 | 5 | export const fetchData = async query => { 6 | const url = `${API}/search?query=${query}`; 7 | 8 | return await axios.get(url); 9 | }; 10 | 11 | fetchData('react'); 12 | -------------------------------------------------------------------------------- /src/test.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | 3 | import { fetchData, API } from './'; 4 | 5 | jest.mock('axios'); 6 | 7 | describe('fetchData', () => { 8 | it('fetches successfully data from an API', async () => { 9 | const data = { 10 | data: { 11 | hits: [ 12 | { 13 | objectID: '1', 14 | title: 'a', 15 | }, 16 | { 17 | objectID: '2', 18 | title: 'b', 19 | }, 20 | ], 21 | }, 22 | }; 23 | 24 | axios.get.mockImplementationOnce(() => Promise.resolve(data)); 25 | 26 | await expect(fetchData('react')).resolves.toEqual(data); 27 | 28 | expect(axios.get).toHaveBeenCalledWith( 29 | `${API}/search?query=react`, 30 | ); 31 | }); 32 | 33 | it('fetches erroneously data from an API', async () => { 34 | const errorMessage = 'Network Error'; 35 | 36 | axios.get.mockImplementationOnce(() => 37 | Promise.reject(new Error(errorMessage)), 38 | ); 39 | 40 | await expect(fetchData('react')).rejects.toThrow(errorMessage); 41 | }); 42 | }); 43 | --------------------------------------------------------------------------------