Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(load): use Rule | AsyncRule | SyncRule as rule value type in Plugin #2146

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions @commitlint/load/src/utils/load-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -8,6 +9,39 @@ jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), {
virtual: true,
});

jest.mock(
'commitlint-plugin-rule',
(): Plugin => {
const rule: Rule<number> = (_parsed, when, _value) => {
return [when === 'never'];
};
return {rules: {rule}};
},
{virtual: true}
);

jest.mock(
'commitlint-plugin-sync-rule',
(): Plugin => {
const syncRule: SyncRule<number> = (_parsed, when, _value) => {
return [when === 'never'];
};
return {rules: {syncRule}};
},
{virtual: true}
);

jest.mock(
'commitlint-plugin-async-rule',
(): Plugin => {
const asyncRule: AsyncRule<number> = (_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'));
Expand All @@ -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 '"
Expand Down
10 changes: 8 additions & 2 deletions @commitlint/types/src/load.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {Rule, RulesConfig, RuleConfigQuality} from './rules';
import {
Rule,
RulesConfig,
RuleConfigQuality,
AsyncRule,
SyncRule,
} from './rules';

export type PluginRecords = Record<string, Plugin>;

export interface Plugin {
rules: {
[ruleName: string]: Rule<unknown>;
[ruleName: string]: Rule | AsyncRule | SyncRule;
};
}

Expand Down