pify-express-handler

1.2.0 • Public • Published

Pify Express Handler

A 0-dependency promisify package for easier testing of express handlers used in Firebase Functions & Google Cloud Functions.

⚠️ Warning: express middleware not supported.

Usage:

import { promisifyHandler } from "pify-express-handler"

const someHandler = async (req, res) => {
    await ...; // some call to a server
    res.status(201).send("It's been created!");
}

// Test
it("should create the resource if everything succeeds", async () => {
    const handler = promisifyHandler(someHandler);

    const {status, body, headers} = await handler({/* empty request will suffice for this test*/});

    expect(status).to.equal(201);
    expect(body).to.equal("It's been created!");
    expect(headers['foo']).to.equal('bar');
})

Why?

When you want to test your function you have a couple approaches.

Instead; you can use this package to make testing your function smooth as butter 🧈.

Limitations

Currently, only the following functions are supported:

  • .write()
  • .status()
  • .end()
  • .set()
  • .send()
  • .json()

If your function uses any other of the res object's functions this package will throw an error.

I'm happy to add more support later, if this package picks up. Please let me know through submitting an issue! 😀

Package Sidebar

Install

npm i pify-express-handler

Weekly Downloads

0

Version

1.2.0

License

MIT

Unpacked Size

14 kB

Total Files

3

Last publish

Collaborators

  • bvanderdrift