Skip to content

Commit

Permalink
feat(core)!: Rename package to assertive-ts/core (#105)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Deprecates the @stackbuilders/assertive-ts package in favor of assertive-ts/core so we can group plugins under the assertive-ts namespace
  • Loading branch information
JoseLion authored Aug 21, 2023
1 parent 76936a2 commit 01e0aeb
Show file tree
Hide file tree
Showing 68 changed files with 315 additions and 486 deletions.
172 changes: 24 additions & 148 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
[![CI](https://github.com/stackbuilders/assertive-ts/actions/workflows/ci.yml/badge.svg)](https://github.com/stackbuilders/assertive-ts/actions/workflows/ci.yml)
[![Release](https://github.com/stackbuilders/assertive-ts/actions/workflows/release.yml/badge.svg)](https://github.com/stackbuilders/assertive-ts/actions/workflows/release.yml)
[![Pages](https://github.com/stackbuilders/assertive-ts/actions/workflows/pages.yml/badge.svg)](https://github.com/stackbuilders/assertive-ts/actions/workflows/pages.yml)
[![NPM version](https://img.shields.io/npm/v/@stackbuilders/assertive-ts?logo=npm)](https://www.npmjs.com/package/@stackbuilders/assertive-ts)
[![NPM bundle size](https://img.shields.io/bundlephobia/min/@stackbuilders/assertive-ts)](https://www.npmjs.com/package/@stackbuilders/assertive-ts)
[![NPM downloads](https://img.shields.io/npm/dm/@stackbuilders/assertive-ts)](https://www.npmjs.com/package/@stackbuilders/assertive-ts)
[![NPM license](https://img.shields.io/npm/l/@stackbuilders/assertive-ts)](./LICENSE)
[![GitHub Release Date](https://img.shields.io/github/release-date/stackbuilders/assertive-ts)](https://github.com/stackbuilders/assertive-ts/releases)
[![NPM Core version](https://img.shields.io/npm/v/@assertive-ts/core?logo=npm&label=core)](https://www.npmjs.com/package/@assertive-ts/core)
[![NPM license](https://img.shields.io/npm/l/@assertive-ts/core)](https://github.com/stackbuilders/assertive-ts/blob/main/LICENSE)
[![Known Vulnerabilities](https://snyk.io/test/github/stackbuilders/assertive-ts/badge.svg)](https://snyk.io/test/github/stackbuilders/assertive-ts)

# AssertiveTS
# Assertive.ts

A type-safe fluent assertion library written in TypeScript and inspired by [Jest](https://jestjs.io/docs/expect) assertions and the popular [AssertJ](https://assertj.github.io/doc/).

This library is designed to work in the browser and in Node.js. It ships with a rich set of expressive and flexible matchers that allows chaining multiple assertions. AssertiveTS is framework agnostic and should be used with a test framework such as [Jest](/docs/jest-tutorial.md), [Mocha](/docs/mocha-tutorial.md), or Ava.
This library is designed to work in the browser and in Node.js. It ships with a rich set of expressive and flexible matchers that allows chaining multiple assertions. Assertive.ts is framework agnostic and should be used with a test framework such as [Jest](/docs/jest-tutorial.md), [Mocha](/docs/mocha-tutorial.md), or Ava.

**🚨 BREAKING CHANGES:** Since v2, the `@stackbuilders/assertive-ts` package has been renamed to `@assertive-ts/core` so we can group other packages, such as plugins, into the same namespace. Check the [packages](#packages) section for more info.

## Type-safe library

A distinctive feature of AssertiveTS with other assertion libraries is that it leverages the TypeScript compiler to avoid type coercions and mismatches. It also infers the static type of the value you want to assert and provides you with intelligent matcher completion and signature help so that you can write code more quickly and correctly.
A distinctive feature of Assertive.ts with other assertion libraries is that it leverages the TypeScript compiler to avoid type coercions and mismatches. It also infers the static type of the value you want to assert and provides you with intelligent matcher completion and signature help so that you can write code more quickly and correctly.

### Features

Expand All @@ -29,39 +28,35 @@ A distinctive feature of AssertiveTS with other assertion libraries is that it l
- Works with any test runner and framework such as [Jest](/docs/jest-tutorial.md), [Mocha](/docs/mocha-tutorial.md), or Ava
- Well tested: more than 300 tests!

## Install

```sh
npm install --save-dev @stackbuilders/assertive-ts
```
## Packages

Or:
For convenience, this library is split into packages grouped within the same namespace:

```sh
yarn add --dev @stackbuilders/assertive-ts
```
- **[assertive-ts/core](https://github.com/stackbuilders/assertive-ts/blob/main/packages/core/README.md):** Core functionalities, assertions applicable for any kind of application. This package is required for the [extension mechanism](https://github.com/stackbuilders/assertive-ts/blob/main/packages/core/README.md#extension-mechanism-⚙️) (plugins). Also, this is package replaces the deprecated `stackbuilders/assertive-ts` package.

## Usage

Import the library in your test script:
Using you favorite test runner, you just need to import the `expect` and test away! If you don't really agree with `expect` as the name of the assertion function, we provide a couple aliases, such as `assert` and `assertThat`.

```ts
import { expect } from "@stackbuilders/assertive-ts"
```
import { expect } from "@assertive-ts/core"

Use the `expect` function along with a "matcher" function on the value you want to assert:
describe("sum", () => {
it("returns the sum of two numbers", () => {
const result = sum(3, 2);

```ts
expect(sum(1, 2)).toBeEqual(3);
expect(result).toBeEqual(5);
});
});
```

To assert the opposite, just add `.not` before a matcher:
To assert the opposite, you can simply use the `.not` modifier before the matcher:

```ts
expect(sum(1, 2)).not.toBeNull();
```

With `assertive-ts` you can use **fluent assertions**, which means you can chain multiple matcher functions to the same value under test:
This library provides **fluent assertions**, which means you can chain multiple matcher functions to the same value under test:

```ts
expect("assertive-ts is awesome!")
Expand All @@ -86,133 +81,14 @@ expect(14).toEndWith("4");
^ ? type error: `toEndWith` does not exist in `NumberAssertion`
```

For a list of all matchers and extended documentation, please refer to the [API documentation](https://stackbuilders.github.io/assertive-ts/docs/build/).

### Type Factory 🏭

A great feature of AssertiveTS is the type safety across the API. But, what should you do if you want to check the value under test is of some specific type during runtime? The answer is simple, AssertiveTS provides a `.asType(TypeFactory)` method, where the [TypeFactory](https://stackbuilders.github.io/assertive-ts/docs/build/interfaces/TypeFactory.html) parameter lets you check for the specific type and narrow the assertion instance to a more specific one. To make things simpler, AssertiveTS provides [TypeFactories](https://stackbuilders.github.io/assertive-ts/docs/build/interfaces/StaticTypeFactories.html) for the basic types:

```ts
import { expect, TypeFactories } from "@stackbuilders/assertive-ts";

expect(value)
.asType(TypeFactories.String)
.toBeEmpty();

expect(list)
.asType(TypeFactories.array(TypeFactories.Number))
.toHaveSameMembers([1, 2, 3, 4, 5]);
```

If the built-in type factories are not enough to assert your specific type, you can always create your own factory. A `TypeFactory<S, A>` is nothing more than an object with 3 properties:

- `Factory: new(actual: S) => A` - The specific assertion constructor to return if the predicate is true. Where `S` is the actual value type, and `A` is the type of the assertion to return (`A` should extend from `Assertion<S>`).
- `predicate(value: unknown): value is S` - A predicate function that checks if the value is of the expected type.
- `typeName: string` - The name of the checked type. Used to make the assertion error message clearer.

So, using a custom `TypeFactory` can look like the following:

```ts
interface Point3D {
x: number;
y: number;
z: number;
}

expect(maybePoint).asType({
Factory: ObjectAssertion<Point3D>,
predicate: (value): value is Point3D => {
return typeof value === "object"
&& value !== null
&& "x" in value
&& "y" in value
&& "z" in value
&& Object.values(value).every(v => typeof v === "number");
},
typeName: "Point3D"
});
```

### Handling TypeScript Unions

[Union types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) are a TypeScript concept that is only applicable at type level. During runtime, the value can only be one of the types. For instance, if we say `const foo: number | string = ...`, at runtime `foo` will be either a `number` or a `string`. If you want to use a more specific assertion on a union type, you can use `.asType(..)` to first assert the expected type, and then move forward with more assertions:

```ts
const foo: number | string = 5;

expect(foo)
.asType(TypeFactories.Number)
.toBePositive();
```

### Help! The value can also be `null` or `undefined`

When a value can be also `null` or `undefined`, we're going over the same concept as [Union types](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types). So if you want to make more specific assertions over a value that can be `null | undefined`, just use `.asType(..)` first:

```ts
const bar: string | null | undefined = " ";

expect(bar)
.asType(TypeFactories.String)
.toBeBlank();
```
For a list of all [Core](https://github.com/stackbuilders/assertive-ts/blob/main/packages/core/README.md) matchers and extended documentation, you can refer to the [Core API documentation](https://stackbuilders.github.io/assertive-ts/docs/core/build/).

## Test Runner Integration

- [Jest Integration](docs/jest-tutorial.md)
- [Mocha Integration](docs/mocha-tutorial.md)

## API Reference

You can find the full API reference [here](https://stackbuilders.github.io/assertive-ts/docs/build/)

## Extension mechanism ⚙️

This feature allows you to extend the `expect(..)` function to return additional `Assertion<T>` instances depending on the value under test. This opens the door to add additional assertion matchers for more specific cases. An `Assertion<T>` can be added in the form of a `Plugin`:
```ts
interface Plugin<T, A extends Assertion<T>> {
Assertion: new(actual: T) => A;
insertAt: "top" | "bottom";
predicate: (actual: unknown) => actual is T;
}
```

Where `Assertion` is the class you want to add, `insertAt` determines if the logic is inserted before or after all the primitives, and `predicate` is the logical code used to determine if value matches the `Assertion` type.

Once you have a plugin object, you can add it to assertive-ts with the `usePlugin(..)` helper function. Calls to this function should go on the setup file of your test runner or in a `beforeAll()` hook, so the extension is applied to all your tests.
```ts
// test/setup.ts
import { usePlugin } from "@stackbuilders/assertive-ts";

import { FilePlugin, HTMLElementPlugin } from "./plugins"; // your custom (or 3rd-party) plugins

usePlugin(FilePlugin);
usePlugin(HTMLElementPlugin);
// ...
```

### What about the types?

Each new plugin should add an additional overload to the `expect(..)` function to maintain type safety. To do that, you can extend the `Expect` interface to add the additional overloads. For example:
```ts
import { FileAssertion } from "./FileAssertion";
import { HTMLElementAssertion } from "./HTMLElementAssertion";

declare module "@stackbuilders/assertive-ts" {

export interface Expect {
(actual: File): FileAssertion;
(actual: HTMLElement): HTMLElementAssertion;
// ...
}
}
```

> **Note:** 3rd-party libraries should do this on their types entry point (index.d.ts), this way the interface is automatically extended when their plugin is passed to the `usePlugin(..)` function.
### How to...
Assertive.ts works on any JavaScript test runner, both on Node.js and browser environments. Below you can find some example of how to use it on some of the most common test runners:

If you're looking to write a plugin, you can find a simple example [here](./examples/symbolPlugin/). The example plugin is used in the [Jest](./examples/jest/test/plugins.test.ts) and [Mocha](./examples/mocha/test/plugins.test.ts) examples too, so you can also take a look at them to see how to apply and use plugins.
- [Jest Integration](https://github.com/stackbuilders/assertive-ts/blob/main/docs/jest-tutorial.md)
- [Mocha Integration](https://github.com/stackbuilders/assertive-ts/blob/main/docs/mocha-tutorial.md)

## Contributors ✨

Expand Down
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defaults:
- scope:
path: ""
values:
title: AssertiveTS
title: Assertive.ts
subtitle: A type-safe fluent assertion library
10 changes: 5 additions & 5 deletions docs/jest-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Let's set up a project with Jest and assertive-ts to write our first assertion.
First, let's install the dependencies:

```
npm install --save-dev typescript ts-jest jest @types/jest @stackbuilders/assertive-ts
npm install --save-dev typescript ts-jest jest @types/jest @assertive-ts/core
```

This library is meant to be used with TypeScript, so to have better results we encourage you to use TypeScript in your project.
Expand All @@ -31,11 +31,11 @@ export const sum = (a: number, b: number): number => {
}
```

Now let's write a test for that function. Make sure to import `expect` from the `@stackbuilders/assertive-ts` module:
Now let's write a test for that function. Make sure to import `expect` from the `@assertive-ts/core` module:

*tests/mathUtils.test.ts*
```typescript
import { expect } from "@stackbuilders/assertive-ts";
import { expect } from "@assertive-ts/core";
import { sum } from "../src/mathUtils";

describe("sum", () => {
Expand All @@ -55,12 +55,12 @@ You might want to use the `expect` function from Jest along with assertive-ts as

```typescript
import { expect } from "@jest/globals";
import { assert } from "@stackbuilders/assertive-ts";
import { assert } from "@assertive-ts/core";
```

- Or use an explicit import to rename the `expect` function from Jest to something like `jestExpect`:

```typescript
import { expect as jestExpect } from "@jest/globals";
import { expect } from "@stackbuilders/assertive-ts";
import { expect } from "@assertive-ts/core";
```
6 changes: 3 additions & 3 deletions docs/mocha-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Let's set up a project with Mocha and assertive-ts.

First, let's install the dependencies:
```
npm install --save-dev typescript mocha @types/mocha ts-node @stackbuilders/assertive-ts
npm install --save-dev typescript mocha @types/mocha ts-node @assertive-ts/core
```

This library is meant to be used with TypeScript, so to have better results we encourage you to use TypeScript in your project.
Expand Down Expand Up @@ -40,11 +40,11 @@ export const sum = (a: number, b: number): number => {
}
```

Now let's write a test for that function. Make sure to import `expect` from the `@stackbuilders/assertive-ts` module.
Now let's write a test for that function. Make sure to import `expect` from the `@assertive-ts/core` module.

*tests/mathUtils.test.ts*
```typescript
import { expect } from "@stackbuilders/assertive-ts";
import { expect } from "@assertive-ts/core";
import { sum } from "../src/mathUtils";

describe("sum", () => {
Expand Down
4 changes: 2 additions & 2 deletions examples/jest/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Jest example

Example project using AssertiveTS with Jest's test runner.
Example project using Assertive.ts with Jest's test runner.

This example showcases the use of AssertiveTS' `expect` and its `assert` alias,
This example showcases the use of Assertive.ts' `expect` and its `assert` alias,
as well as the use of Jest's own `expect` function imported with a different
name.

Expand Down
2 changes: 1 addition & 1 deletion examples/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test": "jest"
},
"devDependencies": {
"@assertive-ts/core": "workspace:^",
"@examples/symbol-plugin": "workspace:^",
"@stackbuilders/assertive-ts": "workspace:^",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.8",
"jest": "^29.6.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/jest/test/mathUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, expect } from "@assertive-ts/core";
import { expect as jestExpect } from "@jest/globals";
import { assert, expect } from "@stackbuilders/assertive-ts";

import { sum } from "../src/mathUtils";

Expand Down
2 changes: 1 addition & 1 deletion examples/jest/test/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from "@stackbuilders/assertive-ts";
import { expect } from "@assertive-ts/core";

describe("plugins", () => {
it("can use the symbol plugin", () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/jest/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { usePlugin } from "@assertive-ts/core";
import { SymbolPlugin } from "@examples/symbol-plugin";
import { usePlugin } from "@stackbuilders/assertive-ts";

beforeAll(() => {
usePlugin(SymbolPlugin);
Expand Down
4 changes: 2 additions & 2 deletions examples/mocha/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Mocha example

Example project using AssertiveTS with Mocha's test runner.
Example project using Assertive.ts with Mocha's test runner.

This example showcases the use of AssertiveTS' `expect` and its `assert` alias
This example showcases the use of Assertive.ts' `expect` and its `assert` alias
with Mocha as the test runner.

## How to setup
Expand Down
2 changes: 1 addition & 1 deletion examples/mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"test": "mocha"
},
"devDependencies": {
"@assertive-ts/core": "workspace:^",
"@examples/symbol-plugin": "workspace:^",
"@stackbuilders/assertive-ts": "workspace:^",
"@types/mocha": "^10.0.1",
"@types/node": "^20.4.8",
"mocha": "^10.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/mocha/test/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { usePlugin } from "@assertive-ts/core";
import { SymbolPlugin } from "@examples/symbol-plugin";
import { usePlugin } from "@stackbuilders/assertive-ts";
import { RootHookObject } from "mocha";

export function mochaHooks(): RootHookObject {
Expand Down
2 changes: 1 addition & 1 deletion examples/mocha/test/mathUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, expect } from "@stackbuilders/assertive-ts";
import { assert, expect } from "@assertive-ts/core";

import { sum } from "../src/mathUtils";

Expand Down
2 changes: 1 addition & 1 deletion examples/mocha/test/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from "@stackbuilders/assertive-ts";
import { expect } from "@assertive-ts/core";

describe("plugins", () => {
it("can use the symbol plugin", () => {
Expand Down
4 changes: 2 additions & 2 deletions examples/symbolPlugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"compile": "tsc"
},
"devDependencies": {
"@stackbuilders/assertive-ts": "workspace:^",
"@assertive-ts/core": "workspace:^",
"typescript": "^5.1.6"
},
"peerDependencies": {
"@stackbuilders/assertive-ts": "*"
"@assertive-ts/core": "*"
}
}
4 changes: 2 additions & 2 deletions examples/symbolPlugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Plugin } from "@stackbuilders/assertive-ts";
import { Plugin } from "@assertive-ts/core";

import { SymbolAssertion } from "./lib/SymbolAssertion";

declare module "@stackbuilders/assertive-ts" {
declare module "@assertive-ts/core" {

export interface Expect {
// eslint-disable-next-line @typescript-eslint/prefer-function-type
Expand Down
2 changes: 1 addition & 1 deletion examples/symbolPlugin/src/lib/SymbolAssertion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Assertion, AssertionError } from "@stackbuilders/assertive-ts";
import { Assertion, AssertionError } from "@assertive-ts/core";

export class SymbolAssertion extends Assertion<symbol> {

Expand Down
Loading

0 comments on commit 01e0aeb

Please sign in to comment.