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

refactor: port load to typescript #786

Merged
merged 19 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: port load tests from ava to jest
Co-authored-by: Armano <armano2@users.noreply.github.com>
  • Loading branch information
marionebl and armano2 committed Jan 27, 2020
commit 54e16bc0de0eeec48a72690772f2f8ba0f1e846f
107 changes: 46 additions & 61 deletions @commitlint/load/src/utils/loadPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,65 @@
import test from 'ava';
const proxyquire = require('proxyquire')
.noCallThru()
.noPreserveCache();
import loadPlugin from './loadPlugin';

test.beforeEach(t => {
const plugins = {};
const plugin = {};
const scopedPlugin = {};
const stubbedLoadPlugin = proxyquire('./loadPlugin', {
'commitlint-plugin-example': plugin,
'@scope/commitlint-plugin-example': scopedPlugin
}).default;
t.context.data = {
plugins,
plugin,
scopedPlugin,
stubbedLoadPlugin
};
jest.mock('commitlint-plugin-example', () => ({example: true}), {
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);
jest.mock('@scope/commitlint-plugin-example', () => ({scope: true}), {
virtual: true
});

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 short name', () => {
const plugins = loadPlugin({}, 'example');
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
});

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 load a plugin when referenced by long name', () => {
const plugins = loadPlugin({}, 'commitlint-plugin-example');
expect(plugins['example']).toBe(require('commitlint-plugin-example'));
});

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 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 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 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 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 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", 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 = 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", 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 = loadPlugin({}, '@scope/commitlint-plugin-example');
expect(plugins['example']).toBe(undefined);
});
8 changes: 6 additions & 2 deletions @commitlint/load/src/utils/loadPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import chalk from 'chalk';
import {normalizePackageName, getShorthandName} from './pluginNaming';
import {WhitespacePluginError, MissingPluginError} from './pluginErrors';

export type PluginRecords = Record<string, unknown>;

export default function loadPlugin(
plugins: any,
plugins: PluginRecords,
pluginName: string,
debug: boolean = false
) {
): PluginRecords {
const longName = normalizePackageName(pluginName);
const shortName = getShorthandName(longName);
let plugin = null;
Expand Down Expand Up @@ -66,4 +68,6 @@ export default function loadPlugin(

plugins[pluginKey] = plugin;
}

return plugins;
}
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module.exports = {
testRegex: undefined,
testMatch: [
'**/*.test.ts?(x)',
'**/@commitlint/read/src/*.test.js?(x)',
'**/@commitlint/cli/src/*.test.js?(x)'
'**/@commitlint/{read,cli,load}/src/*.test.js?(x)'
]
};