项目作者: tkesgar

项目描述 :
❤️ Simulate request to express middlewares for testing
高级语言: TypeScript
项目地址: git://github.com/tkesgar/ariadoa.git
创建时间: 2020-03-11T21:30:47Z
项目社区:https://github.com/tkesgar/ariadoa

开源协议:MIT License

下载


ariadoa

Build Status

See? No problem, right?

ariadoa provides a utility function createTestResponse that can be used for
testing Express apps, routers, or middlewares:

  1. import { createTestResponse } from "@tkesgar/ariadoa";
  2. it("should send data from fn", async () => {
  3. const { status, payload } = await createTestResponse([
  4. (req, res) => {
  5. res.send({ foo: "bar" });
  6. },
  7. ]);
  8. expect(status).toBe(200);
  9. expect(JSON.parse(payload)).toEqual({
  10. status: "success",
  11. data: { foo: "bar" },
  12. });
  13. });

It is a convenience wrapper with typings for the excellent hecks
library.

Installation

  1. npm install @tkesgar/ariadoa --save-dev

Usage

createTestResponse

Given a list of middlewares, create an app with the middlewares and inject with
a request.

Please refer to hapi Server.inject() docs for further details of
the request options and response object. Note that createTestResponse sets the
request URL to / by default.

  1. import { createTestResponse } from "@tkesgar/ariadoa";
  2. const response = await createTestResponse([
  3. (req, res) => {
  4. res.send({ foo: "bar", baz: 123 });
  5. },
  6. ]);
  7. console.log(response.status);
  8. // > 200
  9. console.log(response.payload);
  10. // > '{"foo":"bar","baz":123}'

createTestServer

Given a list of middlewares, create a hapi Server instance with the Express
middlewares mounted. The resulting server then can be injected with different
requests each time.

This is the same function used by createTestResponse to create a server. You
might want to use this function to manually create the server instance, if
creating the server is particularly expensive.

Please refer to hapi Server docs for further details of the
server object.

Contribute

Feel free to send issues or create pull requests.

License

Licensed under MIT License.