-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathload-plugin.test.ts
114 lines (98 loc) · 3.46 KB
/
load-plugin.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import loadPlugin from './load-plugin';
import {AsyncRule, Plugin, Rule, SyncRule} from '@commitlint/types';
jest.mock('commitlint-plugin-example', () => ({example: true}), {
virtual: true,
});
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'));
});
test('should load a plugin when referenced by long name', () => {
const plugins = loadPlugin({}, 'commitlint-plugin-example');
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 '"
);
expect(() => loadPlugin({}, 'whitespace\t')).toThrow(
'Whitespace found in plugin name'
);
expect(() => loadPlugin({}, 'whitespace\n')).toThrow(
'Whitespace found in plugin name'
);
expect(() => loadPlugin({}, 'whitespace\r')).toThrow(
'Whitespace found in plugin name'
);
});
test("should throw an error when a plugin doesn't exist", () => {
expect(() => loadPlugin({}, 'nonexistentplugin')).toThrow(
'Failed to load plugin'
);
});
test('should load a scoped plugin when referenced by short name', () => {
const plugins = loadPlugin({}, '@scope/example');
expect(plugins['@scope/example']).toBe(
require('@scope/commitlint-plugin-example')
);
});
test('should load a scoped plugin when referenced by long name', () => {
const plugins = loadPlugin({}, '@scope/commitlint-plugin-example');
expect(plugins['@scope/example']).toBe(
require('@scope/commitlint-plugin-example')
);
});
/* when referencing a scope plugin and omitting @scope/ */
test("should load a scoped plugin when referenced by short name, but should not get the plugin if '@scope/' is omitted", () => {
const plugins = loadPlugin({}, '@scope/example');
expect(plugins['example']).toBe(undefined);
});
test("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", () => {
const plugins = loadPlugin({}, '@scope/commitlint-plugin-example');
expect(plugins['example']).toBe(undefined);
});