node-serialport icon indicating copy to clipboard operation
node-serialport copied to clipboard

Introduce MockPortBinding.writeToPort to help with testing serialport-based code

Open Lucas-C opened this issue 3 years ago • 2 comments

💥 Proposal

What feature you'd like to see

Provide a way for MockPortBinding to write data to another MockPortBinding.

Motivation / Pitch

Often when testing a software application based on node-serialport, we have several devices (2 a least) communicating through serial ports. It can then be useful to mock the behaviour of the program on the other end of the serial port. This is currently difficult to do with @serialport/binding-mock that only provides the ability to send data to itself and/or record it.

Pull Request

I submitted the following PR to provide this feature, including an example use case: https://github.com/serialport/binding-mock/pull/20

Lucas-C avatar Nov 17 '22 13:11 Lucas-C

Good ideas! I left a comment

reconbot avatar Nov 17 '22 14:11 reconbot

I think what we really want is to mock the devices we connect to. My unit tests aren't about whether I'm using SerialPort correctly as much as they are about whether my code is communicating with the external device. To that end I need to build a test module that effectively connects to the back end of a mock serial port. Writes to the mock port become 'data' events on my mock device. Writes from my mock device become 'data' events on the port.

It's so close, but right now as best I can tell I can only do loop back and inspect recordings. That doesn't let me mock the device's protocol.

jmbldwn avatar Nov 25 '23 18:11 jmbldwn