Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to TypeScript #1

Merged
merged 23 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: 2.1

workflows:
lint:
jobs:
- prep-deps
- test-lint:
requires:
- prep-deps
- test-unit:
requires:
- prep-deps
- all-tests-pass:
requires:
- test-lint
- test-unit

jobs:
prep-deps:
docker:
- image: circleci/node:10
steps:
- checkout
- run:
name: Install deps
command: |
.circleci/scripts/deps-install.sh
- run:
name: Collect yarn install HAR logs
command: |
.circleci/scripts/collect-har-artifact.sh
- persist_to_workspace:
root: .
paths:
- node_modules
- build-artifacts

test-lint:
docker:
- image: circleci/node:10
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Lint
command: yarn lint

test-unit:
docker:
- image: circleci/node:10
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Unit tests
command: yarn test

all-tests-pass:
docker:
- image: circleci/node:10
steps:
- run:
name: All tests passed
command: echo 'Great success'
9 changes: 9 additions & 0 deletions .circleci/scripts/collect-har-artifact.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set -e
set -u
set -o pipefail

mkdir -p build-artifacts/yarn-install-har
mv ./*.har build-artifacts/yarn-install-har/
8 changes: 8 additions & 0 deletions .circleci/scripts/deps-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -x
set -e
set -u
set -o pipefail

yarn --frozen-lockfile --ignore-scripts --har
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
indent_size = 4
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.js
node_modules
dist
coverage
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: [
'@metamask/eslint-config',
'@metamask/eslint-config/config/jest',
'@metamask/eslint-config/config/typescript',
],
};
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto

# Reviewing the lockfile contents is an important step in verifying that
# we're using the dependencies we expect to be using
package-lock.json linguist-generated=false
yarn.lock linguist-generated=false
19 changes: 3 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.js
*.d.ts
*.map

# Created by https://www.gitignore.io/api/osx,node

### Node ###
# Logs
logs
*.log
Expand All @@ -24,21 +24,8 @@ coverage
# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/
Expand Down
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
.editorconfig
.eslintignore
.eslintrc.js
.gitattributes
*.tgz
*.ts
!*.d.ts
tsconfig.json
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v10
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ API is the same as `EventEmitter`.
### usage

```js
const SafeEventEmitter = require('safe-event-emitter')
import SafeEventEmitter from 'safe-event-emitter'

const ee = new SafeEventEmitter()
ee.on('boom', () => { throw new Error() })
Expand Down
84 changes: 0 additions & 84 deletions index.js

This file was deleted.

79 changes: 79 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import SafeEventEmitter from './index';

describe('safeEventEmitter', () => {
it('can be constructed without error', () => {
expect(new SafeEventEmitter()).toBeDefined();
});

it('can emit a value with no listeners', () => {
const see = new SafeEventEmitter();
expect(see.emit('foo', 42)).toBe(false);
});

it('can emit a value with 1 listeners', () => {
expect.assertions(2);

const see = new SafeEventEmitter();
see.on('foo', (x) => expect(x).toBe(42));
expect(see.emit('foo', 42)).toBe(true);
});

it('can emit a value with 2 listeners', () => {
expect.assertions(3);

const see = new SafeEventEmitter();
see.on('foo', (x) => expect(x).toBe(42));
see.on('foo', (x) => expect(x).toBe(42));
expect(see.emit('foo', 42)).toBe(true);
});

it('returns false when _events is somehow undefined', () => {
const see = new SafeEventEmitter();
see.on('foo', () => { /* */ });
delete (see as any)._events;
expect(see.emit('foo', 42)).toBe(false);
});

it('throws error from handler after setTimeout', () => {
jest.useFakeTimers();
const see = new SafeEventEmitter();
see.on('boom', () => {
throw new Error('foo');
});
expect(() => {
see.emit('boom');
}).not.toThrow();
expect(() => {
jest.runAllTimers();
}).toThrow('foo');
});

it('throws error emitted when there is no error handler', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error', new Error('foo'));
}).toThrow('foo');
});

it('throws error emitted when there is no error handler AND _events is somehow undefined', () => {
const see = new SafeEventEmitter();
delete (see as any)._events;
expect(() => {
see.emit('error', new Error('foo'));
}).toThrow('foo');
});

it('throws default error when there is no error handler and error event emitted', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error');
}).toThrow('Unhandled error.');
});

it('throws error when there is no error handler and error event emitted', () => {
const see = new SafeEventEmitter();
expect(() => {
see.emit('error', { message: 'foo' });
}).toThrow('Unhandled error. (foo)');
});
});
Loading