@interaktiv/mibuilder-core
Version:
Core libraries to interact with MiBuilder projects.
90 lines (81 loc) • 3.06 kB
JavaScript
;
var _mibuilderError = require("./mibuilder-error");
var _testSetup = require("./test-setup");
// Setup the test environment.
(0, _testSetup.testSetup)();
describe('constructor', () => {
it('should return a mutable MiBuilderError', () => {
const msg = 'this is a test message';
const err = new _mibuilderError.MiBuilderError(msg);
expect(err.message).toBe(msg);
expect(err.name).toBe('MiBuilderError');
expect(err.actions).toBeUndefined();
expect(err.exitCode).toBe(1);
const actions = ['Do this action', 'Do that action'];
err.actions = actions;
expect(err.actions).toEqual(actions);
err.exitCode = 100;
expect(err.exitCode).toBe(100);
});
});
describe('wrap', () => {
it('should return a wrapped error', () => {
const myErrorMsg = 'yikes! What did you do?';
const myErrorName = 'OhMyError';
const myError = new Error(myErrorMsg);
myError.name = myErrorName;
const myMiBuilderError = _mibuilderError.MiBuilderError.wrap(myError);
expect(myMiBuilderError).toEqual(expect.any(_mibuilderError.MiBuilderError));
expect(myMiBuilderError.message).toBe(myErrorMsg);
expect(myMiBuilderError.name).toBe(myErrorName);
expect(myMiBuilderError.stack).toMatch('Outer stack:\n');
expect(myMiBuilderError.stack).toMatch(myError.stack);
});
it('should return a wrapped error with a code', () => {
class CodeError extends Error {}
const myErrorCode = 'OhMyError';
const myError = new CodeError('test');
myError.code = myErrorCode;
const myMiBuilderError = _mibuilderError.MiBuilderError.wrap(myError);
expect(myMiBuilderError).toEqual(expect.any(_mibuilderError.MiBuilderError));
expect(myMiBuilderError.code).toBe(myErrorCode);
});
it('should return a new error with just a string', () => {
const myMiBuilderError = _mibuilderError.MiBuilderError.wrap('test');
expect(myMiBuilderError).toEqual(expect.any(_mibuilderError.MiBuilderError));
expect(myMiBuilderError.message).toEqual('test');
});
});
describe('toObject', () => {
it('should return the proper JSON object WITH commandName and data', () => {
const message = 'its a trap!';
const name = 'BadError';
const actions = ['do the opposite'];
const exitCode = 100;
const commandName = 'TestCommand1';
const data = {
foo: 'pity the foo'
};
const mibuilderError = new _mibuilderError.MiBuilderError(message, name, actions, exitCode);
mibuilderError.setCommandName(commandName).setData(data);
expect(mibuilderError.toObject()).toStrictEqual({
name,
message,
exitCode,
actions,
commandName,
data
});
});
it('should return the proper JSON object WITHOUT commandName and data', () => {
const message = "it's a trap!";
const name = 'BadError';
const mibuilderError = new _mibuilderError.MiBuilderError(message, name);
expect(mibuilderError.toObject()).toStrictEqual({
name,
message,
exitCode: 1,
actions: undefined
});
});
});