Skip to content

Commit

Permalink
test: Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 5, 2020
1 parent 6c42b24 commit c2cb9b1
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 98 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
strategy:
matrix:
os: [ macos-latest, windows-latest, ubuntu-latest ]
node-version: [ 14.x ]
# node-version: [ 8.x, 10.x, 12.x, 14.x ]
node-version: [ 8.x, 10.x, 12.x, 14.x ]

env:
OS: ${{ matrix.os }}
Expand Down
38 changes: 0 additions & 38 deletions CHANGELOG.md

This file was deleted.

38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Events <!-- omit in toc -->
# Events

`@foxify/events` is a high performance EventEmitter alternative for Node.js and browser that has been optimized to be faster than the native version, (why not?!).

Expand Down Expand Up @@ -35,6 +35,8 @@ This module is API compatible with the EventEmitter that ships by default with N
- [Strict events](#strict-events)
- [Contextual emits](#contextual-emits)
- [Benchmarks](#benchmarks)
- [Tests](#tests)
- [Coverage](#coverage)
- [Versioning](#versioning)
- [Authors](#authors)
- [License](#license)
Expand All @@ -47,8 +49,16 @@ npm i @foxify/events

## Usage

JavaScript:

```js
const { EventEmitter } = require("@foxify/events");
const EventEmitter = require("@foxify/events").default;
```

TypeScript:

```ts
import EventEmitter from "@foxify/events";
```

For the API documentation, please follow the official Node.js [documentation](https://nodejs.org/api/events.html).
Expand All @@ -71,14 +81,14 @@ class Emitter extends EventEmitter<Events> {

const eventEmitter = new Emitter();

// Throws an error, since this event requires the "bar" argument of type "string"
eventEmitter.emit("foo");
// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
eventEmitter.on("foo", bar => 1);

// Works just fine
// Throws an error (TS compile time), since this event requires the "bar" argument of type "string"
eventEmitter.emit("foo");

// Works just fine. so don't worry about "ImplicitAny" config, since type of "bar" is defined as "string"
eventEmitter.on("foo", bar => 1);
// Works just fine
eventEmitter.emit("foo", "bar");

```

Expand Down Expand Up @@ -107,10 +117,22 @@ eventEmitter.prependOnceListener("event:5", emitted, context);

## Benchmarks

```bash
```shell
npm run benchmarks
```

## Tests

```shell
npm test
```

## Coverage

```shell
npm run coverage
```

## Versioning

We use [SemVer](http://semver.org) for versioning. For the versions available, see the [tags on this repository](https://github.com/foxifyjs/events/tags).
Expand Down
8 changes: 8 additions & 0 deletions __tests__/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,11 @@ it("emits to all event listeners", () => {

expect(pattern.join(";")).toBe("foo1;foo2");
});

it("should throw the error", () => {
const e = new EventEmitter();

const error = new Error("test");

expect(() => e.emit("error", error)).toThrow(error);
});
13 changes: 10 additions & 3 deletions __tests__/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ it("returns an array of function", () => {

e.on("foo", foo);

expect(e.listeners("foo")).toBeInstanceOf(Array);
expect(e.listeners("foo").length).toBe(1);
expect(e.listeners("foo")).toEqual([foo]);
// eslint-disable-next-line @typescript-eslint/no-empty-function
function bar() {}

e.on("foo", bar);

const listeners = e.listeners("foo");

expect(listeners).toBeInstanceOf(Array);
expect(listeners.length).toBe(2);
expect(listeners).toEqual([foo, bar]);
});

it("is not vulnerable to modifications", () => {
Expand Down
46 changes: 39 additions & 7 deletions __tests__/once.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import EventEmitter from "../src";

it("only emits it once", () => {
it("should only emits it once (case 1)", () => {
const e = new EventEmitter();
let calls = 0;

e.once("foo", () => {
calls++;
});
const listener = jest.fn();

e.once("foo", listener);

e.emit("foo");
e.emit("foo");
Expand All @@ -15,7 +14,35 @@ it("only emits it once", () => {
e.emit("foo");

expect(e.listeners("foo").length).toBe(0);
expect(calls).toBe(1);
expect(listener).toBeCalledTimes(1);
});

it("should only emits it once (case 2)", () => {
const e = new EventEmitter();

const listener = jest.fn();

e.once("foo", listener);

e.emit("foo");
e.emit("foo");

expect(e.listeners("foo").length).toBe(0);
expect(listener).toBeCalledTimes(1);
});

it("should only emits it once (case 3)", () => {
const e = new EventEmitter();

const listener = jest.fn();

e.once("foo", listener);
e.once("foo", listener);

e.emit("foo");

expect(e.listeners("foo").length).toBe(0);
expect(listener).toBeCalledTimes(2);
});

it("only emits once if emits are nested inside the listener", () => {
Expand Down Expand Up @@ -63,6 +90,8 @@ it("only emits once for multiple events", () => {
});

it("only emits once with context", (done) => {
expect.assertions(4);

const context = { foo: "bar" };
const e = new EventEmitter();

Expand All @@ -75,5 +104,8 @@ it("only emits once with context", (done) => {
done();
},
context,
).emit("foo", "bar");
);

expect(e.emit("foo", "bar")).toBe(true);
expect(e.listenerCount("foo")).toBe(0);
});
20 changes: 20 additions & 0 deletions __tests__/prependListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import EventEmitter from "../src";

it("should prepend the listener", () => {
const e = new EventEmitter();
const calls: number[] = [];

e.on("foo", () => {
calls.push(1);
});

e.prependListener("foo", () => {
calls.push(2);
});

e.emit("foo");

expect(e.listeners("foo").length).toBe(2);
expect(e.listenerCount("foo")).toBe(2);
expect(calls).toEqual([2, 1]);
});
25 changes: 25 additions & 0 deletions __tests__/prependOnceListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import EventEmitter from "../src";

it("should prepend the listener and remove it after first event", () => {
const e = new EventEmitter();
const calls: number[] = [];

e.on("foo", () => {
calls.push(1);
});

e.on("foo", () => {
calls.push(2);
});

e.prependOnceListener("foo", () => {
calls.push(3);
});

e.emit("foo");
e.emit("foo");

expect(e.listeners("foo").length).toBe(2);
expect(e.listenerCount("foo")).toBe(2);
expect(calls).toEqual([3, 1, 2, 1, 2]);
});
6 changes: 6 additions & 0 deletions __tests__/removeAllListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ it("removes all events, literally!", () => {
expect(e.emit("bar")).toBe(false);
expect(e.emit("aaa")).toBe(false);
});

it("should not fail if the events don't exist", () => {
const e = new EventEmitter();

expect(e.removeAllListeners("not-found")).toEqual(e);
});
6 changes: 6 additions & 0 deletions __tests__/removeListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ it("removes only the first listener matching the specified listener", () => {
expect(e.listeners("bar")).toEqual([bar]);
expect(e.listeners("foo")).toEqual([]);
});

it("should not fail if the events don't exist", () => {
const e = new EventEmitter();

expect(e.removeListener("not-found", () => 1)).toEqual(e);
});
2 changes: 1 addition & 1 deletion benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Benchmarks

```bash
```shell
Starting benchmark add-remove.js

╔══════════════════════╤══════════════════════╤══════════════════════╤══════════════════════╤══════════════════════╗
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"benchmarks": "find benchmarks -maxdepth 1 -name '*.js' -exec benchmarks/start.sh {} \\;"
},
"devDependencies": {
"@types/jest": "^26.0.15",
"@types/jest": "^26.0.16",
"@types/node": "^14.14.10",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"@typescript-eslint/eslint-plugin": "^4.9.0",
"@typescript-eslint/parser": "^4.9.0",
"eslint": "^7.15.0",
"eslint-plugin-jest": "^24.1.3",
"jest": "^26.6.3",
"prettier": "^2.2.1",
Expand Down
1 change: 0 additions & 1 deletion src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,5 @@ export default EventEmitter;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isArray(value: any): value is any[] {
// return typeof value.length === "number";
return value.length !== undefined;
}
30 changes: 0 additions & 30 deletions tslint.json

This file was deleted.

0 comments on commit c2cb9b1

Please sign in to comment.