All Projects → jameswlane → Jest Express

jameswlane / Jest Express

Licence: mit
Mock Express for testing with Jest

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Jest Express

Nodebestpractices
✅ The Node.js best practices list (December 2021)
Stars: ✭ 72,734 (+51484.4%)
Mutual labels:  express, jest
Javascript Testing Best Practices
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (August 2021)
Stars: ✭ 13,976 (+9812.06%)
Mutual labels:  express, jest
Express Babel
Express starter kit with ES2017+ support, testing, linting, and code coverage
Stars: ✭ 621 (+340.43%)
Mutual labels:  express, jest
Express Graphql Boilerplate
Express GraphQL API with JWT Authentication and support for sqlite, mysql, and postgresql
Stars: ✭ 201 (+42.55%)
Mutual labels:  express, jest
Mostly
They mostly come at night; mostly.
Stars: ✭ 78 (-44.68%)
Mutual labels:  express, jest
Typescript Express Starter
🚀 TypeScript Express Starter
Stars: ✭ 238 (+68.79%)
Mutual labels:  express, jest
Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (-77.3%)
Mutual labels:  express, jest
Express Webpack React Redux Typescript Boilerplate
🎉 A full-stack boilerplate that using express with webpack, react and typescirpt!
Stars: ✭ 156 (+10.64%)
Mutual labels:  express, jest
Node Typescript Boilerplate
Minimalistic project template to jump start a Node.js back-end application in TypeScript. ESLint, Jest and type definitions included.
Stars: ✭ 1,061 (+652.48%)
Mutual labels:  express, jest
Express Bookshelf Realworld Example App
🐳 An Express and Bookshelf based backend implementation of the RealWorld API Spec.
Stars: ✭ 45 (-68.09%)
Mutual labels:  express, jest
Nxplorerjs Microservice Starter
Node JS , Typescript , Express based reactive microservice starter project for REST and GraphQL APIs
Stars: ✭ 193 (+36.88%)
Mutual labels:  express, jest
Express App Testing Demo
a simple express app for demonstrating testing and code coverage
Stars: ✭ 89 (-36.88%)
Mutual labels:  express, jest
Blog Service
blog service @nestjs
Stars: ✭ 188 (+33.33%)
Mutual labels:  express, jest
React
Extremely simple boilerplate, easiest you can find, for React application including all the necessary tools: Flow | React 16 | redux | babel 6 | webpack 3 | css-modules | jest | enzyme | express + optional: sass/scss
Stars: ✭ 244 (+73.05%)
Mutual labels:  express, jest
2019 12
🎟 급증하는 트래픽에도 안정적인 예약 서비스, Atomic Pattern을 적용한 재사용 가능한 컴포넌트, 실용적인 Testing을 주제로 하는 이벤트 서비스
Stars: ✭ 169 (+19.86%)
Mutual labels:  express, jest
Node Express Boilerplate
A boilerplate for building production-ready RESTful APIs using Node.js, Express, and Mongoose
Stars: ✭ 890 (+531.21%)
Mutual labels:  express, jest
Js Stack Boilerplate
Final boilerplate code of the JavaScript Stack from Scratch tutorial –
Stars: ✭ 145 (+2.84%)
Mutual labels:  express, jest
Push Starter
React Redux Starter with SSR 🤖
Stars: ✭ 43 (-69.5%)
Mutual labels:  express, jest
Fullstack Shopping Cart
MERN stack shopping cart, written in TypeScript
Stars: ✭ 82 (-41.84%)
Mutual labels:  express, jest
Reeakt
A modern React boilerplate to awesome web applications
Stars: ✭ 116 (-17.73%)
Mutual labels:  express, jest

Jest Express

All Contributors CircleCI dependencies Status Maintainability Test Coverage codecov devDependencies Status Downloads/week

Mock Express for testing with Jest

Other

  1. Development
  2. Contributing
  3. License

express()

How to setup Application to use in Jest:

jest.mock('express', () => {
  return require('jest-express');
});

express.json()

Ways to use this API:

expect(express.json).toHaveBeenCalledWith([options]);

express.static()

Ways to use this API:

expect(express.static).toHaveBeenCalledWith(root, [options]);

express.Router()

Ways to use this API:

expect(express.Router).toHaveBeenCalledWith([options]);

express.urlencoded()

Ways to use this API:

expect(express.urlencoded).toHaveBeenCalledWith([options]);

Application

How to setup Application to use in Jest:

import { Express } from 'jest-express/lib/express';
import { server } from '../src/server.js';

let app;

describe('Server', () => {
  beforeEach(() => {
    app = new Express();
  });

  afterEach(() => {
    app.resetMocked();
  });

  test('should setup server', () => {
    const options = {
      port: 3000,
    };

    server(app, options);

    expect(app.set).toBeCalledWith('port', options.port);
  });
});

app.locals

Ways to use this API:

Setup:

beforeEach(() => {
  app = new Express();
  app.setLocals('title', 'My App');
});

app.mountpath

Ways to use this API:

Setup:

beforeEach(() => {
  app = new Express();
  app.setMountPath('/admin');
});

app.all()

Ways to use this API:

expect(app.all).toBeCalledWith(path, callback [, callback ...]);

app.get()

Ways to use this API:

expect(app.get).toBeCalledWith(path, callback [, callback ...]);

app.head()

Ways to use this API:

expect(app.head).toBeCalledWith(path, callback [, callback ...]);

app.post()

Ways to use this API:

expect(app.post).toBeCalledWith(path, callback [, callback ...]);

app.put()

Ways to use this API:

expect(app.put).toBeCalledWith(path, callback [, callback ...]);

app.delete()

Ways to use this API:

expect(app.delete).toBeCalledWith(path, callback [, callback ...]);

app.connect()

Ways to use this API:

expect(app.connect).toBeCalledWith(path, callback [, callback ...]);

app.options()

Ways to use this API:

expect(app.options).toBeCalledWith(path, callback [, callback ...]);

app.trace()

Ways to use this API:

expect(app.trace).toBeCalledWith(path, callback [, callback ...]);

app.patch()

Ways to use this API:

expect(app.patch).toBeCalledWith(path, callback [, callback ...]);

app.param()

Ways to use this API:

expect(app.param).toBeCalledWith([name], callback);

app.render()

Ways to use this API:

expect(app.param).toBeCalledWith(view, [locals], callback);

app.use()

Ways to use this API:

expect(app.use).toBeCalledWith([path,] callback [, callback...]);

Request

How to setup Request to use in Jest:

import { Request } from 'jest-express/lib/request';
import { endpoint } from '../src/endpoint.js';

let request;

describe('Endpoint', () => {
  beforeEach(() => {
    request = new Request('/users?sort=desc', {
      headers: {
        Accept: 'text/html'
      }
    });
  });

  afterEach(() => {
    request.resetMocked();
  });

  test('should setup endpoint', () => {
    endpoint(request);

    expect(request).toBeCalled();
  });
});

request.baseUrl

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setBaseUrl(baseUrl);
});

request.body

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setBody(body);
});

request.cookies

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setCookies(cookies);
});

request.fresh

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setFresh(boolean);
});

request.hostname

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setHostname(string);
});

request.setHeaders

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setHeaders("X-Custom-Header", "foo");
});

// or

beforeEach(() => {
  request = new Request();
  request.setHeaders({  
    "X-Custom-Header", "foo",
    "X-Custom-Header-2", "bar"
  });
});

request.ip

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setIp(ip);
});

request.ips

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setIps(ips);
});

request.method

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setMethod(method);
});

request.originalUrl

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setOriginalUrl(originalUrl);
});

request.params

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setParams(params);
});

request.path

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setPath(path);
});

request.protocol

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setProtocol(protocol);
});

request.query

You can use it by passing key value pair:

Setup:

beforeEach(() => {
  request = new Request();
  request.setQuery('Accept', 'text/html');
});

Or by passing an object:

beforeEach(() => {
  request = new Request();
  request.setQuery({ 'Accept': 'text/html', 'Accept-Language': 'en' });
});

request.route

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setRoute(route);
});

request.secure

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setSecure(secure);
});

request.signedCookies

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setSignedCookies(signedCookies);
});

request.stale

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setStale(boolean);
});

request.subdomains

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setSubdomains(subdomains);
});

request.xhr

Ways to use this API:

Setup:

beforeEach(() => {
  request = new Request();
  request.setXhr(boolean);
});

request.accepts()

Ways to use this API:

expect(request.accepts).toBeCalledWith(types);

request.acceptsCharsets()

Ways to use this API:

expect(request.acceptsCharsets).toBeCalledWith(charset [, ...]);

request.acceptsEncodings()

Ways to use this API:

expect(request.acceptsEncodings).toBeCalledWith(encoding [, ...]);

request.acceptsLanguages()

Ways to use this API:

expect(request.acceptsLanguages).toBeCalledWith(lang [, ...]);

request.get()

Ways to use this API:

expect(request.get).toBeCalledWith(field);

request.is()

Ways to use this API:

expect(request.is).toBeCalledWith(type);

request.param()

Ways to use this API:

expect(request.param).toBeCalledWith(name [, defaultValue]);

request.range()

Ways to use this API:

expect(request.range).toBeCalledWith(size[, options]);

Response

How to setup Response to use in Jest:

import { Response } from 'jest-express/lib/response';
import { endpoint } from '../src/endpoint.js';

let response;

describe('Endpoint', () => {
  beforeEach(() => {
    response = new Response();
  });

  afterEach(() => {
    response.resetMocked();
  });

  test('should setup endpoint', () => {
    endpoint(response);

    expect(response).toBeCalled();
  });
});

response.setHeader

Ways to use this API:

Setup:

beforeEach(() => {
  response = new Response();
  response.setHeader(key, value);
  expect(response.setHeader).toBeCalledWith(key, value);
});

response.removeHeader

Ways to use this API:

Setup:

beforeEach(() => {
  response = new Response();
  response.removeHeader(key);
  expect(response.removeHeader).toBeCalledWith(key);
});

response.headersSent

Ways to use this API:

Setup:

beforeEach(() => {
  response = new Response();
  response.setHeadersSent(boolean);
});

response.locals

Ways to use this API:

Setup:

beforeEach(() => {
  response = new Response();
  response.setLocals('title', 'My App');
});

response.append()

Ways to use this API:

expect(response.append).toBeCalledWith(field [, value]);

response.attachment()

Ways to use this API:

expect(response.attachment).toBeCalledWith([filename]);

reponse.body

Ways to use this API:

expect(response.body).toEqual(value);

response.cookie()

Ways to use this API:

expect(response.cookie).toBeCalledWith(name, value [, options]);

response.clearCookie()

Ways to use this API:

expect(response.clearCookie).toBeCalledWith(name [, options]);

response.download()

Ways to use this API:

expect(response.download).toBeCalledWith(path [, filename] [, options] [, fn]);

response.end()

Ways to use this API:

expect(response.end).toBeCalledWith([data] [, encoding]);

response.format()

Ways to use this API:

expect(response.format).toBeCalledWith(object);

response.get()

Ways to use this API:

expect(response.get).toBeCalledWith(field);

response.getHeader()

Ways to use this API:

response.setHeader('Accept', 'text/html')
expect(response.getHeader('Accept')).toEqual('text/html');

response.header()

An alias for response.set()

expect(response.header).toBeCalledWith(field, [value]);

response.json()

Ways to use this API:

expect(response.json).toBeCalledWith([body]);

response.jsonp()

Ways to use this API:

expect(response.jsonp).toBeCalledWith([body]);

response.links()

Ways to use this API:

expect(response.links).toBeCalledWith(links);

response.location()

Ways to use this API:

expect(response.location).toBeCalledWith(path);

response.redirect()

Ways to use this API:

expect(response.redirect).toBeCalledWith([status,] path);

response.render()

Ways to use this API:

expect(response.render).toBeCalledWith(view [, locals] [, callback]);

response.send()

Ways to use this API:

expect(response.send).toBeCalledWith([body]);

response.sendFile()

Ways to use this API:

expect(response.sendFile).toBeCalledWith(path [, options] [, fn]);

response.sendStatus()

Ways to use this API:

expect(response.sendStatus).toBeCalledWith(statusCode);

response.set()

Sets headers. It is calling response.setHeader() internally.

expect(response.set).toBeCalledWith(field [, value]);

response.status()

Ways to use this API:

expect(response.status).toBeCalledWith(code);

response.statusCode

ways to use this API:

expect(response.statusCode).toEqual(code);

response.type()

Ways to use this API:

expect(response.type).toBeCalledWith(type);

response.vary()

Ways to use this API:

expect(response.vary).toBeCalledWith(field);

Router

How to setup Response to use in Jest:

import { Router } from 'jest-express/lib/router';
import { endpoint } from '../src/endpoint.js';

let router;

describe('Endpoint', () => {
  beforeEach(() => {
    router = new Router();
  });

  afterEach(() => {
    router.resetMocked();
  });

  test('should setup endpoint', () => {
    endpoint(router);

    expect(router).toBeCalled();
  });
});

router.all()

Ways to use this API:

expect(router.all).toBeCalledWith(path, [callback, ...] callback);

router.get()

Ways to use this API:

expect(router.get).toBeCalledWith(path, [callback, ...] callback);

router.param()

Ways to use this API:

expect(router.param).toBeCalledWith(name, callback);

router.route()

Ways to use this API:

expect(router.route).toBeCalledWith(path);

router.use()

Ways to use this API:

expect(router.use).toBeCalledWith([path], [function, ...] function);

resetMocked()

Resets all information stored in the mock, including any initial implementation and mock name given.

This is useful when you want to completely restore a mock back to its initial state.

Development

Setup

$ git clone [email protected]:jameswlane/jest-express.git
$ cd jest-express
$ npm install

Tests

Linters:

$ npm run tslint

Tests:

$ npm test

Contributing

License

Contributors

Thanks goes to these wonderful people (emoji key):


James W. Lane III

💻 📖 🚇 ⚠️ 🔧

Adam Stankiewicz

🐛 💻 📖 ⚠️ 💡

Garen Torikian

💻 ⚠️

Konstantin Azizov

📖

dozenne

💻 ⚠️

László Székely-Tóth

💻 ⚠️

mattmarcello

💻 ⚠️

Harshith Keni

💻

Max Holman

💻 ⚠️

Matthew Alsup

💻

ttxndrx

💻

Ben Bakhar

💻 ⚠️ 📖

Ronald J Kimball

💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].