Code Monkey home page Code Monkey logo

mocha-foam's Introduction

mocha-foam

An experimental Mocha BDD wrapper for fast-check's property based testing.

Contents

Install

Add mocha-foam to your project as a dev dependency, along with fast-check and mocha, if you are not already using them:

> yarn add -D fast-check mocha mocha-foam

Usage

This is a BDD-style wrapper around fc.check(fc.asyncProperty(...)), and supports all of the same Arbitraries that fast-check normally provides.

Property based suites are defined with over and the library is provided as an ES module:

import { expect } from 'chai';
import { integer } from 'fast-check';
import { over } from 'mocha-foam';

describe('example properties', () => {
  over('some numbers', integer(), (it) => {
    it('should be even', (n: number) => {
      return n % 2 === 0;
    });

    it('should be odd', async (n: number) => {
      expect(n % 2).to.equal(1);
    });
  });
});

Assertions can be made through expect (and the other Chai assertion styles) or by returning a boolean, and may be async functions. The test callbacks are called with a Mocha context in this, so the usual caveats apply to arrow functions.

Note the wrapped it passed to the suite callback by over. This wrapped it calls through to Mocha's it, after wrapping the test in a fast-check property, while over calls through to Mocha's describe. Most Mocha features, like beforeEach and afterEach hooks, work correctly within the suite defined by over.

You can pass additional parameters to the fast-check Runner after the suite callback:

describe('more examples', () => {
  over('some UUIDs', uuid(), (it) => {
    // beforeEach hooks work normally, since the wrapped it calls through to real it
    beforeEach(() => {
      console.log('before each UUID test');
    });

    it('should be a good one', (id: string) => {
      return id[9] !== 'a';
    });

    it('should be long enough', (id: string) => {
      expect(id).to.have.length.greaterThan(2);
    });
  }, {
    // fast-check parameters are supported, like examples
    examples: ['a', 'b'],
    numRuns: 1_000,
  });

  over('mapped properties', tuple(lorem(), integer()).map(([a, b]) => a.substr(b)), (it) => {
    it('should have content', (text: string) => {
      return text.length > 0;
    });
  }, {
    // error formatting can be overridden with a custom handler, or fast-check's default reporter
    errorReporter: defaultReportMessage,
  });
});

The default reporter, briefReporter, will print the number of runs, number of shrinks, and any counterexamples found:

  5) example properties
       some UUIDs
         should be a good one:
     Error: Property failed by returning false after 3 runs and 8 shrinks, failing on: "00000000-a000-300d-8000-000000000000" (seed: -886543855, path: '2:0:10:0:10:0:2:3:3')
      at /home/ssube/code/ssube/mocha-foam/out/src/index.js:20:385
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

Build

To build mocha-foam, run make. The build depends on:

  • Node 14+
  • Yarn 1.x

To run small tests with coverage, run make cover.

The make help target will print help for the available targets.

License

mocha-foam is released under the MIT license.

mocha-foam's People

Contributors

renovate-bot avatar renovate[bot] avatar ssube avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

mocha-foam's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • update: replace dependency standard-version with commit-and-tag-version ^9.5.0

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

gitlabci
.gitlab/ci-tools.yml
  • docker.artifacts.apextoaster.com/apextoaster/base 1.5
  • docker.artifacts.apextoaster.com/apextoaster/code-climate 0.6
  • docker.artifacts.apextoaster.com/apextoaster/docker 20.10
  • docker.artifacts.apextoaster.com/apextoaster/docker-dind 20.10
  • docker.artifacts.apextoaster.com/apextoaster/node 16.19
  • docker.artifacts.apextoaster.com/apextoaster/sonar-scanner 4.4
npm
package.json
  • @types/chai-as-promised 7.1.8
  • @types/mocha 10.0.6
  • @types/node 18.15.3
  • @types/sinon 10.0.20
  • @types/sinon-chai 3.2.12
  • c8 8.0.1
  • chai 4.4.1
  • chai-as-promised 7.1.1
  • fast-check 3.10.0
  • mocha 10.3.0
  • sinon 15.2.0
  • sinon-chai 3.7.0
  • source-map-support 0.5.21
  • standard-version ^9.5.0
  • tslib 2.5.3
  • typescript 4.9.5
  • fast-check ^2.0.0 || ^3.0.0
  • mocha ^9.0.0 || ^10.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.