Skip to content

Commit ec03637

Browse files
committed
refactor(load): finish port to typescript
1 parent a8ab889 commit ec03637

File tree

6 files changed

+110
-131
lines changed

6 files changed

+110
-131
lines changed

@commitlint/load/package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
"@commitlint/utils": "^8.1.0",
3939
"@types/cosmiconfig": "5.0.3",
4040
"@types/lodash": "4.14.136",
41-
"execa": "0.11.0",
42-
"globby": "10.0.1",
43-
"proxyquire": "2.1.1",
41+
"@types/resolve-from": "^5.0.1",
4442
"typescript": "3.5.3"
4543
},
4644
"dependencies": {

@commitlint/load/src/index.serial-test.ts

-20
This file was deleted.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as path from 'path';
2+
import load from '.';
3+
4+
const {fix} = require('@commitlint/test');
5+
6+
const fixture = (name: string) => path.resolve(__dirname, '../fixtures', name);
7+
8+
test('default cwd option to process.cwd()', async () => {
9+
const cwd = await fix.bootstrap(fixture('basic'));
10+
const before = process.cwd();
11+
process.chdir(cwd);
12+
13+
try {
14+
const actual = await load();
15+
expect(actual.rules.basic).toBeTruthy();
16+
} catch (err) {
17+
throw err;
18+
} finally {
19+
process.chdir(before);
20+
}
21+
});

@commitlint/load/src/index.test.ts

+33-46
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
const plugin = jest.fn();
2+
const scopedPlugin = jest.fn();
3+
4+
jest.mock('commitlint-plugin-example', () => plugin, {virtual: true});
5+
jest.mock('@scope/commitlint-plugin-example', () => scopedPlugin, {
6+
virtual: true
7+
});
8+
19
import path from 'path';
210
import resolveFrom from 'resolve-from';
311

412
const {fix, git} = require('@commitlint/test');
513

614
import load from '.';
715

8-
const proxyquire = require('proxyquire')
9-
.noCallThru()
10-
.noPreserveCache();
11-
1216
const fixture = (name: string) => path.resolve(__dirname, '../fixtures', name);
1317

1418
test('extends-empty should have no rules', async () => {
@@ -41,48 +45,30 @@ test('rules should be loaded from absolute config file', async () => {
4145
expect(actual.rules.foo).toBe('bar');
4246
});
4347

44-
// test('plugins should be loaded from seed', async () => {
45-
// const plugin = {'@global': true};
46-
// const scopedPlugin = {'@global': true};
47-
// const {default: stubbedLoad} = proxyquire('../.', {
48-
// 'commitlint-plugin-example': plugin,
49-
// '@scope/commitlint-plugin-example': scopedPlugin
50-
// });
51-
52-
// const cwd = await git.bootstrap(fixture('extends-empty'));
53-
// const actual = await stubbedLoad(
54-
// {plugins: ['example', '@scope/example']},
55-
// {cwd}
56-
// );
57-
58-
// expect(actual.plugins).toBe({
59-
// example: plugin,
60-
// '@scope/example': scopedPlugin
61-
// });
62-
// });
63-
64-
// test('plugins should be loaded from config', async () => {
65-
// const plugin = {'@global': true};
66-
// const scopedPlugin = {'@global': true};
67-
// const stubbedLoad = proxyquire('.', {
68-
// 'commitlint-plugin-example': plugin,
69-
// '@scope/commitlint-plugin-example': scopedPlugin
70-
// });
71-
72-
// const cwd = await git.bootstrap(fixture('extends-plugins'));
73-
// const actual = await stubbedLoad({}, {cwd});
74-
// t.deepEqual(actual.plugins, {
75-
// example: plugin,
76-
// '@scope/example': scopedPlugin
77-
// });
78-
// });
48+
test('plugins should be loaded from seed', async () => {
49+
const cwd = await git.bootstrap(fixture('extends-empty'));
50+
const actual = await load({plugins: ['example', '@scope/example']}, {cwd});
51+
52+
expect(actual.plugins).toMatchObject({
53+
example: plugin,
54+
'@scope/example': scopedPlugin
55+
});
56+
});
57+
58+
test('plugins should be loaded from config', async () => {
59+
const cwd = await git.bootstrap(fixture('extends-plugins'));
60+
const actual = await load({}, {cwd});
61+
62+
expect(actual.plugins).toMatchObject({
63+
example: plugin,
64+
'@scope/example': scopedPlugin
65+
});
66+
});
7967

8068
test('uses seed with parserPreset', async () => {
8169
const cwd = await git.bootstrap(fixture('parser-preset'));
8270
const {parserPreset: actual} = await load(
83-
{
84-
parserPreset: './conventional-changelog-custom'
85-
},
71+
{parserPreset: './conventional-changelog-custom'},
8672
{cwd}
8773
);
8874

@@ -92,10 +78,11 @@ test('uses seed with parserPreset', async () => {
9278
});
9379
});
9480

95-
// test('invalid extend should throw', async () => {
96-
// const cwd = await git.bootstrap(fixture('extends-invalid'));
97-
// await t.throws(load({}, {cwd}));
98-
// });
81+
test('invalid extend should throw', async () => {
82+
const cwd = await git.bootstrap(fixture('extends-invalid'));
83+
84+
await expect(load({}, {cwd})).rejects.toThrow();
85+
});
9986

10087
test('empty file should have no rules', async () => {
10188
const cwd = await git.bootstrap(fixture('empty-object-file'));
+54-61
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,73 @@
1-
import test from 'ava';
2-
const proxyquire = require('proxyquire')
3-
.noCallThru()
4-
.noPreserveCache();
1+
const commitlintPluginExample = jest.fn();
2+
const scopedCommitlintPluginExample = jest.fn();
53

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
206
});
7+
jest.mock(
8+
'@scope/commitlint-plugin-example',
9+
() => scopedCommitlintPluginExample,
10+
{virtual: true}
11+
);
2112

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);
2619
});
2720

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);
3225
});
3326

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+
);
4841
});
4942

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+
);
5548
});
5649

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);
6154
});
6255

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);
6760
});
6861

6962
/* 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();
7467
});
7568

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();
8073
});

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@
13171317
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
13181318
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==
13191319

1320-
1320+
"@types/[email protected]", "@types/resolve-from@^5.0.1":
13211321
version "5.0.1"
13221322
resolved "https://registry.npmjs.org/@types/resolve-from/-/resolve-from-5.0.1.tgz#2714eaa840c0472dcfa96ec3fb9d170dbf0b677d"
13231323
integrity sha512-1G7n5Jtr5inoS1Ez2Y9Efedk9/wH6uGQslbfhGTOw9J42PCAwuyaDgQHW7fIq02+shwB02kM/w31W8gMxI8ORg==

0 commit comments

Comments
 (0)