From fdbc006dfff7ec13c498f24d3ec9e2d8eb91cdea Mon Sep 17 00:00:00 2001 From: George Cadwallader Date: Tue, 1 Oct 2024 18:48:51 +0100 Subject: [PATCH] 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 --- tests/commands/commitlint.spec.ts | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/commands/commitlint.spec.ts diff --git a/tests/commands/commitlint.spec.ts b/tests/commands/commitlint.spec.ts new file mode 100644 index 0000000..62c4c78 --- /dev/null +++ b/tests/commands/commitlint.spec.ts @@ -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 + >); + }); + + 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 + >); + }); + + 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 + >); + }); + + 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'), + ); + }); + }); +});