0.15.1 • Published 5 months ago

@clipboard-health/testing-core v0.15.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@clipboard-health/testing-core

TypeScript-friendly testing utilities.

Table of contents

Install

npm install @clipboard-health/testing-core

Usage

Type narrowing expect helpers

Jest's expect(...).toBeDefined() does not narrow types.

This gives a type error:

const value = getValue(); // returns 'string | undefined'

expect(value).toBeDefined();

const { length } = value;
// ^? Property 'length' does not exist on type 'string | undefined'.

This library's helpers narrow types:

import { ok } from "node:assert/strict";

import { expectToBeDefined } from "@clipboard-health/testing-core";

function getValue(): string | undefined {
  return "hi";
}

const value = getValue();
expectToBeDefined(value);

// Narrowed to `string`
const { length } = value;
ok(length === 2);
import { ok } from "node:assert/strict";

import { expectToBeLeft } from "@clipboard-health/testing-core";
import { either as E } from "@clipboard-health/util-ts";

function divide(numerator: number, denominator: number): E.Either<string, number> {
  if (denominator === 0) {
    return E.left("Cannot divide by zero");
  }

  return E.right(numerator / denominator);
}

const value = divide(10, 0);
expectToBeLeft(value);

// Narrowed to Left
ok(value.left === "Cannot divide by zero");
import { ok } from "node:assert/strict";

import { expectToBeRight } from "@clipboard-health/testing-core";
import { either as E } from "@clipboard-health/util-ts";

function divide(numerator: number, denominator: number): E.Either<string, number> {
  if (denominator === 0) {
    return E.left("Cannot divide by zero");
  }

  return E.right(numerator / denominator);
}

const value = divide(10, 2);
expectToBeRight(value);

// Narrowed to Right
ok(value.right === 5);
import { ok } from "node:assert/strict";

import { expectToBeDefined, expectToBeSafeParseError } from "@clipboard-health/testing-core";
import { z } from "zod";

const schema = z.object({ name: z.string() });

const value = schema.safeParse({ name: 1 });
expectToBeSafeParseError(value);

// Narrowed to `SafeParseError`
const firstIssue = value.error.issues[0];
expectToBeDefined(firstIssue);

// Narrowed to `ZodIssue`
ok(firstIssue.message === "Expected string, received number");
import { ok } from "node:assert/strict";

import { expectToBeSafeParseSuccess } from "@clipboard-health/testing-core";
import { z } from "zod";

const schema = z.object({ name: z.string() });

const value = schema.safeParse({ name: "hi" });
expectToBeSafeParseSuccess(value);

// Narrowed to `SafeParseSuccess`
ok(value.data.name === "hi");

Local development commands

See package.json scripts for a list of commands.

0.11.8

9 months ago

0.11.9

9 months ago

0.11.0

12 months ago

0.11.1

12 months ago

0.13.0

7 months ago

0.11.2

12 months ago

0.13.1

7 months ago

0.11.3

10 months ago

0.15.0

5 months ago

0.13.2

7 months ago

0.11.4

10 months ago

0.15.1

5 months ago

0.13.3

6 months ago

0.11.5

10 months ago

0.13.4

5 months ago

0.11.6

10 months ago

0.11.7

10 months ago

0.9.0

12 months ago

0.9.1

12 months ago

0.12.10

7 months ago

0.12.11

7 months ago

0.12.7

7 months ago

0.12.8

7 months ago

0.12.9

7 months ago

0.12.0

9 months ago

0.12.1

8 months ago

0.14.0

5 months ago

0.12.2

8 months ago

0.12.3

8 months ago

0.12.4

8 months ago

0.12.5

8 months ago

0.12.6

7 months ago

0.11.10

9 months ago

0.10.0

12 months ago

0.8.1

12 months ago

0.8.0

12 months ago

0.8.3

12 months ago

0.8.2

12 months ago

0.7.11

1 year ago

0.7.10

1 year ago

0.7.9

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago