Skip to content

Commit

Permalink
refactor: rename colour by color
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecasar committed Feb 1, 2024
1 parent 8c54e3f commit 4827807
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/errors/mock-runner-error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { paintText, colourCodes } from '../helpers/colours.js';
import { paintText, colorCodes } from '../helpers/colors.js';
import { Logger } from '../helpers/logger.js';

export class MockRunnerError extends Error {
Expand Down Expand Up @@ -29,19 +29,19 @@ export class MockRunnerError extends Error {
let type = '';
switch (this.level) {
case 2:
type = colourCodes.fg.red;
type = colorCodes.fg.red;
break;
case 1:
type = colourCodes.fg.crimson;
type = colorCodes.fg.crimson;
break;
default:
type = colourCodes.fg.cyan;
type = colorCodes.fg.cyan;
}
Logger.error(
`Error of level ${paintText(this.level.toString(), type)}, type ${paintText(
this.code.toString(),
colourCodes.fg.gray
)} over ${paintText(this.emitter, colourCodes.fg.blue)}`
colorCodes.fg.gray
)} over ${paintText(this.emitter, colorCodes.fg.blue)}`
);
Logger.info(`${this.stack}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/colours.js → src/helpers/colors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const colourCodes = Object.freeze({
export const colorCodes = Object.freeze({
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
Expand Down Expand Up @@ -39,5 +39,5 @@ export const colourCodes = Object.freeze({
* @returns {string} - The painted text.
*/
export function paintText(text, color) {
return `${color}${text}${colourCodes.reset}`;
return `${color}${text}${colorCodes.reset}`;
}
13 changes: 6 additions & 7 deletions test/unit/errors/mock-runner-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import esmock from 'esmock';
import { createSandbox, match } from 'sinon';
import sinonChai from 'sinon-chai';

import { colourCodes } from '../../../src/helpers/colours.js';
import { colorCodes } from '../../../src/helpers/colors.js';
import { globalMocksFactory } from '../../helpers/global-mocks-factory.js';

use(sinonChai);
Expand All @@ -14,12 +14,11 @@ class Logger {
}

const mocks = {
'../helpers/logger.js': { Logger },
'../../../src/helpers/logger.js': { Logger },
};
const globalMocks = globalMocksFactory(sandbox);
const fileToTest = '../../../src/errors/mock-runner-error.js';
const absolutePath = new URL(fileToTest, import.meta.url).pathname;
const { MockRunnerError } = await esmock(absolutePath, absolutePath, mocks, globalMocks);
const { MockRunnerError } = await esmock(fileToTest, import.meta.url, mocks, globalMocks);

// Use Sinon-Chai assertions
use(sinonChai);
Expand Down Expand Up @@ -50,23 +49,23 @@ describe('unit: MockRunnerError', () => {
const error = new MockRunnerError('Test error', 500, 2, 'testEmitter');
error.showError();

expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colourCodes.fg.red)));
expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colorCodes.fg.red)));
expect(Logger.info).to.have.been.calledOnceWithExactly(error.stack);
});

it('should log the error information and use crimson color with a type 2', () => {
const error = new MockRunnerError('Test error', 500, 1, 'testEmitter');
error.showError();

expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colourCodes.fg.crimson)));
expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colorCodes.fg.crimson)));
expect(Logger.info).to.have.been.calledOnceWithExactly(error.stack);
});

it('should log the error information and use cyan color with a type undefined', () => {
const error = new MockRunnerError('Test error', 500, undefined, 'testEmitter');
error.showError();

expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colourCodes.fg.cyan)));
expect(Logger.error).to.have.been.calledWith(match((value) => value.includes(colorCodes.fg.cyan)));
expect(Logger.info).to.have.been.calledOnceWithExactly(error.stack);
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/unit/helpers/colours.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect, use } from 'chai';
import sinonChai from 'sinon-chai';

import { paintText, colourCodes } from '../../../src/helpers/colours.js';
import { paintText, colorCodes } from '../../../src/helpers/colors.js';

use(sinonChai);

describe('unit: colours', () => {
it('should have the correct colours', () => {
expect(colourCodes).to.deep.equal({
describe('unit: colors', () => {
it('should have the correct colors', () => {
expect(colorCodes).to.deep.equal({
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
Expand Down Expand Up @@ -45,8 +45,8 @@ describe('unit: colours', () => {
describe('paintText', () => {
it('should return the text with the specified color', () => {
const text = 'Hello, World!';
const color = colourCodes.fg.red;
const expected = `${color}${text}${colourCodes.reset}`;
const color = colorCodes.fg.red;
const expected = `${color}${text}${colorCodes.reset}`;
const result = paintText(text, color);
expect(result).to.equal(expected);
});
Expand Down

0 comments on commit 4827807

Please sign in to comment.