Skip to content

Commit

Permalink
Migrate to TypeScript (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
whymarrh authored Feb 13, 2020
1 parent f7677f6 commit c3a5843
Show file tree
Hide file tree
Showing 18 changed files with 4,177 additions and 118 deletions.
81 changes: 81 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
version: 2.1

workflows:
build-test:
jobs:
- prep-deps
- test-build:
requires:
- prep-deps
- test-lint:
requires:
- prep-deps
- test-unit:
requires:
- prep-deps
- all-tests-pass:
requires:
- test-build
- 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-build:
docker:
- image: circleci/node:10
steps:
- checkout
- attach_workspace:
at: .
- run:
name: Build project
command: yarn build

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
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.js
!.eslintrc.js
!jest.config.js
node_modules
dist
coverage
*.d.ts
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
root: true,
extends: [
'@metamask/eslint-config',
'@metamask/eslint-config/config/jest',
'@metamask/eslint-config/config/nodejs',
'@metamask/eslint-config/config/typescript',
],
};
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto
*.d.ts linguist-generated=true

# 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
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

0 comments on commit c3a5843

Please sign in to comment.