From eb2be7caf6c88a41a184ef5ea6e4675f0f372771 Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Wed, 27 May 2020 18:39:01 +0100 Subject: Export Mitt types for TS consumers (#101) * Export Mitt types for TS consumers * Add rudimentary tests for exported TS types * Run tests against the generated output instead of src --- package.json | 11 +++++++++-- src/index.ts | 12 ++++++------ test/index.js | 20 +++++++++++++++++--- test/types.ts | 20 ++++++++++++++++++++ tsconfig.json | 11 +++++++++++ 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 test/types.ts create mode 100644 tsconfig.json diff --git a/package.json b/package.json index 08b12d2..36fb941 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,10 @@ "umd:main": "dist/mitt.umd.js", "typings": "dist/index.d.ts", "scripts": { - "testonly": "mocha --require esm --require ts-node/register test/**/*.js", + "test": "npm-run-all --silent typecheck lint testonly", + "testonly": "mocha --require esm test/**/*.js", "lint": "eslint src test --ext ts --ext js", - "test": "tsc src/index.ts --noEmit && npm run lint && npm run testonly", + "typecheck": "tsc **/*.ts --noEmit", "bundle": "microbundle", "build": "npm-run-all --silent clean -p bundle -s docs", "clean": "rimraf dist", @@ -23,6 +24,7 @@ "keywords": [ "events", "eventemitter", + "emitter", "pubsub" ], "homepage": "https://github.com/developit/mitt", @@ -47,6 +49,7 @@ "env": { "browser": true, "mocha": true, + "jest": false, "es6": true }, "globals": { @@ -57,12 +60,16 @@ 2, "always" ], + "jest/valid-expect": 0, "@typescript-eslint/no-explicit-any": 0, "@typescript-eslint/explicit-function-return-type": 0, "@typescript-eslint/explicit-module-boundary-types": 0, "@typescript-eslint/no-empty-function": 0 } }, + "eslintIgnore": [ + "dist" + ], "devDependencies": { "@types/chai": "^4.2.11", "@types/mocha": "^7.0.2", diff --git a/src/index.ts b/src/index.ts index f8e7e64..cfb1cf3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,16 @@ -type EventType = string | symbol; +export type EventType = string | symbol; // An event handler can take an optional event argument // and should not return a value -type Handler = (event?: any) => void; -type WildcardHandler= (type: EventType, event?: any) => void +export type Handler = (event?: any) => void; +export type WildcardHandler= (type: EventType, event?: any) => void // An array of all currently registered event handlers for a type -type EventHandlerList = Array; -type WildCardEventHandlerList = Array; +export type EventHandlerList = Array; +export type WildCardEventHandlerList = Array; // A map of event types and their corresponding event handlers. -type EventHandlerMap = Map; +export type EventHandlerMap = Map; export interface Emitter { on(type: EventType, handler: Handler): void; diff --git a/test/index.js b/test/index.js index dceb4b7..a837d36 100644 --- a/test/index.js +++ b/test/index.js @@ -1,11 +1,25 @@ -import mitt from '../src'; +import mitt from '..'; import chai, { expect } from 'chai'; import { spy } from 'sinon'; import sinonChai from 'sinon-chai'; chai.use(sinonChai); -it('should default export be a function', () => { - expect(mitt).to.be.a('function'); +describe('mitt', () => { + it('should default export be a function', () => { + expect(mitt).to.be.a('function'); + }); + + it('should accept an optional event handler map', () => { + expect(() => mitt(new Map())).not.to.throw; + const map = new Map(); + const a = spy(); + const b = spy(); + map.set('foo', [a, b]); + const events = mitt(map); + events.emit('foo'); + expect(a).to.have.been.calledOnce; + expect(b).to.have.been.calledOnce; + }); }); describe('mitt#', () => { diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..23334bb --- /dev/null +++ b/test/types.ts @@ -0,0 +1,20 @@ +import mitt, { EventHandlerList, EventHandlerMap } from '..'; + +const events = mitt(); +function foo() {} +events.on('foo', foo); +events.emit('foo', 'hello'); + +// handler return type should be ignored: +events.on('foo', async e => e * 42); + +// event map type +const map = new Map([ + ['foo', [foo]] +]); +const events2 = mitt(map); +events2.emit('foo', 'hello'); + +// event map type & iterables +const map2 : EventHandlerMap = new Map(Object.entries(({ foo: [foo] }))); +mitt(map2); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2610831 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "noEmit": true, + "declaration": true, + "moduleResolution": "node" + }, + "exclude": [ + "test" + ] +} -- cgit v1.2.3