diff --git a/@commitlint/load/src/utils/load-plugin.test.ts b/@commitlint/load/src/utils/load-plugin.test.ts index abe9b74bc6..7c78d87f2f 100644 --- a/@commitlint/load/src/utils/load-plugin.test.ts +++ b/@commitlint/load/src/utils/load-plugin.test.ts @@ -1,4 +1,5 @@ import loadPlugin from './load-plugin'; +import {AsyncRule, Plugin, Rule, SyncRule} from '@commitlint/types'; jest.mock('commitlint-plugin-example', () => ({example: true}), { virtual: true, @@ -8,6 +9,39 @@ jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), { virtual: true, }); +jest.mock( + 'commitlint-plugin-rule', + (): Plugin => { + const rule: Rule = (_parsed, when, _value) => { + return [when === 'never']; + }; + return {rules: {rule}}; + }, + {virtual: true} +); + +jest.mock( + 'commitlint-plugin-sync-rule', + (): Plugin => { + const syncRule: SyncRule = (_parsed, when, _value) => { + return [when === 'never']; + }; + return {rules: {syncRule}}; + }, + {virtual: true} +); + +jest.mock( + 'commitlint-plugin-async-rule', + (): Plugin => { + const asyncRule: AsyncRule = (_parsed, when, _value) => { + return new Promise(() => [when === 'never']); + }; + return {rules: {asyncRule}}; + }, + {virtual: true} +); + test('should load a plugin when referenced by short name', () => { const plugins = loadPlugin({}, 'example'); expect(plugins['example']).toBe(require('commitlint-plugin-example')); @@ -18,6 +52,21 @@ test('should load a plugin when referenced by long name', () => { expect(plugins['example']).toBe(require('commitlint-plugin-example')); }); +test('should load a plugin with a rule', () => { + const plugins = loadPlugin({}, 'commitlint-plugin-rule'); + expect(plugins['rule']).toBe(require('commitlint-plugin-rule')); +}); + +test('should load a plugin with a sync rule', () => { + const plugins = loadPlugin({}, 'commitlint-plugin-sync-rule'); + expect(plugins['sync-rule']).toBe(require('commitlint-plugin-sync-rule')); +}); + +test('should load a plugin with an async rule', () => { + const plugins = loadPlugin({}, 'commitlint-plugin-async-rule'); + expect(plugins['async-rule']).toBe(require('commitlint-plugin-async-rule')); +}); + test('should throw an error when a plugin has whitespace', () => { expect(() => loadPlugin({}, 'whitespace ')).toThrow( "Whitespace found in plugin name 'whitespace '" diff --git a/@commitlint/types/src/load.ts b/@commitlint/types/src/load.ts index cfcd59acc4..ffb947966a 100644 --- a/@commitlint/types/src/load.ts +++ b/@commitlint/types/src/load.ts @@ -1,10 +1,16 @@ -import {Rule, RulesConfig, RuleConfigQuality} from './rules'; +import { + Rule, + RulesConfig, + RuleConfigQuality, + AsyncRule, + SyncRule, +} from './rules'; export type PluginRecords = Record; export interface Plugin { rules: { - [ruleName: string]: Rule; + [ruleName: string]: Rule | AsyncRule | SyncRule; }; }