4.4.1 • Published 11 months ago

@jsenv/assert v4.4.1

Weekly downloads
52
License
MIT
Repository
github
Last release
11 months ago

assert

npm package

@jsenv/assert is the NPM package used by jsenv to write tests.

It can be resumed by the following quote:

equal() is my favorite assertion. If the only available assertion in every test suite was equal(), almost every test suite in the world would be better for it.

— Eric Elliot in Rethinking Unit Test Assertion

Example

import { assert } from "@jsenv/assert";

assert({
  actual: {
    foo: true,
  },
  expect: {
    foo: false,
  },
});

img

There is 200+ examples in ./tests/

How it works

assert does nothing when comparison is successfull but throws an error when comparison is failing.

Features

Colors

The message produced have colors that helps to see the diff. Each color have a meaning described below:

ColorMeaning
greysame in actual and expect
reddifferent from expect
greendifferent from actual
yellowexists only in actual / exists only in expect

JavaScript aware

Comparison understands JavaScript and makes the diff more redable

assert({
  actual: 149600000,
  expect: 1464301,
});

img

This includes things like comparison on url parts, date parts, http headers and many more.

Multiline diff

assert({
  actual: {
    foo: `Hello,
my name is Benjamin
and my brother is joe`,
  },
  expect: {
    foo: `Hello,
my name is Ben
and my brother is joe`,
  },
});

img

Keep long diff readable

assert({
  actual: "http://example_that_is_quite_long.com/dir/file.txt",
  expect: "http://example_that_is_quite_long.com/dir/file.css",
});

img

Keep nested diff readable

When the diff is very deep the message omits the parents to keep the message concise and readable

assert({
  actual: {
    the: {
      nesting: {
        is: {
          very: {
            deep: {
              in: {
                this: {
                  one: {
                    foo: {
                      a: true,
                      tata: { test: true, bar: { a: "1" } },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
  expect: {
    the: {
      nesting: {
        is: {
          very: {
            deep: {
              in: {
                this: {
                  one: {
                    foo: false,
                  },
                },
              },
            },
          },
        },
      },
    },
  },
});

img

Custom assertions

assert({
  actual: 50,
  expect: assert.between(100, 200),
});

img

And much more

Usage in Node.js

npm i --save-dev @jsenv/assert
import { assert } from "@jsenv/assert";

assert({
  actual: true,
  expect: false,
});

Usage in a browser

Using NPM

npm i --save-dev @jsenv/assert
<script type="module">
  import { assert } from "@jsenv/assert";

  assert({
    actual: true,
    expect: false,
  });
</script>

Using CDN

<script type="module">
  import { assert } from "https://unpkg.com/@jsenv/assert@latest";

  assert({
    actual: true,
    expect: false,
  });
</script>
4.1.8

12 months ago

4.1.7

12 months ago

4.1.9

12 months ago

4.4.1

11 months ago

4.0.5

1 year ago

4.4.0

11 months ago

4.0.4

1 year ago

4.0.10

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.2.1

11 months ago

4.0.3

1 year ago

4.2.0

11 months ago

4.0.2

1 year ago

4.0.12

1 year ago

4.0.11

1 year ago

4.0.14

12 months ago

4.0.13

12 months ago

4.0.8

1 year ago

4.1.10

11 months ago

4.1.11

11 months ago

4.1.12

11 months ago

4.1.13

11 months ago

4.1.14

11 months ago

4.1.15

11 months ago

4.1.4

12 months ago

4.1.3

12 months ago

4.1.6

12 months ago

4.1.5

12 months ago

4.1.0

12 months ago

4.3.0

11 months ago

4.1.2

12 months ago

4.1.1

12 months ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.11.0

2 years ago

2.10.1

2 years ago

2.12.0

2 years ago

2.10.2

2 years ago

2.10.0

2 years ago

2.9.0

2 years ago

2.13.0

2 years ago

2.13.1

2 years ago

2.8.1

2 years ago

2.8.0

3 years ago

2.7.1

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.5.0

3 years ago

2.4.1

3 years ago

2.5.2

3 years ago

2.4.0

4 years ago

2.3.2

4 years ago

2.3.0

4 years ago

2.3.1

4 years ago

2.2.9

4 years ago

2.2.8

4 years ago

2.2.7

4 years ago

2.2.6

4 years ago

2.2.5

4 years ago

2.2.4

4 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.2.2

5 years ago

1.2.0

5 years ago

1.2.1

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago