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

jest-joi

v1.1.17

Published

Jest matcher for Joi schemas

Downloads

5,529

Readme

Jest Joi

expect(value).toMatchSchema(schema);

Matchers for validating values against Joi schemas in Jest tests, with awesome error messages and TypeScript support

Version RunKit Workflow Coverage License Badges

For a quick demo, head over to RunKit!

Setup

npm i -D jest-joi

TypeScript

(e.g. via ts-jest)

// jest.setup.ts
// Note: Make sure this is within the scope of your TypeScript config!
import { matchers } from "jest-joi";
expect.extend(matchers);
// jest.config.ts
export default {
  setupFilesAfterEnv: ["./jest.setup.ts"],
};

JavaScript

// jest.setup.js
const jestJoi = require("jest-joi");
expect.extend(jestJoi.matchers);
// jest.config.js
module.exports = {
  setupFilesAfterEnv: ["./jest.setup.js"],
};

For more configuration options, see the Jest configuration docs, especially the setupFilesAfterEnv property.

Usage

Just call the .toMatchSchema() matcher with the Joi schema to validate the value against:

expect(value).toMatchSchema(schema);

Options may be passed as an optional second parameter:

expect(value).toMatchSchema(schema, options);

When the value doesn't match the schema, Jest Joi will provide detailed error messages:

// Simple mismatches describe the error:

test("Value should match schema", () => {
  const schema = Joi.string();
  const value = 3;
  expect(value).toMatchSchema(schema);
});

// [FAIL]  src/schema.spec.ts
//  ✕ Value should match schema
//
//    expect(received).toMatchSchema(schema)
//
//    Received: 3
//    Expected: Received must be a string
// Complex mismatches annotate the value:

test("Value should match schema", () => {
  const schema = Joi.object({
    a: Joi.string(),
  });
  const value = {
    a: false,
  };
  expect(value).toMatchSchema(schema);
});

// [FAIL]  src/schema.spec.ts
//  ✕ Value should match schema
//
//    expect(received).toMatchSchema(schema)
//
//    Received:
//    {
//      "a" [1]: false
//    }
//
//    [1] "a" must be a string
// Negated matches display the schema:

test("Value should not match schema", () => {
  const schema = Joi.string();
  const value = "a";
  expect(value).not.toMatchSchema(schema);
});

// [FAIL]  src/schema.spec.ts
//  ✕ Value should not match schema
//
//    expect(received).not.toMatchSchema(schema)
//
//    Received: "a"
//    Schema: { type: "string" }
// Options can be passed as a second argument:

test("Value should match schema with options", () => {
  const schema = Joi.object({});
  const value = {
    b: true,
  };
  const options = {
    allowUnknown: false,
  };
  expect(value).toMatchSchema(schema, options);
});

// FAIL  src/schema.spec.ts
//  ✕ Value should match schema with options (7ms)
//
//    expect(received).toMatchSchema(schema)
//
//    Received:
//    {
//      "b" [1]: true
//    }
//
//    [1] "b" is not allowed

Matchers

Jest Joi includes matchers both for validating values against a schema, and for validating schemas themselves.

toMatchSchema()

Pass a value to expect() to validate against the schema. The schema may be either a Joi schema object or a schema literal (which will be compiled using Joi.compile()).

expect(value).toMatchSchema(schema, options?);

toBeSchema()

Pass a value to expect() to validate whether it's a Joi schema.

expect(schema).toBeSchema();

toBeSchemaLike()

Pass a value to expect() to validate whether it's a Joi schema or a schema literal (a value that can be compiled using Joi.compile()).

expect(schema).toBeSchemaLike();