navy
Version:
Quick and powerful development environments using Docker and Docker Compose
91 lines (89 loc) • 3.78 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _util = require("../util");
/* eslint-env mocha */
describe('cli/doctor/util', function () {
let sandbox;
let consoleLogStub;
beforeEach(function () {
sandbox = _sinon.default.createSandbox();
consoleLogStub = sandbox.stub(console, 'log');
});
afterEach(function () {
sandbox.restore();
});
describe('fix', function () {
it('should throw an invariant violation when no callback is provided', async function () {
let caught;
try {
await (0, _util.fix)('a message');
} catch (err) {
caught = err;
}
(0, _chai.expect)(caught).to.be.an('error');
(0, _chai.expect)(caught.message).to.contain('DOCTOR_FIX_NO_PARAMS');
});
it('should print the formatted message and call the callback', async function () {
const callback = sandbox.stub().resolves();
await (0, _util.fix)('Found %s, removing', 'env-1', callback);
(0, _chai.expect)(consoleLogStub.calledOnce).to.equal(true);
const printed = consoleLogStub.firstCall.args[0];
(0, _chai.expect)(printed).to.contain('Found env-1, removing');
(0, _chai.expect)(callback.calledOnce).to.equal(true);
});
it('should await the callback before resolving', async function () {
let resolved = false;
const callback = sandbox.stub().callsFake(() => new Promise(resolve => {
setImmediate(() => {
resolved = true;
resolve();
});
}));
await (0, _util.fix)('msg', callback);
(0, _chai.expect)(resolved).to.equal(true);
});
});
describe('start', function () {
it('should log a dimmed message with arrow prefix', function () {
(0, _util.start)('Doing thing');
(0, _chai.expect)(consoleLogStub.calledOnce).to.equal(true);
const printed = consoleLogStub.getCalls().map(c => c.args.join(' ')).join(' ');
(0, _chai.expect)(printed).to.contain('----->');
(0, _chai.expect)(printed).to.contain('Doing thing');
});
});
describe('catchInvariant', function () {
it('should call the catch callback when fn throws an Invariant Violation matching the code', async function () {
const catchCallback = sandbox.stub().resolves();
const err = new Error('MY_CODE: failure happened');
err.name = 'Invariant Violation';
await (0, _util.catchInvariant)('MY_CODE', async () => {
throw err;
}, catchCallback);
(0, _chai.expect)(catchCallback.calledOnce).to.equal(true);
});
it('should not call the catch callback when fn throws a non-invariant error', async function () {
const catchCallback = sandbox.stub().resolves();
await (0, _util.catchInvariant)('MY_CODE', async () => {
throw new Error('some other error');
}, catchCallback);
(0, _chai.expect)(catchCallback.called).to.equal(false);
});
it('should not call the catch callback when invariant code does not match', async function () {
const catchCallback = sandbox.stub().resolves();
const err = new Error('OTHER_CODE: failure');
err.name = 'Invariant Violation';
await (0, _util.catchInvariant)('MY_CODE', async () => {
throw err;
}, catchCallback);
(0, _chai.expect)(catchCallback.called).to.equal(false);
});
it('should not call the catch callback when fn resolves without throwing', async function () {
const catchCallback = sandbox.stub().resolves();
await (0, _util.catchInvariant)('MY_CODE', async () => undefined, catchCallback);
(0, _chai.expect)(catchCallback.called).to.equal(false);
});
});
});