Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove several deps improve testing code #188

Merged
merged 3 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ The customizable Commitizen plugin (or standalone utility) to help achieve consi
Suitable for large teams working with multiple projects with their own commit scopes. It allows you to **select** the pre-defined scopes or commit types. It works perfectly with https://github.com/semantic-release/semantic-release.


[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![Build Status](https://travis-ci.org/leonardoanalista/cz-customizable.svg)](https://travis-ci.org/leonardoanalista/cz-customizable) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![codecov.io](https://codecov.io/github/leonardoanalista/cz-customizable/coverage.svg?branch=master)](https://codecov.io/github/leonardoanalista/cz-customizable?branch=master) [![npm monthly downloads](https://img.shields.io/npm/dm/cz-customizable.svg?style=flat-square)](https://www.npmjs.com/package/cz-customizable)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![npm monthly downloads](https://img.shields.io/npm/dm/cz-customizable.svg?style=flat-square)](https://www.npmjs.com/package/cz-customizable)


You have two ways to use `cz-customizable`. Originally, this project started as a commitizen plugin (Option 1). We introduced the second option to run this `cz-customizable` in standalone mode (Option 2), just like any NodeJS script. It's recommended to use `Option 2` for simplicity. The way you configure is shared between both options.
Expand Down
2 changes: 1 addition & 1 deletion __tests__/build-commit.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const buildCommit = require('../buildCommit');
const buildCommit = require('../lib/build-commit');

describe('buildCommit()', () => {
const answers = {
Expand Down
238 changes: 114 additions & 124 deletions __tests__/cz-customizable.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
const rewire = require('rewire');

// TODO: enable tests
describe.skip('cz-customizable', () => {
let module;
// let cz;
let commit;

beforeEach(() => {
module = rewire('../index.js');

// eslint-disable-next-line no-underscore-dangle
module.__set__({
log: {
info() {},
const czModule = require('./../index');
const readConfigFile = require('../lib/read-config');

const commit = jest.fn();

jest.mock('./../lib/read-config');

beforeEach(() => {
const defaultConfig = {
types: [
{ value: 'feat', name: 'feat: A new feature' },
{ value: 'fix', name: 'fix: A bug fix' },
{ value: 'docs', name: 'docs: Documentation only changes' },
{
value: 'style',
name:
'style: Changes that do not affect the meaning of the code\n' +
' (white-space, formatting, missing semi-colons, etc)',
},

readConfigFile() {
return {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
};
{
value: 'refactor',
name: 'refactor: A code change that neither fixes a bug nor adds a feature',
},
});

// cz = jasmine.createSpyObj('cz', ['prompt', 'Separator']);
commit = jasmine.createSpy();
});
{
value: 'perf',
name: 'perf: A code change that improves performance',
},
{ value: 'test', name: 'test: Adding missing tests' },
{
value: 'chore',
name:
'chore: Changes to the build process or auxiliary tools\n' +
' and libraries such as documentation generation',
},
{ value: 'revert', name: 'revert: Revert to a commit' },
{ value: 'WIP', name: 'WIP: Work in progress' },
],
scopes: [{ name: 'accounts' }, { name: 'admin' }, { name: 'exampleScope' }, { name: 'changeMe' }],
allowTicketNumber: false,
isTicketNumberRequired: false,
ticketNumberPrefix: 'TICKET-',
ticketNumberRegExp: '\\d{1,5}',
messages: {
type: "Select the type of change that you're committing:",
scope: '\nDenote the SCOPE of this change (optional):',
customScope: 'Denote the SCOPE of this change:',
subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
breaking: 'List any BREAKING CHANGES (optional):\n',
footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
confirmCommit: 'Are you sure you want to proceed with the commit above?',
},
allowCustomScopes: true,
allowBreakingChanges: ['feat', 'fix'],
skipQuestions: ['body'],
subjectLimit: 100,
};
readConfigFile.mockReturnValue(defaultConfig);
});

describe('cz-customizable', () => {
function getMockedCz(answers) {
return {
prompt() {
Expand All @@ -54,7 +81,7 @@ describe.skip('cz-customizable', () => {
const mockCz = getMockedCz(answers);

// run commitizen plugin
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith('feat: do it all');
});
Expand All @@ -67,7 +94,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith('feat: with backticks \\`here\\`');
});
Expand All @@ -76,7 +103,7 @@ describe.skip('cz-customizable', () => {
const mockCz = getMockedCz({});

// run commitizen plugin
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).not.toHaveBeenCalled();
});
Expand All @@ -93,7 +120,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith(
'feat(myScope): create a new cool feature\n\n-line1\n-line2\n\nBREAKING CHANGE:\nbreaking\n\nISSUES CLOSED: my footer'
Expand All @@ -109,7 +136,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat(myScope): create a new cool feature');
});

Expand All @@ -121,7 +148,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('WIP: this is my work-in-progress');
});

Expand All @@ -135,7 +162,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

setTimeout(() => {
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
Expand All @@ -153,10 +180,10 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

setTimeout(() => {
expect(commit.wasCalled).toEqual(false);
expect(commit).toHaveBeenCalledTimes(0);
done();
}, 100);
});
Expand All @@ -179,20 +206,19 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

const firstPart = 'feat(myScope): ';

const firstLine = commit.mostRecentCall.args[0].split('\n\n')[0];
expect(firstLine).toEqual(firstPart + answers.subject.slice(0, 100));
expect(commit.mock.calls[0][0]).toMatchInlineSnapshot(`
"${firstPart + answers.subject.slice(0, 100)}

// it should wrap body
const body = commit.mostRecentCall.args[0].split('\n\n')[1];
expect(body).toEqual(`${chars100}\nbody-second-line`);
0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789
body-second-line

// it should wrap footer
const footer = commit.mostRecentCall.args[0].split('\n\n')[2];
expect(footer).toEqual(`ISSUES CLOSED: ${footerChars100}\nfooter-second-line`);
ISSUES CLOSED: 0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-012345
footer-second-line"
`);
});

it('should call commit() function with custom breaking prefix', () => {
Expand All @@ -205,28 +231,19 @@ describe.skip('cz-customizable', () => {
footer: 'my footer',
};

// eslint-disable-next-line no-underscore-dangle
module.__set__({
log: {
info() {},
},

readConfigFile() {
return {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
breakingPrefix: 'WARNING:',
};
readConfigFile.mockReturnValue({
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
breakingPrefix: 'WARNING:',
});

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith(
'feat(myScope): create a new cool feature\n\nWARNING:\nbreaking\n\nISSUES CLOSED: my footer'
Expand All @@ -243,28 +260,19 @@ describe.skip('cz-customizable', () => {
footer: 'my footer',
};

// eslint-disable-next-line no-underscore-dangle
module.__set__({
log: {
info() {},
},

readConfigFile() {
return {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
footerPrefix: 'FIXES:',
};
readConfigFile.mockReturnValue({
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
footerPrefix: 'FIXES:',
});

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith(
'feat(myScope): create a new cool feature\n\nBREAKING CHANGE:\nbreaking\n\nFIXES: my footer'
Expand All @@ -281,28 +289,19 @@ describe.skip('cz-customizable', () => {
footer: 'my footer',
};

// eslint-disable-next-line no-underscore-dangle
module.__set__({
log: {
info() {},
},

readConfigFile() {
return {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
footerPrefix: '',
};
readConfigFile.mockReturnValue({
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
footerPrefix: '',
});

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);

expect(commit).toHaveBeenCalledWith(
'feat(myScope): create a new cool feature\n\nBREAKING CHANGE:\nbreaking\n\nmy footer'
Expand All @@ -319,30 +318,21 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-1234 create a new cool feature');
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-TICKET-1234 create a new cool feature');
});

it('should call commit() function with ticket number and prefix', () => {
// eslint-disable-next-line no-underscore-dangle
module.__set__({
log: {
info() {},
},

readConfigFile() {
return {
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
breakingPrefix: 'WARNING:',
ticketNumberPrefix: 'TICKET-',
};
readConfigFile.mockReturnValue({
types: [{ value: 'feat', name: 'feat: my feat' }],
scopes: [{ name: 'myScope' }],
scopeOverrides: {
fix: [{ name: 'fixOverride' }],
},
allowCustomScopes: true,
allowBreakingChanges: ['feat'],
breakingPrefix: 'WARNING:',
ticketNumberPrefix: 'TICKET-',
});

const answers = {
Expand All @@ -354,7 +344,7 @@ describe.skip('cz-customizable', () => {
};

const mockCz = getMockedCz(answers);
module.prompter(mockCz, commit);
czModule.prompter(mockCz, commit);
expect(commit).toHaveBeenCalledWith('feat(myScope): TICKET-1234 create a new cool feature');
});
});
3 changes: 2 additions & 1 deletion __tests__/questions.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable nada/path-case */
const fs = require('fs');
const questions = require('../questions.js');
const questions = require('../lib/questions.js');

describe('cz-customizable', () => {
let config;
Expand Down Expand Up @@ -268,6 +268,7 @@ describe('cz-customizable', () => {
});
});

// TODO: fix tests
describe.skip('commit already prepared', () => {
let existsSync;
let readFileSync;
Expand Down
Loading