Coder Social home page Coder Social logo

axios-test-instance's Introduction

Axios Test Instance

Test NodeJS backends using Axios

build status codecov npm prettier jest

Table of Contents

Installation

npm install axios-test-instance

Usage

  1. Create an Axios test instance by passing your app, which may be a Koa app, an Express app, or an HTTP request handler, to setTestApp in a beforeAll or beforeEach block.
  2. Import request and use it in tests.
import { request, setTestApp } from 'axios-test-instance'

import { app } from './app.js'

beforeAll(async () => {
  await setTestApp(app)
})

The method above works with Jest, but it might not work with your testing framework. For this use case, a test instance can be created manually using createInstance.

import { createInstance } from 'axios-test-instance'

import { app } from './app.js'

let instance

beforeAll(async () => {
  instance = await createInstance(app)
})

afterAll(async () => {
  await instance.close()
})

Chances are you’re already using an Axios instance in your frontend. In this case, patchInstance can be used to patch this instance instead.

import { patchInstance } from 'axios-test-instance'

import { app } from './app.js'
import { request } from './request.js'

let instance

beforeAll(async () => {
  instance = await patchInstance(request, app)
})

afterAll(async () => {
  await instance.close()
})

Now requests made using this instance, will be redirected to the app under test.

Examples

For usages examples, have a look at our test cases:

See also

License

MIT © Remco Haszing

axios-test-instance's People

Contributors

remcohaszing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

axios-test-instance's Issues

Avoid variadic `listen` with fastify 4

Using fastify@4 with this module prints

(node:76172) [FSTDEP011] FastifyDeprecation: Variadic listen method is deprecated. Please use ".listen(optionsObject)" instead. The variadic signature will be removed in `fastify@5`.
    at Object.emit (/Users/simen/repos/monorepo/node_modules/process-warning/index.js:52:13)
    at Object.listen (/Users/simen/repos/monorepo/node_modules/fastify/lib/server.js:31:16)
    at /Users/simen/repos/monorepo/node_modules/axios-test-instance/index.js:29:17
    at new Promise (<anonymous>)
    at startServer (/Users/simen/repos/monorepo/node_modules/axios-test-instance/index.js:28:16)
    at patchInstance (/Users/simen/repos/monorepo/node_modules/axios-test-instance/index.js:90:34)
    at createInstance (/Users/simen/repos/monorepo/node_modules/axios-test-instance/index.js:126:28)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.