Socket
Socket
Sign inDemoInstall

assert-logic

Package Overview
Dependencies
4
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    assert-logic

This repository/project is a template/placeholder for new NPM packages.


Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

1.1.0 - 2024-03-27

Added

  • Added some and every functions to assert-logic

Readme

Source

assert-logic

Downloads Version@npm Version@git CI Docs

This handy tool can be used to implement additional logic into your assertions, either using Chai, Jest, Assert, or plain JavaScript.

Usage

Install the package:

npm install assert-logic --save-dev

Use it in your tests:

const {and, or} = require('assert-logic');
const {expect} = require('chai');

test("Test if a number is even and greater than 10 or less than 7", () => {
    const number = 12;
    and(
        () => expect(number).to.be.an('number'),
        () => expect(number % 2).to.equal(0),
        or(
            () => expect(number).to.be.lessThan(7),
            () => expect(number).to.be.greaterThan(10),
        ),
    ).evaluate();
});

API

Pass/Fail

In the context of this package, a value is considered failed if it is:

  1. a (sync) function that throws an error
  2. a (sync) function that returns a falsy value (except for undefined)
  3. a (async) function that returns a Promise that rejects
  4. a (async) function that returns a Promise that resolves to a falsy value (except for undefined)
  5. a Promise that rejects
  6. a Promise that resolves to a falsy value (except for undefined)
  7. value that is falsy (except for undefined)

In all other cases, the value is considered passed.

Operations

The API contains the following functions: and, or, not, xor, nand, nor, xnor, every, some, and also the evaluate function.

The following ones accept any number of sync/async functions, Promises, or values as arguments.

OperationDescription
andPasses if all of its arguments pass
orPasses if any of its arguments pass
notNegates the result of its argument
xorPasses if odd number of its arguments passes
nandPasses if any of its arguments fails
norPasses if all of its arguments fail
xnorPasses if all of its arguments pass or all of them fail

The every and some functions are used to evaluate an array of values. The values must be the first parameter, but both functions accept a second parameter that is a function that will be called with each value. If the function is not passed as the second parameter, the value will be interpreted as boolean values.

The every function is a shorthand for and with the values as arguments, while the some function is a shorthand for or with the values as arguments.

const {every, some} = require('assert-logic');
const {expect} = require('expect');

const items = [{id: 1, name: 'Item 1', price: 10}, {id: 2, name: 'Item 2', price: -20}, /* ... */];
every(items, item => {
    expect(item.id, "Item should have ID as positive number").toBeGreaterThan(0);
}).evaluate(); // will not throw
some(items, item => {
    expect(item.price, "Item should have a positive price").toBeGreaterThan(0);
}).evaluate(); // will throw
Append

The append function can be used to append additional logic to an existing variadic logic (all except pass).

const {and} = require('assert-logic');

const logic = and(
    () => true,
    () => false,
);
logic.append(() => true).evaluate(); // will throw an error

Evaluation

The evaluate function will evaluate the logic and throw an error if it fails.

The evaluate function does not need to be called if the logic is used as an argument to an assertion, as the assertion will call it automatically.

The evaluate function will return a Promise if any of the arguments are async.

AssertionError

Given the following expression what is expected to fail:

and(
    xor(true, false, true),
    true,
).evaluate()

The error message will look like this:

AssertionError (AND): Expected all expression to pass, but not all did.
Results:
  - AssertionError (XOR): Expected odd number of expressions to pass, but even number did.
    Results:
      - Pass
      - AssertionError (PASS): Expected expression to pass.
        Results:
          - Error: "Failed expression: (boolean false)"
        Expression: (boolean false)
      - Pass
    Expression: |-
      XOR(
        (boolean true)
        (boolean false)
        (boolean true)
      )
  - Pass
Expression: |-
  AND(
    XOR(
      (boolean true)
      (boolean false)
      (boolean true)
    )
    (boolean true)
  )

How NOT to use it

As a replacement for assertions

This package is not meant to be used as a replacement for assertions, but rather as a tool to implement additional logic into your assertions.

With passing the assertions directly

const {and} = require('assert-logic');
const {expect} = require('chai');

test("Test if a number is even and greater than 10 or less than 7", () => {
    const number = 12;
    and(
        expect(number).to.be.an('number'),
        expect(number % 2).to.equal(0),
        or(
            expect(number).to.be.lessThan(7),
            expect(number).to.be.greaterThan(10),
        ),
    ).evaluate();
});

This will not work as expected, as the assertions will be evaluated before the logic is evaluated.

Other

For detailed documentation see the TypeDocs documentation.

This package uses debug for logging, use assert-logic to see debug logs:

DEBUG=assert-logic node my-script.js

FAQs

Last updated on 27 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc