|
1 |
| -import test from 'ava'; |
2 |
| -const proxyquire = require('proxyquire') |
3 |
| - .noCallThru() |
4 |
| - .noPreserveCache(); |
| 1 | +const commitlintPluginExample = jest.fn(); |
| 2 | +const scopedCommitlintPluginExample = jest.fn(); |
5 | 3 |
|
6 |
| -test.beforeEach(t => { |
7 |
| - const plugins = {}; |
8 |
| - const plugin = {}; |
9 |
| - const scopedPlugin = {}; |
10 |
| - const stubbedLoadPlugin = proxyquire('./loadPlugin', { |
11 |
| - 'commitlint-plugin-example': plugin, |
12 |
| - '@scope/commitlint-plugin-example': scopedPlugin |
13 |
| - }); |
14 |
| - t.context.data = { |
15 |
| - plugins, |
16 |
| - plugin, |
17 |
| - scopedPlugin, |
18 |
| - stubbedLoadPlugin |
19 |
| - }; |
| 4 | +jest.mock('commitlint-plugin-example', () => commitlintPluginExample, { |
| 5 | + virtual: true |
20 | 6 | });
|
| 7 | +jest.mock( |
| 8 | + '@scope/commitlint-plugin-example', |
| 9 | + () => scopedCommitlintPluginExample, |
| 10 | + {virtual: true} |
| 11 | +); |
21 | 12 |
|
22 |
| -test('should load a plugin when referenced by short name', t => { |
23 |
| - const {stubbedLoadPlugin, plugins, plugin} = t.context.data; |
24 |
| - stubbedLoadPlugin(plugins, 'example'); |
25 |
| - t.is(plugins['example'], plugin); |
| 13 | +import loadPlugin from './loadPlugin'; |
| 14 | + |
| 15 | +test('should load a plugin when referenced by short name', () => { |
| 16 | + const plugins: any = {}; |
| 17 | + loadPlugin(plugins, 'example'); |
| 18 | + expect(plugins['example']).toBe(commitlintPluginExample); |
26 | 19 | });
|
27 | 20 |
|
28 |
| -test('should load a plugin when referenced by long name', t => { |
29 |
| - const {stubbedLoadPlugin, plugins, plugin} = t.context.data; |
30 |
| - stubbedLoadPlugin(plugins, 'commitlint-plugin-example'); |
31 |
| - t.is(plugins['example'], plugin); |
| 21 | +test('should load a plugin when referenced by long name', () => { |
| 22 | + const plugins: any = {}; |
| 23 | + loadPlugin(plugins, 'commitlint-plugin-example'); |
| 24 | + expect(plugins['example']).toBe(commitlintPluginExample); |
32 | 25 | });
|
33 | 26 |
|
34 |
| -test('should throw an error when a plugin has whitespace', t => { |
35 |
| - const {stubbedLoadPlugin, plugins} = t.context.data; |
36 |
| - t.throws(() => { |
37 |
| - stubbedLoadPlugin(plugins, 'whitespace '); |
38 |
| - }, /Whitespace found in plugin name 'whitespace '/u); |
39 |
| - t.throws(() => { |
40 |
| - stubbedLoadPlugin(plugins, 'whitespace\t'); |
41 |
| - }, /Whitespace found in plugin name/u); |
42 |
| - t.throws(() => { |
43 |
| - stubbedLoadPlugin(plugins, 'whitespace\n'); |
44 |
| - }, /Whitespace found in plugin name/u); |
45 |
| - t.throws(() => { |
46 |
| - stubbedLoadPlugin(plugins, 'whitespace\r'); |
47 |
| - }, /Whitespace found in plugin name/u); |
| 27 | +test('should throw an error when a plugin has whitespace', () => { |
| 28 | + const plugins: any = {}; |
| 29 | + expect(() => loadPlugin(plugins, 'whitespace ')).toThrow( |
| 30 | + /Whitespace found in plugin name 'whitespace '/u |
| 31 | + ); |
| 32 | + expect(() => loadPlugin(plugins, 'whitespace\t')).toThrow( |
| 33 | + /Whitespace found in plugin name/u |
| 34 | + ); |
| 35 | + expect(() => loadPlugin(plugins, 'whitespace\n')).toThrow( |
| 36 | + /Whitespace found in plugin name/u |
| 37 | + ); |
| 38 | + expect(() => loadPlugin(plugins, 'whitespace\r')).toThrow( |
| 39 | + /Whitespace found in plugin name/u |
| 40 | + ); |
48 | 41 | });
|
49 | 42 |
|
50 |
| -test("should throw an error when a plugin doesn't exist", t => { |
51 |
| - const {stubbedLoadPlugin, plugins} = t.context.data; |
52 |
| - t.throws(() => { |
53 |
| - stubbedLoadPlugin(plugins, 'nonexistentplugin'); |
54 |
| - }, /Failed to load plugin/u); |
| 43 | +test("should throw an error when a plugin doesn't exist", () => { |
| 44 | + const plugins: any = {}; |
| 45 | + expect(() => loadPlugin(plugins, 'nonexistentplugin')).toThrow( |
| 46 | + /Failed to load plugin/u |
| 47 | + ); |
55 | 48 | });
|
56 | 49 |
|
57 |
| -test('should load a scoped plugin when referenced by short name', t => { |
58 |
| - const {stubbedLoadPlugin, plugins, scopedPlugin} = t.context.data; |
59 |
| - stubbedLoadPlugin(plugins, '@scope/example'); |
60 |
| - t.is(plugins['@scope/example'], scopedPlugin); |
| 50 | +test('should load a scoped plugin when referenced by short name', () => { |
| 51 | + const plugins: any = {}; |
| 52 | + loadPlugin(plugins, '@scope/example'); |
| 53 | + expect(plugins['@scope/example']).toBe(scopedCommitlintPluginExample); |
61 | 54 | });
|
62 | 55 |
|
63 |
| -test('should load a scoped plugin when referenced by long name', t => { |
64 |
| - const {stubbedLoadPlugin, plugins, scopedPlugin} = t.context.data; |
65 |
| - stubbedLoadPlugin(plugins, '@scope/commitlint-plugin-example'); |
66 |
| - t.is(plugins['@scope/example'], scopedPlugin); |
| 56 | +test('should load a scoped plugin when referenced by long name', () => { |
| 57 | + const plugins: any = {}; |
| 58 | + loadPlugin(plugins, '@scope/commitlint-plugin-example'); |
| 59 | + expect(plugins['@scope/example']).toBe(scopedCommitlintPluginExample); |
67 | 60 | });
|
68 | 61 |
|
69 | 62 | /* when referencing a scope plugin and omitting @scope/ */
|
70 |
| -test("should load a scoped plugin when referenced by short name, but should not get the plugin if '@scope/' is omitted", t => { |
71 |
| - const {stubbedLoadPlugin, plugins} = t.context.data; |
72 |
| - stubbedLoadPlugin(plugins, '@scope/example'); |
73 |
| - t.is(plugins['example'], undefined); |
| 63 | +test("should load a scoped plugin when referenced by short name, but should not get the plugin if '@scope/' is omitted", () => { |
| 64 | + const plugins: any = {}; |
| 65 | + loadPlugin(plugins, '@scope/example'); |
| 66 | + expect(plugins['example']).toBeUndefined(); |
74 | 67 | });
|
75 | 68 |
|
76 |
| -test("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", t => { |
77 |
| - const {stubbedLoadPlugin, plugins} = t.context.data; |
78 |
| - stubbedLoadPlugin(plugins, '@scope/commitlint-plugin-example'); |
79 |
| - t.is(plugins['example'], undefined); |
| 69 | +test("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", () => { |
| 70 | + const plugins: any = {}; |
| 71 | + loadPlugin(plugins, '@scope/commitlint-plugin-example'); |
| 72 | + expect(plugins['example']).toBeUndefined(); |
80 | 73 | });
|
0 commit comments