-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(commitlint): command assertions
Testing the commitlint command to make sure it's working as expected. Tests include, validating correct and incorrect commit messages and log output assertion. Ref: #75
- Loading branch information
1 parent
6b084b6
commit fdbc006
Showing
1 changed file
with
116 additions
and
0 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,116 @@ | ||
import {beforeEach, describe, expect, it, vi} from 'vitest'; | ||
|
||
import {handler} from '../../src/commands/commitlint'; | ||
import * as logger from '../../src/lib/logger'; | ||
import * as sourceControl from '../../src/lib/source-control'; | ||
import {Arguments} from 'yargs'; | ||
|
||
declare module 'vitest' { | ||
export interface TestContext { | ||
commandResult?: number; | ||
} | ||
} | ||
|
||
describe('command/commitlint', () => { | ||
beforeEach(() => { | ||
vi.spyOn(logger, 'log').mockImplementation(() => {}); | ||
}); | ||
|
||
describe('with all valid commit messages', () => { | ||
beforeEach(async ctx => { | ||
vi.spyOn(sourceControl, 'getSourceControlProvider').mockImplementation( | ||
async () => ({ | ||
isEnabled: async () => true, | ||
getBranchName: async () => 'feat/123', | ||
root: async () => '/path/to/repo', | ||
getCurrentCommitMessage: async () => 'feat: create the thing', | ||
getCommits: async () => ['feat: correct', 'chore: another correct'], | ||
}), | ||
); | ||
|
||
ctx.commandResult = await handler({_: [2, '']} as Arguments< | ||
Record<string, never> | ||
>); | ||
}); | ||
|
||
it('returns correct exit code', ctx => { | ||
expect(ctx.commandResult).toStrictEqual(0); | ||
}); | ||
|
||
it('displays warnings', () => { | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('references may not be empty'), | ||
); | ||
}); | ||
}); | ||
|
||
describe('with level set to 1', () => { | ||
beforeEach(async ctx => { | ||
vi.spyOn(sourceControl, 'getSourceControlProvider').mockImplementation( | ||
async () => ({ | ||
isEnabled: async () => true, | ||
getBranchName: async () => 'feat/123', | ||
root: async () => '/path/to/repo', | ||
getCurrentCommitMessage: async () => 'feat: create the thing', | ||
getCommits: async () => ['feat: correct', 'chore: another correct'], | ||
}), | ||
); | ||
|
||
ctx.commandResult = await handler({_: [1, '']} as Arguments< | ||
Record<string, never> | ||
>); | ||
}); | ||
|
||
it('does not show warnings', ctx => { | ||
expect(logger.log).not.toHaveBeenCalledWith( | ||
expect.stringContaining('references may not be empty'), | ||
); | ||
}); | ||
}); | ||
|
||
describe('with invalid commit messages', () => { | ||
beforeEach(async ctx => { | ||
vi.spyOn(sourceControl, 'getSourceControlProvider').mockImplementation( | ||
async () => ({ | ||
isEnabled: async () => true, | ||
getBranchName: async () => 'feat/123', | ||
root: async () => '/path/to/repo', | ||
getCurrentCommitMessage: async () => 'feat: create the thing', | ||
getCommits: async () => ['my first commit that is incorrect'], | ||
}), | ||
); | ||
|
||
ctx.commandResult = await handler({_: [2, '']} as Arguments< | ||
Record<string, never> | ||
>); | ||
}); | ||
|
||
it('returns correct exit code', ctx => { | ||
expect(ctx.commandResult).toStrictEqual(2); | ||
}); | ||
|
||
it('logs commit message', () => { | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('my first commit that is incorrect'), | ||
); | ||
}); | ||
|
||
it('logs invalid subject error message', () => { | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('subject may not be empty'), | ||
); | ||
}); | ||
|
||
it('logs invalid type error message', () => { | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('type may not be empty'), | ||
); | ||
}); | ||
|
||
it('logs invalid references warning message', () => { | ||
expect(logger.log).toHaveBeenCalledWith( | ||
expect.stringContaining('references may not be empty'), | ||
); | ||
}); | ||
}); | ||
}); |