Skip to content

Commit

Permalink
move to recommended async test style, minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithan committed Nov 4, 2019
1 parent 1893934 commit dffdb09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/cmd/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,12 +921,11 @@ module.exports = class SerialCommand {
}

/**
* This is a wrapper function created so _serialWifiConfig can return the
* true promise state, but the wrapper can still act like it handled any
* failures gracefully as it acted before testing was attempted.
* This is a wrapper function so _serialWifiConfig can return the
* true promise state for testing.
*/
serialWifiConfig(...args) {
return this._serialWifiConfig.apply(this, args)
return this._serialWifiConfig(...args)
.then(() => {
console.log('Done! Your device should now restart.');
}, (err) => {
Expand Down Expand Up @@ -1221,16 +1220,16 @@ module.exports = class SerialCommand {
serialPort.drain();

// In case device is not in listening mode.
startTimeout(5000, 'Serial timed out while initially listening to device, please ensure device is in listening mode with particle usb start-listening');
startTimeout(5000, 'Serial timed out while initially listening to device, please ensure device is in listening mode with particle usb start-listening', 'InitialTimeoutError');
});

function serialClosedEarly(){
reject('Serial port closed early');
}

function startTimeout(to, message = timeoutError){
function startTimeout(to, message = timeoutError, name = 'TimeoutError'){
self._serialTimeout = setTimeout(() => {
reject(message);
reject(VError({name}, message));
}, to);
}

Expand Down
33 changes: 24 additions & 9 deletions src/cmd/serial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,34 @@ describe('Serial Command', () => {
});
});

describe('serialWifiConfig', () => {
it('can reject with timeout after 5000ms if not getting any serial data', () => {
describe('serialWifiConfig', async () => {
it('can reject with timeout after 5000ms if not getting any serial data', async () => {
clock = sinon.useFakeTimers();
const device = { port: 'baltimore' };
const mockSerial = new MockSerial();

mockSerial.write = function write(data) {
if (data === 'w') {
// This next tick allows _serialWifiConfig to set the timeout before we move the clock foward.
process.nextTick(() => {
clock.tick(5010);
});
}
};

serial.serialPort = mockSerial;
const wifiPromise = serial._serialWifiConfig(device);
// This allows _serialWifiConfig's internal promises to run and try to
// connect to the serial device before moving time forward.
process.nextTick(() => {
clock.tick(5010);
});
return expect(wifiPromise).to.be.rejected;


let error;

try {
await serial._serialWifiConfig(device);
} catch (e) {
error = e;
}

expect(error).to.be.an.instanceOf(Error);
expect(error).to.have.property('name', 'InitialTimeoutError');
});
});
});
Expand Down

0 comments on commit dffdb09

Please sign in to comment.