npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-axios

v0.2.1

Published

REST API Server Mocking with Axios

Downloads

768

Readme

Build Status Code coverage Maintenance yes GitHub license Documentation status

Jest Axios

Introduction

Jest Axios is a Jest plugin that simplifies the process of mocking axios requests during testing. It fully utilizes Jest's built-in capabilities for mocking functions, and will automatically override requests made using axios throughout your application.

It does this with three main features:

  1. Easy configuration for defining functions to return data for specific endpoints and request methods.
  2. An internal database for storing data to serve via endpoint mocks.
  3. Providing tools that minimize boilerplate when mocking data for data models and collections of models.

Why is this Useful?

It can be difficult to test JavaScript packages and complex web applications that fetch data from external services. Jest provides good tools for mocking data, but those tools aren't scalable for axios specifically or easy to configure for common REST API patterns. The result is a lot of unnecessary boilerplate in Jest tests, which can cause confusion and increase the maintenance burden of a test suite. This package simplifies that process by providing users a tool to quickly and robustly spin up mocked API services that will serve real data when axios requests are made throughout an application.

Installation

Install in Project

To use this library in a project, add the package to your package dependencies via:

npm install --save jest-axios

Or, with yarn:

yarn add jest-axios

Documentation

Documentation for the project can be found here.

Quickstart

The documentation that follows details how to mock a REST API for a minimal todo list application. First, we'll define configuration for mocking requests in our application, and will then use axios in jest tests to showcase the mocked requests.

For more detailed documentation about how to define and configure mock REST API services with this library, see the Guide section of the documentation.

To start, here is the syntax for configuring a server to mock requests:

// contents of tests/index.test.js
import axios from 'axios';
import { Server } from 'jest-axios';

class App extends Server {

  data() {
    return {
      todos: [
        { name: 'foo', done: false },
        { name: 'bar', done: true },
      ],
    };
  }

  api() {
    return {
      '/todos': this.collection('todos'),
      '/todos/:id': this.model('todos'),
      '/todos/:id/complete': {
        post: id => this.db.todos.update(id, { done: true });
      },
    };
  }
}

Once we have this defined, we need to configure our tests to initialize the server and mock axios:

// contents of tests/index.test.js
...

server = App('todo-app');
jest.mock('axios');
server.init(axios);

With the server initialized, let's run some tests to show the mocked requests:

// contesnts of tests/index.test.js
...

test('api.test', async () => {

  // issue request
  const response = await axios.get('/todos');

  // check status code
  assert.equal(response.status, 200);

  // check payload
  assert.equal(response.data, [
    { id: 1, name: 'foo', done: false },  
    { id: 2, name: 'bar', done: true },
  ]);

  // issue missing request and check error
  try {
    await axios.get('/missing');
    assert.fail('Endpoint `/missing` should have returned error.');
  } catch (err) {
    assert.equal(err.status, 404);
  }

  // get specific todo item
  let todo = await axios.get('/todos/1').then(response => response.data);
  assert.equal(todo, { name: 'foo', done: false });

  // complete todo item and check again
  await axios.post('/todos/1/complete');
  todo = await axios.get('/todos/1').then(response => response.data);
  assert.equal(todo.done, true);
});

That's it! This library will also automatically mock any axios requests you make throughout your project, so you don't need to directly make axios calls inside tests to benefit from this package.

This simple example showcases only a small portion of the features available in the package. For more detailed examples and explanations, see the docs.

Contributors

Getting Started

To get started contributing to the project, simply clone the repo and setup the dependencies using yarn or npm install:

git clone [email protected]:bprinty/jest-axios.git
cd jest-axios/
yarn

Once you do that, you should be ready to write code, run tests, and edit the documentation.

Building Documentation

To develop documentation for the project, make sure you have all of the developer dependencies installed from the package.json file in the repo. Once you have all of those dependencies, you can work on the documentation locally using:

yarn docs:dev

Or, using vuepress directly:

vuepress dev docs

Running Tests

The Jest framework is used for testing this application. To run tests for the project, use:

yarn test

To have Jest automatically watch for changes to code for re-running tests in an interactive way, use:

yarn test:watch

To run or watch a specific test during development, use:

yarn test:watch -t model.update

Or, you can invoke jest directly:

jest
jest --watch
jest --watch -t model.update

Submiting Feature Requests

If you would like to see or build a new feature for the project, submit an issue in the GitHub Issue Tracker for the project. When submitting a feature request, please fully explain the context, purpose, and potential implementation for the feature, and label the ticket with the discussion label. Once the feature is approved, it will be re-labelled as feature and added to the project Roadmap.

Improving Documentation

Project documentation can always be improved. If you see typos, inconsistencies, or confusing wording in the documentation, please create an issue in the GitHub Issue Tracker with the label documentation. If you would like to fix the issue or improve the documentation, create a branch with the issue number (i.e. GH-123) and submit a PR against the master branch.

Submitting PRs

For contributors to this project, please submit improvements according to the following guidelines:

  1. Create a branch named after the ticket you're addressing. GH-1 or bp/GH-1 are examples of good branch naming.
  2. Make your changes and write tests for your changes.
  3. Run all tests locally before pushing code.
  4. Address any test failures caught by Travis CI.
  5. Make sure you've updated the documentation to reflect your changes (if applicable).
  6. Submit a PR against the master branch for the project. Provide any additional context in the PR description or comments.

Keeping up to Speed on the Project

All development efforts for the project are tracked by the project Kanban board. Contributors use that board to communicate the status of pending, in-progress, or resolved development efforts. If you have a question about the Roadmap or current in-progress issues for the project, see that board.