Skip to content

Commit

Permalink
fix: move from serialport/node-serialport (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot authored Feb 5, 2022
1 parent c53eda7 commit aea1368
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 36 deletions.
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Typescript Library Template
# @serialport/binding-mock

[![Release](https://github.com/reconbot/typescript-library-template/actions/workflows/test.yml/badge.svg)](https://github.com/reconbot/typescript-library-template/actions/workflows/test.yml)
```ts
import { MockBinding } from '@serialport/binding-mock'
const MockBinding = new MockBinding()

This is an example project for shipping typescript using the rules layed out by [@southpolesteve](https://twitter.com/southpolesteve) in his ["Shipping Typescript to NPM"](https://speakerdeck.com/southpolesteve/shipping-typescript-to-npm?slide=10) talk that he gave at NYC typescript.

It gives you a library in UMD and ESM that's rolled up with rollup and includes rolled up types. It makes browser users, node users and me very happy.

Also includes eslint, mocha, semantic-release and github actions. Now updated to include the exports directive in the package.json.

## Guide

- Set the repo secret `NPM_TOKEN` before your first push so that you can publish to npm.
- Change all references in package.json to your own project name
- If you want external dependencies, add them to the `external` section in the `rollup.config.js` otherwise they will be bundled in the library.
MockBinding.createPort('/dev/fakePort', { echo: true })
await MockBinding.write(Buffer.from('data')))
```
8 changes: 0 additions & 8 deletions lib/foo-test.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/foo.ts

This file was deleted.

92 changes: 92 additions & 0 deletions lib/index-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { MockBinding } from '.'
import { OpenOptions } from '@serialport/bindings-interface'
import { assert } from 'chai'

export const shouldReject = async (promise: Promise<unknown>, errType = Error, message = 'Should have rejected') => {
try {
await promise
} catch (err) {
assert.instanceOf(err, errType)
return err
}
throw new Error(message)
}

const openOptions: OpenOptions = {
path: '/dev/exists',
baudRate: 9600,
dataBits: 8,
lock: false,
stopBits: 1,
parity: 'none',
rtscts: false,
xon: false,
xoff: false,
xany: false,
hupcl: false,
}

describe('MockBinding', () => {
afterEach(() => {
MockBinding.reset()
})

describe('instance method', () => {
describe('open', () => {
describe('when phony port not created', () => {
it('should reject', async () => {
await shouldReject(MockBinding.open(openOptions))
})
})

describe('when phony port created', () => {
beforeEach(() => {
MockBinding.createPort('/dev/exists')
})

it('should open the phony port', async () => {
const port = await MockBinding.open(openOptions)
assert.isTrue(port.isOpen)
})

it('should have a "port" prop with "info.serialNumber" prop', async () => {
const port = await MockBinding.open(openOptions)
assert.strictEqual(port.port.info.serialNumber, '1')
})
})
})
})

describe('static method', () => {
describe('createPort', () => {
it('should increment the serialNumber', async () => {
MockBinding.createPort('/dev/exists')
MockBinding.createPort('/dev/ttyUSB1')
const port1 = await MockBinding.open(openOptions)
const port2 = await MockBinding.open({ ...openOptions, path: '/dev/ttyUSB1' })
assert.strictEqual(port1.port.info.serialNumber, '1')
assert.strictEqual(port2.port.info.serialNumber, '2')
})
})
describe('reset', () => {
beforeEach(async () => {
MockBinding.createPort('/dev/exists')
const port = await MockBinding.open(openOptions)
assert.strictEqual(port.port?.info.serialNumber, '1')
await port.close()
})

it('should delete any configured phony ports', async () => {
MockBinding.reset()
await shouldReject(MockBinding.open(openOptions))
})

it('should reset the serialNumber assigned to the phony port', async () => {
MockBinding.reset()
MockBinding.createPort('/dev/exists')
const port = await MockBinding.open(openOptions)
assert.strictEqual(port.port.info.serialNumber, '1')
})
})
})
})
Loading

0 comments on commit aea1368

Please sign in to comment.