Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest matcher #25

Open
richardscarrott opened this issue Sep 16, 2022 · 0 comments
Open

Jest matcher #25

richardscarrott opened this issue Sep 16, 2022 · 0 comments

Comments

@richardscarrott
Copy link
Owner

richardscarrott commented Sep 16, 2022

I recently needed to write some tests which made assertions on an API response which did not have fixtures (i.e. e2e tests). This meant I was testing types more than literal values.

The jest assertions were quite cumbersome and I found myself wanting to reach for OK Computer. e.g.

import {
  assert,
  object,
  array,
  string,
  integer,
  or,
  nul,
  create,
  is,
} from 'ok-computer';
import 'jest-extended';
const matchers = require('jest-extended');
expect.extend(matchers as any);

// Assume this comes from a production API so varies in length and data
const actual = {
  status: 200,
  data: {
    drivers: {
      nodes: [
        {
          id: 'dvr_1',
          name: 'Lewis Hamilton',
          team: 'Mercedes',
          carNumber: 44,
        },
        {
          id: 'dvr_2',
          name: 'Max Verstappen',
          team: 'Red Bull Racing',
          carNumber: 33,
        },
        {
          id: 'dvr_3',
          name: 'George Russell',
          team: null,
          carNumber: null,
        },
        {
          id: 'dvr_4',
          name: 'George Russell',
          team: null,
          carNumber: null,
        },
      ],
    },
  },
  headers: {
    'Content-Length': 100,
  },
  errors: [{ code: 'FOO_BAR' }],
};

test('Jest matchers', () => {
  expect(actual).toEqual({
    status: 200,
    data: {
      drivers: {
        nodes: expect.any(Array),
      },
    },
    headers: expect.anything(),
    errors: expect.any(Array),
  });
  actual.data.drivers.nodes.forEach(driver => {
    expect(driver).toEqual({
      id: expect.any(String),
      name: expect.any(String),
      team: expect.toBeOneOf([expect.any(String), null]),
      carNumber: expect.toBeOneOf([expect.any(Number), null]),
    });
  });
});

test('OK Computer', () => {
  const any = create(() => true)('Expected any'); // TODO: Add to OK Computer lib

  const validator = object({
    status: is(200),
    data: object({
      drivers: object({
        nodes: array(
          object({
            id: string,
            name: string,
            team: or(nul, string),
            carNumber: or(nul, integer),
          })
        ),
      }),
    }),
    headers: any,
    errors: array(any),
  });

  assert(actual, validator);
});

I expect the error output is better using the jest matchers, but perhaps a custom jest matcher could handle that?

expect(actual).toBeOkay(validator)

With OK Computer you also get type inference

import { assert, fn } from 'ok-computer';
const actual: unknown = {};
assert(actual, fn);
actual();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant