Skip to content

Commit

Permalink
test: Add test for commitlint functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kevintyj committed Nov 11, 2023
1 parent dccb30f commit f03b11d
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 9 deletions.
48 changes: 48 additions & 0 deletions __tests__/errHandle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import * as core from '@actions/core';
import handleError from '../src/errHandle';

const debugErr = vi.spyOn(core, 'error');
const debugFail = vi.spyOn(core, 'setFailed');

describe('error handler', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('simple string error calls error and fail once', () => {
handleError('error');
expect(debugErr).toBeCalledWith('error');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(1);
});
it('simple string warning calls error once but does not block', () => {
handleError('error', false);
expect(debugErr).toBeCalledWith('error');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(0);
});
it('class error calls error and fail once', () => {
handleError(new Error('error'));
expect(debugErr).toBeCalledWith(new Error('error'));
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(1);
});
it('class error warning calls error once but does not block', () => {
handleError(new Error('error'), false);
expect(debugErr).toBeCalledWith(new Error('error'));
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(0);
});
it('unknown error calls error and fail once', () => {
handleError(false);
expect(debugErr).toBeCalledWith('Unknown error has occurred!');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(1);
});
it('unknown error warning calls error once but does not block', () => {
handleError(false, false);
expect(debugErr).toBeCalledWith('Unknown error has occurred!');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(0);
});
});
74 changes: 74 additions & 0 deletions __tests__/lint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import process from 'node:process';
import { describe, expect, it } from 'vitest';
import load from '@commitlint/load';
import { testLintOptions, verifyTitle } from '../src/lint';

const { getLintOptions } = testLintOptions;

const emptyConfigOption = {
defaultIgnores: true,
helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
ignores: undefined,
parserOpts: {
breakingHeaderPattern: /^(\w*)(?:\((.*)\))?!: (.*)$/,
headerCorrespondence: [
'type',
'scope',
'subject',
],
headerPattern: /^(\w*)(?:\((.*)\))?!?: (.*)$/,
issuePrefixes: [
'#',
],
noteKeywords: [
'BREAKING CHANGE',
'BREAKING-CHANGE',
],
revertCorrespondence: [
'header',
'hash',
],
revertPattern: /^(?:Revert|revert:)\s"?([\s\S]+?)"?\s*This reverts commit (\w*)\./i,
},
plugins: {},
};

const emptyConfigOptionNoParserOpts = {
defaultIgnores: true,
helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
ignores: undefined,
parserOpts: undefined,
plugins: {},
};

describe('commitlint', async () => {
const emptyConfig = await load({});
const defaultConfig = await load({ extends: '@commitlint/config-conventional' });
const currentConfig = await load({}, { file: 'commitlint.config.js', cwd: process.cwd() });

it('configurations return proper extensions and rules', () => {
expect(emptyConfig).toHaveProperty('extends', ['@commitlint/config-conventional']);
expect(defaultConfig).toHaveProperty('extends', ['@commitlint/config-conventional']);
expect(currentConfig).toHaveProperty('rules.subject-case', [2, 'always', 'sentence-case']);
});

it('configuration returns the right qualified lint options', () => {
expect(getLintOptions(emptyConfig)).toMatchObject(emptyConfigOption);
expect(getLintOptions(defaultConfig)).toMatchObject(emptyConfigOption);
expect(getLintOptions(currentConfig)).toMatchObject(emptyConfigOption);
delete currentConfig.parserPreset?.parserOpts;
expect(getLintOptions(currentConfig)).toMatchObject(emptyConfigOptionNoParserOpts);
});

it('throw error on incorrect title', async () => {
await expect(verifyTitle('foo: bar')).rejects.toThrowError(/check failed/);
await expect(verifyTitle('foo: bar', 'something.config.js')).rejects.toThrowError(/subject-case/);
await expect(verifyTitle('test: add tests', 'commitlint.config.js')).rejects.toThrowError(/sentence-case/);
});

it('return true if title is valid', async () => {
await expect(verifyTitle('fix: Add new commets')).resolves.toEqual(true);
await expect(verifyTitle('feat: Title is short and nice!', 'something.config.js')).resolves.toEqual(true);
await expect(verifyTitle('test: Add test suites', 'commitlint.config.js')).resolves.toEqual(true);
});
});
8 changes: 5 additions & 3 deletions __tests__/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect, it } from 'vitest';
import { describe, expect, it } from 'vitest';
import logWithTile from '../src/log';

it('log with title and error to return the correct string', () => {
expect(logWithTile('title', 'error')).toBe('title\n=====================\nerror');
describe('logger', () => {
it('log with title and error to return the correct string', () => {
expect(logWithTile('title', 'error')).toBe('title\n=====================\nerror');
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@commitlint/cli": "^18.2.0",
"@commitlint/types": "^18.1.0",
"@types/node": "^20.9.0",
"@vitest/coverage-v8": "^0.34.6",
"eslint": "^8.53.0",
"husky": "^8.0.3",
"lint-staged": "^15.0.2",
Expand Down
116 changes: 116 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions src/errHandle.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as core from '@actions/core';
import { error, setFailed } from '@actions/core';

function handleError(err: Error | unknown) {
/**
* Handle standard error handler for github fail status
* @param {Error | unknown} err - error thrown
* @param {boolean} [fail] - enable to throw github failure status
*/
function handleError(err: Error | string | unknown, fail: boolean = true) {
if (err instanceof Error) {
core.error(err);
core.setFailed(err.message);
error(err);
fail && setFailed(err.message);
}
else {
core.error('Unknown error has occurred!');
const message: string = typeof err == 'string' ? err : 'Unknown error has occurred!';
error(message);
fail && setFailed(message);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function run(): Promise<void> {
const pullRequestObject: pullRequest = {
title: pullRequestPayload.title as string,
};

await verifyTitle(pullRequestObject.title, 'commitlint.config.js');
}

Expand Down
Loading

0 comments on commit f03b11d

Please sign in to comment.