testkit.wind.js

![GitHub release](https://img.shields.io/github/release/codeinbrackets/testkit.wind.js.svg) [![npm](https://img.shields.io/npm/v/testkit.wind.js.svg)](https://www.npmjs.com/package/testkit.wind.js) [![Build Status](https://travis-ci.org/CodeInBrackets/tes

Usage no npm install needed!

<script type="module">
  import testkitWindJs from 'https://cdn.skypack.dev/testkit.wind.js';
</script>

README

logomakr_8cwvt1">

GitHub release npm Build Status Coverage Status PRs Welcome issues Welcome npm GitHub issues GitHub pull requests Downloads

testkit.wind.js

TestKit for writing tests for wind.js. This is not a test runner but a set of tools to interact with an actor system, you will need a runner like Jest.

Quick Start

Install the package as a development dependency:

PS> npm i testkit.wind.js --save-dev

Create your own test file with your framework of choice and set up the corresponding fixtures:

const TestKit = require("testkit.wind.js");

describe("My First Test", () => {
    let testKit = TestKit();

    beforeEach(() => {
        testKit.start();
    });

    afterEach(async () => {
        await testKit.stop();
    });

    /// your test goes here
});

By default, TestKit will create a new Actor System with the default configuration. If you want to use your own pre-configured actor system (with your own materialization or supervision) you can pass it through a parameter in the testkit function.

    let testKit = TestKit(myActorSystem);

The testkit promotes decoupling the ActorSystem from the actor itself, so instead of building actor classes, it's better to create an Actor Factory that can receive the ActorSystem. Like this:

module.exports = (Actor) => {
    return class PingActor extends Actor { 
        onReceive() { return "ping"; }
    };
};

Then you can instantiate your own actors in the TestKit:

    let PingActor = require("./ping-actor")(testKit.Actor);
    let myPingActor = new PingActor();

An example of test could be:

    test("that ping always return ping when asked", async () => {
        let myPing = new PingActor();
        let result = await testKit.sendTo(myPing, {}).transactionResult();

        expect(result).resolves.toBe("ping");
    });

Method References

TestKit.mock(onReceive = () => {})

Creates a mock actor that, when receives a message, calls the provided onReceive function. If no function is passed, it calls an empty function.

TestKit.stub()

Creates a stub of an actor that saves all received messages on a store that can be queried later with mailboxOf.

TestKit.sendTo(actor, message)

Setups a transaction with the actor and the sent message. Returns a SendToDSL instance.

SendToDSL.stateOf()

Returns the state of the actor after processing the message. Example:

 let actor = testKit.mock(function () { this.state = true });
let state = await testKit.sendTo(actor, {}).stateOf();

expect(state).toEqual({ id: actor.id, state: true });

SendToDSL.mailboxOf(stubActor)

Returns the current mailbox of an stub actor. Example:

let pongActor = testKit.stub();
let pingActor = testKit.mock(function () { this.tell(pongActor, { happy: true }) });

let [message] = await testKit.sendTo(pingActor, {}).mailboxOf(pongActor);

expect(message).toEqual({ happy: true });

SendToDSL.transactionResult()

Returns a promise with the current result of the transaction. If everything went well, the promise will be resolved with the result of the transaction, in case of failures, the promise will be rejected with the exception.

Example of Success:

let actor = testKit.mock(function () { return "happy" });
let mood = testKit.sendTo(actor, {}).transactionResult();

expect(mood).resolves.toBe("happy");

Example of Failure:

let actor = testKit.mock(function () { throw "sad" });
let mood = testKit.sendTo(actor, {}).transactionResult();

expect(mood).rejects.toBe("sad");

Contribution PRs Welcome Issues Welcome

PR and issues are always welcome as a quick way of contributing to the project. Remember to be polite, this is a open source project and ordinary requirements for PRs and issues are also a requirement.

If you want to be a long-term contributor and participate actively on the design of new features on the project, contact us! Check the package.json to see who you need to contact.

Logo

Player graphic by pixel_perfect from Flaticon is licensed under CC BY 3.0. Check out the new logo that I created on LogoMakr.com https://logomakr.com/8CWVt1