npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-bed

v2.0.4

Published

testbed for express routes enabling mocking of dependencies

Downloads

16

Readme

express-bed

Angular style TestBed for Express apps.

This is a minimal implementation of a TestBe for typescript Express routes, in the style of Angular.

It allows for easy instantiation of routes for testing, along with convenience methods for passing in mocks and stubs, as well as getting easy access to injected code for spying in tests.

It relies on each of the routes being classes, with a public create method, which is passed an express app and calls app.get etc. as shown in the usage examples.

Installation

npm install express-bed

Usage (examples from unit tests of project)

To import the package:

import { BaseRoute, ExpressBed, ExpressTestBed } from 'express-bed';

const expressBed = ExpressBed.configureTestingModule({
  routes: [
    //routes
  ],
  injectables: [
    //injectables
  ]
});

Usage examples as per the package unit tests:

import { Express, Request, Response } from 'express';
import { BaseRoute, ExpressBed, ExpressTestBed } from './express-bed';
import supertest = require('supertest');

describe('express-bed', () => {
  let testBed: ExpressTestBed;

  describe('configureTestingModule', () => {
    it('should return a ExpressTestBed object with the correct routes ' +
    'if they are passed via config', (done: any) => {
      class TestRoute implements BaseRoute {
        public create(app: Express) {
          app.get('/user/validPath/', (req: Request, res: Response) => {
            res.status(200).send();
          });
        }
      }

      testBed = ExpressBed.configureTestingModule({
        routes: [TestRoute]
      });

      try {
        supertest(testBed.app)
          .get('/user/validPath/')
          .expect(200)
          .then(() => done());
      } catch (error) {
        fail();
      }

      try {
        supertest(testBed.app)
          .get('/user/invalidPath/')
          .expect(404)
          .then(() => done());
      } catch (error) {
        fail();
      }
    });

    it('should return a ExpressTestBed object with the correct routes and injectables, ' + 
    'as well as any injectable dependencies if they are passed via config', (done: any) => {
      class Dependency {
        public doDepStuff(): string {
          return 'blah';
        }
      }

      class TestInjectable {
        constructor(private dep: Dependency) {}

        public doSomething() {
          this.dep.doDepStuff();
        }
      }

      class TestInjectable2 {
        public doSomethingElse() {
          console.log('blah');
        }
      }

      class TestRoute implements BaseRoute {
        constructor(
          private testInjectable: TestInjectable,
          private testInjectable2: TestInjectable2
        ) {
          this.testInjectable.doSomething();
          this.testInjectable2.doSomethingElse();
        }

        public create(app: Express) {
          app.get('/user/validPath/', (req: Request, res: Response) => {
            res.status(200).send();
          });
        }
      }

      testBed = ExpressBed.configureTestingModule({
        routes: [TestRoute],
        injectables: [
          {
            inject: TestInjectable,
            injectables: [Dependency]
          },
          TestInjectable2
        ]
      });

      try {
        supertest(testBed.app)
          .get('/user/validPath/')
          .expect(200)
          .then(() => done());
      } catch (error) {
        fail();
      }

      expect(testBed.get(TestInjectable) instanceof TestInjectable).toBe(true);
      expect(testBed.get(TestInjectable2) instanceof TestInjectable2).toBe(
        true
      );
    });

    it('should return a ExpressTestBed object with the correct routes and ' + 
    'injectables if they are passed via config', (done: any) => {
      class TestInjectable {
        public doSomething() {}
      }

      class TestRoute implements BaseRoute {
        constructor(private testInjectable: TestInjectable) {
          this.testInjectable.doSomething();
        }

        public create(app: Express) {
          app.get('/user/validPath/', (req: Request, res: Response) => {
            res.status(200).send();
          });
        }
      }

      testBed = ExpressBed.configureTestingModule({
        routes: [TestRoute],
        injectables: [TestInjectable]
      });

      try {
        supertest(testBed.app)
          .get('/user/validPath/')
          .expect(200)
          .then(() => done());
      } catch (error) {
        fail();
      }

      expect(testBed.get(TestInjectable) instanceof TestInjectable).toBe(true);
    });

    it('should return a ExpressTestBed object with the correct routes if ' + 
    'multiple routes provided', (done: any) => {
      class TestRoute implements BaseRoute {
        public create(app: Express) {
          app.get('/user/validPath/', (req: Request, res: Response) => {
            res.status(200).send();
          });
        }
      }

      class TestRoute2 implements BaseRoute {
        public create(app: Express) {
          app.post('/user/validPath2/', (req: Request, res: Response) => {
            res.status(200).send();
          });
        }
      }

      testBed = ExpressBed.configureTestingModule({
        routes: [TestRoute, TestRoute2]
      });

      try {
        supertest(testBed.app)
          .get('/user/validPath/')
          .expect(200)
          .then(() => done());
      } catch (error) {
        fail();
      }

      try {
        supertest(testBed.app)
          .post('/user/validPath2/')
          .expect(200)
          .then(() => done());
      } catch (error) {
        fail();
      }
    });
  });
});