当前位置: 首页 > 软件库 > Web应用开发 > >

jest-express

Mock Express for testing with Jest
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 商鸿哲
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Jest Express

All Contributors

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 git@github.com: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!

  • //模拟后台 let express = require ('express'); let app=express(); app.listen(3000); //以下代码就是解决跨域问题 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "http://localhost:80

  • Jest 单元测试 Jest是 Facebook 的一套开源的 JavaScript 测试框架, 它自动集成了断言、JSDom、覆盖率报告等开发者所需要的所有测试工具。可以在REACT或VUE前端项目中使用。 1、 安装 npm i -D jest vue-jest babel-jest @vue/test-utils npm i babel-core@^7.0.0-0 eslint-plugin

  • Mock函数的作用 在项目中,一个模块的方法内常常会去调用另外一个模块的方法。在单元测试中,我们可能并不需要关心内部调用的方法的执行过程和结果,只想知道它是否被正确调用即可,甚至会指定该函数的返回值。此时,使用Mock函数是十分有必要。 Mock函数提供的以下三种特性,在我们写测试代码时十分有用: 捕获函数调用情况 设置函数返回值 改变函数的内部实现 1. jest.fn() jest.fn()是

  • toBe 使用 Object.is 判断是否严格相等。 toEqual 递归检查对象或数组的每个字段。 toBeNull 只匹配null。 toBeUndefined 只匹配 undefined。 toBeDefined 只匹配非 undefined。 toBeTruthy只匹配真。 toBeFalsy 只匹配假。 toBeGreaterThan 实际值大于期望。 toBeGreaterThanO

  • 有很多NodeJS测试框架,其中两种是: Jest 和 Mocha,都可以用来实现NodeJS自动化测试。 Jest 安装: npm i jest --save-dev 因为是测试用,production环境不需用到,所以安装选项有加 --save-dev. 修改 package.json, 添加"test" key:value 对 "scripts": { "start": "node

  • 官方地址:https://github.com/roughsoft/jest-html-reporters 注意,是jest-html-reporters,不是jest-html-reporter 一、直接安装 npm install jest-html-reporters 二、在jest.config.js中添加配置文件 const moment = require('moment'); con

  • 问题描述 单元测试跑通了,但是去访问coverage测试报告时,没有代码覆盖率,控制台提示如下: Handlebars: Access has been denied to resolve the property "statements" because it is not an "own property" of its parent. You can add a runtime option

  • function getConfig() { return { doman: 'localhost', port: 8080, time:Date.now() } } test('测试快照', () => { //第一次会保存快照,以后就会对比快照,命令行工具可以更新快照 expect(getConfig()).toMatc

 相关资料
  • Jest 可帮你实现无痛的 JavaScript 单元测试支持,由 Facebook 推出。 测试代码: // __tests__/sum-test.jsjest.dontMock('../sum');describe('sum', function() { it('adds 1 + 2 to equal 3', function() {   var sum = require('../sum')

  • Jest's configuration can be defined in the package.json file of your project, or through a jest.config.js file or through the --config <path/to/file.js|cjs|mjs|json> option. If you'd like to use your

  • The community around Jest is working hard to make the testing experience even greater. jest-community is a new GitHub organization for high quality Jest additions curated by Jest maintainers and colla

  • You can cherry pick specific features of Jest and use them as standalone packages. Here's a list of the available packages: jest-changed-files Tool for identifying modified files in a git/hg repositor

  • rs-jest 是一个用于运行 Rust 单元测试的 Jest transformer,安装之后只需要简单配置jest.config.js即可使用。 使用 从 npm 安装 npm install rs-jest --save-dev 配置 jest.config.js module.exports = { transform: { "^.+\\.rs$": "rs-jest" }};

  • jest-puppeteer 允许你使用 Jest & Puppeteer 运行测试。 # for jest 22~23npm install --save-dev jest-puppeteer@3.9.0 puppeteer jest# for jest 24+npm install --save-dev jest-puppeteer puppeteer jest 需要 Jest v22 及以上