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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@axiac.ro/maroco

v1.0.1

Published

Jest Matchers for Routing Controllers

Downloads

1

Readme

Jest Matchers for Routing Controllers

Jest MAtchers for ROuting COntrollers is the testing companion of the Routing Controllers package using Jest. It allows the developer to write semantic expectations for code that handles the routes of a web application.


Installing

Install the package using your favorite package manager, as a developer dependency (devDependency).

How to use Maroco

Using Maroco is as simple as "one, two, three":

  1. Create in the tests directory a file that imports all the controller modules. Import this file into all use cases of the routes. The file is named all-controllers.ts in the example below; feel free to use whatever name fits your project better.
  2. Use the function $route() in use cases to identify the action that handles the described route.
  3. Use the Jest matchers described below on the value returned by $route().

The file all-controllers.ts:

import 'controllers/Hello';
import 'controllers/Foo';
// etc

A use case:

// Load the $route() function; it is needed to identify the action that handles a route
import { $route } from '@axiac.ro/maroco';

// Load all controllers
import './all-controllers';

// Describe the expected behaviour of the code that handles a certain route
describe('GET /hello/world?foo=bar', () => {
  it('is handled', () => {
    expect($route('get', '/hello/world')).toBeHandled();
  });

  it('returns JSON', () => {
    expect($route('get', '/hello/world')).toReturnJSON();
  });

  it('uses the `foo` query parameter', () => {
    expect($route('get', '/hello/world')).toUseQueryParameter('foo')
  })
});

The $route() function

Identifies the controller action that handles the provided verb and path. The object it returns can be passed to expect()

function $route(verb: string, route: string)

Arguments

  • verb - a HTTP verb (GET, POST, PUT, PATCH, DELETE, OPTIONS etc); the value is case-insensitive;
  • route - the path fragment of the URL.

The Jest matchers

The following pseudo-code describes the Jest matchers provided by maroco, and the expected types of their arguments.

// General
expect($route('get', '/foo/bar')).toBeHandled();

// Controller
expect($route('post', '/foo/bar')).toAcceptJSON();
expect($route('post', '/foo/bar')).toReturnJSON();

// Parameters
expect($route('get', '/foo/bar')).toUseRouteParameter(paramName);     // string
expect($route('get', '/foo/bar')).toUseAllRouteParameters();
expect($route('get', '/foo/bar')).toUseQueryParameter(paramName);     // string
expect($route('get', '/foo/bar')).toUseAllQueryParameters();
expect($route('get', '/foo/bar')).toUseBodyParameter(paramName);      // string
expect($route('post', '/foo/bar')).toUseRequestBody();
expect($route('get', '/foo/bar')).toUseRequestHeader(headerName);     // string
expect($route('get', '/foo/bar')).toUseAllRequestHeaders();
expect($route('get', '/foo/bar')).toUseRequestCookie(cookieName);     // string
expect($route('get', '/foo/bar')).toUseAllRequestCookies();
expect($route('get', '/foo/bar')).toUseSessionProperty(propertyName); // string
expect($route('get', '/foo/bar')).toUseSession();
expect($route('get', '/foo/bar')).toUseContext();
expect($route('get', '/foo/bar')).toUseCurrentUser();
expect($route('get', '/foo/bar')).toUseUploadedFile(fileName);        // string
expect($route('get', '/foo/bar')).toUseAllUploadedFiles();

// Response headers
expect($route('get', '/foo/bar')).toRequireAuthorization();
expect($route('post', '/foo/bar')).toAnswerWith200OK();
expect($route('post', '/foo/bar')).toAnswerWith201Created();
expect($route('post', '/foo/bar')).toAnswerWith202Accepted();
expect($route('post', '/foo/bar')).toAnswerWith204NoContent();
expect($route('post', '/foo/bar')).toAnswerWith301MovedPermanently();
expect($route('post', '/foo/bar')).toAnswerWith302Found()
expect($route('post', '/foo/bar')).toAnswerWith(statusCode)           // number
expect($route('post', '/foo/bar')).toReturnContentType(contentType);  // string
expect($route('post', '/foo/bar')).toSetLocation(location);           // string
expect($route('post', '/foo/bar')).toRedirect(url);                   // string
expect($route('post', '/foo/bar')).toSetHeader(header, value?);       // string, string (optional)