-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add test for commitlint functions
- Loading branch information
Showing
9 changed files
with
262 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.