Skip to content

Commit

Permalink
refactor(load): finish port to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Sep 12, 2019
1 parent a8ab889 commit ec03637
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 131 deletions.
4 changes: 1 addition & 3 deletions @commitlint/load/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
"@commitlint/utils": "^8.1.0",
"@types/cosmiconfig": "5.0.3",
"@types/lodash": "4.14.136",
"execa": "0.11.0",
"globby": "10.0.1",
"proxyquire": "2.1.1",
"@types/resolve-from": "^5.0.1",
"typescript": "3.5.3"
},
"dependencies": {
Expand Down
20 changes: 0 additions & 20 deletions @commitlint/load/src/index.serial-test.ts

This file was deleted.

21 changes: 21 additions & 0 deletions @commitlint/load/src/index.serial.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as path from 'path';
import load from '.';

const {fix} = require('@commitlint/test');

const fixture = (name: string) => path.resolve(__dirname, '../fixtures', name);

test('default cwd option to process.cwd()', async () => {
const cwd = await fix.bootstrap(fixture('basic'));
const before = process.cwd();
process.chdir(cwd);

try {
const actual = await load();
expect(actual.rules.basic).toBeTruthy();
} catch (err) {
throw err;
} finally {
process.chdir(before);
}
});
79 changes: 33 additions & 46 deletions @commitlint/load/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const plugin = jest.fn();
const scopedPlugin = jest.fn();

jest.mock('commitlint-plugin-example', () => plugin, {virtual: true});
jest.mock('@scope/commitlint-plugin-example', () => scopedPlugin, {
virtual: true
});

import path from 'path';
import resolveFrom from 'resolve-from';

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

import load from '.';

const proxyquire = require('proxyquire')
.noCallThru()
.noPreserveCache();

const fixture = (name: string) => path.resolve(__dirname, '../fixtures', name);

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

// test('plugins should be loaded from seed', async () => {
// const plugin = {'@global': true};
// const scopedPlugin = {'@global': true};
// const {default: stubbedLoad} = proxyquire('../.', {
// 'commitlint-plugin-example': plugin,
// '@scope/commitlint-plugin-example': scopedPlugin
// });

// const cwd = await git.bootstrap(fixture('extends-empty'));
// const actual = await stubbedLoad(
// {plugins: ['example', '@scope/example']},
// {cwd}
// );

// expect(actual.plugins).toBe({
// example: plugin,
// '@scope/example': scopedPlugin
// });
// });

// test('plugins should be loaded from config', async () => {
// const plugin = {'@global': true};
// const scopedPlugin = {'@global': true};
// const stubbedLoad = proxyquire('.', {
// 'commitlint-plugin-example': plugin,
// '@scope/commitlint-plugin-example': scopedPlugin
// });

// const cwd = await git.bootstrap(fixture('extends-plugins'));
// const actual = await stubbedLoad({}, {cwd});
// t.deepEqual(actual.plugins, {
// example: plugin,
// '@scope/example': scopedPlugin
// });
// });
test('plugins should be loaded from seed', async () => {
const cwd = await git.bootstrap(fixture('extends-empty'));
const actual = await load({plugins: ['example', '@scope/example']}, {cwd});

expect(actual.plugins).toMatchObject({
example: plugin,
'@scope/example': scopedPlugin
});
});

test('plugins should be loaded from config', async () => {
const cwd = await git.bootstrap(fixture('extends-plugins'));
const actual = await load({}, {cwd});

expect(actual.plugins).toMatchObject({
example: plugin,
'@scope/example': scopedPlugin
});
});

test('uses seed with parserPreset', async () => {
const cwd = await git.bootstrap(fixture('parser-preset'));
const {parserPreset: actual} = await load(
{
parserPreset: './conventional-changelog-custom'
},
{parserPreset: './conventional-changelog-custom'},
{cwd}
);

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

// test('invalid extend should throw', async () => {
// const cwd = await git.bootstrap(fixture('extends-invalid'));
// await t.throws(load({}, {cwd}));
// });
test('invalid extend should throw', async () => {
const cwd = await git.bootstrap(fixture('extends-invalid'));

await expect(load({}, {cwd})).rejects.toThrow();
});

test('empty file should have no rules', async () => {
const cwd = await git.bootstrap(fixture('empty-object-file'));
Expand Down
115 changes: 54 additions & 61 deletions @commitlint/load/src/utils/loadPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,73 @@
import test from 'ava';
const proxyquire = require('proxyquire')
.noCallThru()
.noPreserveCache();
const commitlintPluginExample = jest.fn();
const scopedCommitlintPluginExample = jest.fn();

test.beforeEach(t => {
const plugins = {};
const plugin = {};
const scopedPlugin = {};
const stubbedLoadPlugin = proxyquire('./loadPlugin', {
'commitlint-plugin-example': plugin,
'@scope/commitlint-plugin-example': scopedPlugin
});
t.context.data = {
plugins,
plugin,
scopedPlugin,
stubbedLoadPlugin
};
jest.mock('commitlint-plugin-example', () => commitlintPluginExample, {
virtual: true
});
jest.mock(
'@scope/commitlint-plugin-example',
() => scopedCommitlintPluginExample,
{virtual: true}
);

test('should load a plugin when referenced by short name', t => {
const {stubbedLoadPlugin, plugins, plugin} = t.context.data;
stubbedLoadPlugin(plugins, 'example');
t.is(plugins['example'], plugin);
import loadPlugin from './loadPlugin';

test('should load a plugin when referenced by short name', () => {
const plugins: any = {};
loadPlugin(plugins, 'example');
expect(plugins['example']).toBe(commitlintPluginExample);
});

test('should load a plugin when referenced by long name', t => {
const {stubbedLoadPlugin, plugins, plugin} = t.context.data;
stubbedLoadPlugin(plugins, 'commitlint-plugin-example');
t.is(plugins['example'], plugin);
test('should load a plugin when referenced by long name', () => {
const plugins: any = {};
loadPlugin(plugins, 'commitlint-plugin-example');
expect(plugins['example']).toBe(commitlintPluginExample);
});

test('should throw an error when a plugin has whitespace', t => {
const {stubbedLoadPlugin, plugins} = t.context.data;
t.throws(() => {
stubbedLoadPlugin(plugins, 'whitespace ');
}, /Whitespace found in plugin name 'whitespace '/u);
t.throws(() => {
stubbedLoadPlugin(plugins, 'whitespace\t');
}, /Whitespace found in plugin name/u);
t.throws(() => {
stubbedLoadPlugin(plugins, 'whitespace\n');
}, /Whitespace found in plugin name/u);
t.throws(() => {
stubbedLoadPlugin(plugins, 'whitespace\r');
}, /Whitespace found in plugin name/u);
test('should throw an error when a plugin has whitespace', () => {
const plugins: any = {};
expect(() => loadPlugin(plugins, 'whitespace ')).toThrow(
/Whitespace found in plugin name 'whitespace '/u
);
expect(() => loadPlugin(plugins, 'whitespace\t')).toThrow(
/Whitespace found in plugin name/u
);
expect(() => loadPlugin(plugins, 'whitespace\n')).toThrow(
/Whitespace found in plugin name/u
);
expect(() => loadPlugin(plugins, 'whitespace\r')).toThrow(
/Whitespace found in plugin name/u
);
});

test("should throw an error when a plugin doesn't exist", t => {
const {stubbedLoadPlugin, plugins} = t.context.data;
t.throws(() => {
stubbedLoadPlugin(plugins, 'nonexistentplugin');
}, /Failed to load plugin/u);
test("should throw an error when a plugin doesn't exist", () => {
const plugins: any = {};
expect(() => loadPlugin(plugins, 'nonexistentplugin')).toThrow(
/Failed to load plugin/u
);
});

test('should load a scoped plugin when referenced by short name', t => {
const {stubbedLoadPlugin, plugins, scopedPlugin} = t.context.data;
stubbedLoadPlugin(plugins, '@scope/example');
t.is(plugins['@scope/example'], scopedPlugin);
test('should load a scoped plugin when referenced by short name', () => {
const plugins: any = {};
loadPlugin(plugins, '@scope/example');
expect(plugins['@scope/example']).toBe(scopedCommitlintPluginExample);
});

test('should load a scoped plugin when referenced by long name', t => {
const {stubbedLoadPlugin, plugins, scopedPlugin} = t.context.data;
stubbedLoadPlugin(plugins, '@scope/commitlint-plugin-example');
t.is(plugins['@scope/example'], scopedPlugin);
test('should load a scoped plugin when referenced by long name', () => {
const plugins: any = {};
loadPlugin(plugins, '@scope/commitlint-plugin-example');
expect(plugins['@scope/example']).toBe(scopedCommitlintPluginExample);
});

/* 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", t => {
const {stubbedLoadPlugin, plugins} = t.context.data;
stubbedLoadPlugin(plugins, '@scope/example');
t.is(plugins['example'], undefined);
test("should load a scoped plugin when referenced by short name, but should not get the plugin if '@scope/' is omitted", () => {
const plugins: any = {};
loadPlugin(plugins, '@scope/example');
expect(plugins['example']).toBeUndefined();
});

test("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", t => {
const {stubbedLoadPlugin, plugins} = t.context.data;
stubbedLoadPlugin(plugins, '@scope/commitlint-plugin-example');
t.is(plugins['example'], undefined);
test("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", () => {
const plugins: any = {};
loadPlugin(plugins, '@scope/commitlint-plugin-example');
expect(plugins['example']).toBeUndefined();
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@
resolved "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/resolve-from@5.0.1":
"@types/resolve-from@5.0.1", "@types/resolve-from@^5.0.1":
version "5.0.1"
resolved "https://registry.npmjs.org/@types/resolve-from/-/resolve-from-5.0.1.tgz#2714eaa840c0472dcfa96ec3fb9d170dbf0b677d"
integrity sha512-1G7n5Jtr5inoS1Ez2Y9Efedk9/wH6uGQslbfhGTOw9J42PCAwuyaDgQHW7fIq02+shwB02kM/w31W8gMxI8ORg==
Expand Down

0 comments on commit ec03637

Please sign in to comment.