generated from reconbot/typescript-library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move from serialport/node-serialport (#2)
- Loading branch information
Showing
8 changed files
with
503 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.