@charisma-ai/event-emitter-tester

Tester for emitters with on/off interface

Usage no npm install needed!

<script type="module">
  import charismaAiEventEmitterTester from 'https://cdn.skypack.dev/@charisma-ai/event-emitter-tester';
</script>

README

Event Emitter Tester

A utility to make testing event emitters simpler.

Usage

yarn add @charisma-ai/event-emitter-tester
import EventEmitterTester from "@charisma-ai/event-emitter-tester";

class MyTester extends EventEmitterTester {
  constructor(emitter, options = {}) {
    super(emitter, options);
  }

  reply(text: string): MyTester {
    this.addToQueue(() => {
      // do your event here...
    });
    return this;
  }

  awaitMessage(callback: (message: string) => void): MyTester {
    this.awaitEvent("message", callback);
    return this;
  }
}

async function run() {
  const tester = new MyTester(emitter, { timeout: 5000 });
  await tester
    .reply("Hello!")
    .reply("How are you?")
    .awaitMessage(message => expect(message).toEqual("Great thanks"))
    .reply("Ideal!")
    .awaitMessage(message => expect(message).toEqual("Indeed."))
    .run();
  console.log("Complete!");
}