jest-expect-jwt
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

jest-expect-jwt

A nicer way to compare JWT properties in tests

Expectations

toBeTokenContaining

It can be used to partially compare the object encoded within the token excluding the iat or exp.
If you want an exact match then use toBeTokenMatching

Example:

expect(token).toBeTokenContaining({ hello: "world" });

toBeTokenExpiringIn

Check when the token is expiring. This gives a couple of seconds leway either side to avoid having to mock dates. The expiry time can be written in a human readable format by using zeit/ms

Example:

expect(token).toBeExpiringIn("24h");

toBeTokenMatching

It can be used to exactly compare the object encoded within the token excluding the iat or exp.
If you want a partial match then use toBeTokenContaining

Example:

expect(token).toBeTokenMatching({ hello: "world" });

Setup

1. Install the package

npm

npm install --save-dev jest-expect-jwt

yarn

yarn add --dev jest-expect-jwt

2. Configure it within jest

Add it your jest config either jest.config.js or package.json.

{
  ...
  "setupFilesAfterEnv": ["jest-expect-jwt"],
  ...
}

3. Typescript (optional)

If your editor does not detect the types then you can create a global.d.ts with the contents as below.

import "jest-expect-jwt";

Readme

Keywords

none

Package Sidebar

Install

npm i jest-expect-jwt

Weekly Downloads

303

Version

1.0.1

License

ISC

Unpacked Size

21.8 kB

Total Files

29

Last publish

Collaborators

  • tgandrews