Code Monkey home page Code Monkey logo

jest-mock-express's Introduction

Express Mock Jest

This library simulates a http request to load express modules without start the application, working with Jest.

MockRouter

This mock receives the express router exported module. The MockRouter object has all http methods (get, post, put, delete), you can execute requests without start the entire application.

import { MockRouter } from "@carlosrpj/jest-mock-express";
import router from "../../src/routes/api";

const mockRouter = new MockRouter(router);

it("should return a user list", async () => {
  await mockRouter.get("/users");
  expect(mockRouter.res.send).toBeCalledWith([
    { name: "Foo", email: "[email protected]" },
  ]);
});

MockRouter.headers

You can define your request headers with headers function, simulating a http request. This function replace all actual headers.

await mockRouter
  .headers({
    Authorization: "Bearer TOKEN",
    "Content-Type": "application/json",
  })
  .get("/users");

MockRouter.header

Adding a header to request.

await mockRouter.header("Authorization", "Bearer TOKEN").get("/users");

MockRouter.params

Adding many params. This function replace all actual params.

await mockRouter.params({ id: 1 }).get("/users/1");

MockRouter.param

Adding a param to request.

await mockRouter.param("id", 1).get("/users/1");

MockRouter.query

Adding a query to request.
You can use a object with many queries, or a single key/value.

// many queries
await mockRouter.query({ id: 1 }).get("/users/1");
// single key/value
await mockRouter.query("name", "foo").get("/users/1");

MockRouter.body

Adding a body to request.

await mockRouter
  .body({ name: "Foo", email: "[email protected]" })
  .post("/users");

jest-mock-express's People

Contributors

carlosroberto555 avatar

Watchers

 avatar  avatar

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.