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

test(lint): migrate ava tests to jest #912

Merged
merged 1 commit into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 1 addition & 24 deletions @commitlint/lint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,9 @@
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"deps": "dep-check",
"pkg": "pkg-check --skip-import",
"start": "concurrently \"ava -c 4 --verbose --watch\" \"yarn run watch\"",
"test": "ava -c 4 --verbose",
"start": "yarn run watch",
"watch": "babel src --out-dir lib --watch --source-maps"
},
"ava": {
"files": [
"src/**/*.test.js",
"!lib/**/*"
],
"source": [
"src/**/*.js",
"!lib/**/*"
],
"babel": {
"testOptions": {
"presets": [
"babel-preset-commitlint"
]
}
},
"require": [
"@babel/register"
]
},
"babel": {
"presets": [
"babel-preset-commitlint"
Expand Down Expand Up @@ -67,9 +46,7 @@
"@babel/register": "^7.7.7",
"@commitlint/test": "8.2.0",
"@commitlint/utils": "^8.3.4",
"ava": "2.4.0",
"babel-preset-commitlint": "^8.2.0",
"concurrently": "3.6.1",
"cross-env": "6.0.3",
"execa": "0.11.0",
"globby": "10.0.1",
Expand Down
197 changes: 95 additions & 102 deletions @commitlint/lint/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
import test from 'ava';
import lint from '.';

test('throws without params', async t => {
const error = await t.throwsAsync(lint());
t.is(error.message, 'Expected a raw commit');
test('throws without params', async () => {
const error = lint();
await expect(error).rejects.toThrow('Expected a raw commit');
});

test('throws with empty message', async t => {
const error = await t.throwsAsync(lint(''));
t.is(error.message, 'Expected a raw commit');
test('throws with empty message', async () => {
const error = lint('');
await expect(error).rejects.toThrow('Expected a raw commit');
});

test('positive on stub message and no rule', async t => {
test('positive on stub message and no rule', async () => {
const actual = await lint('foo: bar');
t.true(actual.valid);
expect(actual.valid).toBe(true);
});

test('positive on stub message and adhered rule', async t => {
test('positive on stub message and adhered rule', async () => {
const actual = await lint('foo: bar', {
'type-enum': [2, 'always', ['foo']]
});
t.true(actual.valid);
expect(actual.valid).toBe(true);
});

test('negative on stub message and broken rule', async t => {
test('negative on stub message and broken rule', async () => {
const actual = await lint('foo: bar', {
'type-enum': [2, 'never', ['foo']]
});
t.false(actual.valid);
expect(actual.valid).toBe(false);
});

test('positive on ignored message and broken rule', async t => {
test('positive on ignored message and broken rule', async () => {
const actual = await lint('Revert "some bogus commit"', {
'type-empty': [2, 'never']
});
t.true(actual.valid);
t.is(actual.input, 'Revert "some bogus commit"');
expect(actual.valid).toBe(true);
expect(actual.input).toBe('Revert "some bogus commit"');
});

test('negative on ignored message, disabled ignored messages and broken rule', async t => {
test('negative on ignored message, disabled ignored messages and broken rule', async () => {
const actual = await lint(
'Revert "some bogus commit"',
{
Expand All @@ -48,10 +47,10 @@ test('negative on ignored message, disabled ignored messages and broken rule', a
defaultIgnores: false
}
);
t.false(actual.valid);
expect(actual.valid).toBe(false);
});

test('positive on custom ignored message and broken rule', async t => {
test('positive on custom ignored message and broken rule', async () => {
const ignoredMessage = 'some ignored custom message';
const actual = await lint(
ignoredMessage,
Expand All @@ -62,11 +61,11 @@ test('positive on custom ignored message and broken rule', async t => {
ignores: [c => c === ignoredMessage]
}
);
t.true(actual.valid);
t.is(actual.input, ignoredMessage);
expect(actual.valid).toBe(true);
expect(actual.input).toBe(ignoredMessage);
});

test('positive on stub message and opts', async t => {
test('positive on stub message and opts', async () => {
const actual = await lint(
'foo-bar',
{
Expand All @@ -79,108 +78,102 @@ test('positive on stub message and opts', async t => {
}
}
);
t.true(actual.valid);
expect(actual.valid).toBe(true);
});

test('throws for invalid rule names', async t => {
const error = await t.throwsAsync(
lint('foo', {foo: [2, 'always'], bar: [1, 'never']})
);
test('throws for invalid rule names', async () => {
const error = lint('foo', {foo: [2, 'always'], bar: [1, 'never']});

t.is(error.message.indexOf('Found invalid rule names: foo, bar'), 0);
await expect(error).rejects.toThrow(/^Found invalid rule names: foo, bar/);
});

test('throws for invalid rule config', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {
'type-enum': 1,
'scope-enum': {0: 2, 1: 'never', 2: ['foo'], length: 3}
})
);
test('throws for invalid rule config', async () => {
const error = lint('type(scope): foo', {
'type-enum': 1,
'scope-enum': {0: 2, 1: 'never', 2: ['foo'], length: 3}
});

t.true(error.message.indexOf('type-enum must be array') > -1);
t.true(error.message.indexOf('scope-enum must be array') > -1);
await expect(error).rejects.toThrow('type-enum must be array');
await expect(error).rejects.toThrow('scope-enum must be array');
});

test('allows disable shorthand', async t => {
await t.notThrowsAsync(lint('foo', {'type-enum': [0], 'scope-enum': [0]}));
test('allows disable shorthand', async () => {
const result = lint('foo', {'type-enum': [0], 'scope-enum': [0]});

await expect(result).resolves.toEqual({
errors: [],
input: 'foo',
valid: true,
warnings: []
});
});

test('throws for rule with invalid length', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {'scope-enum': [1, 2, 3, 4]})
);
test('throws for rule with invalid length', async () => {
const error = lint('type(scope): foo', {'scope-enum': [1, 2, 3, 4]});

t.true(error.message.indexOf('scope-enum must be 2 or 3 items long') > -1);
await expect(error).rejects.toThrow('scope-enum must be 2 or 3 items long');
});

test('throws for rule with invalid level', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {
'type-enum': ['2', 'always'],
'header-max-length': [{}, 'always']
})
);

t.true(error.message.indexOf('rule type-enum must be number') > -1);
t.true(error.message.indexOf('rule type-enum must be number') > -1);
Copy link
Contributor Author

@armano2 armano2 Jan 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of checking this 2 times we should check

'rule type-enum must be number'
'rule header-max-length must be number'

test('throws for rule with invalid level', async () => {
const error = lint('type(scope): foo', {
'type-enum': ['2', 'always'],
'header-max-length': [{}, 'always']
});
await expect(error).rejects.toThrow('rule type-enum must be number');
await expect(error).rejects.toThrow('rule header-max-length must be number');
});

test('throws for rule with out of range level', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {
'type-enum': [-1, 'always'],
'header-max-length': [3, 'always']
})
);
test('throws for rule with out of range level', async () => {
const error = lint('type(scope): foo', {
'type-enum': [-1, 'always'],
'header-max-length': [3, 'always']
});

t.true(error.message.indexOf('rule type-enum must be between 0 and 2') > -1);
t.true(error.message.indexOf('rule type-enum must be between 0 and 2') > -1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of checking this 2 times we should check

'rule type-enum must be between 0 and 2'
'rule header-max-length must be between 0 and 2'

await expect(error).rejects.toThrow('rule type-enum must be between 0 and 2');
await expect(error).rejects.toThrow(
'rule header-max-length must be between 0 and 2'
);
});

test('throws for rule with invalid condition', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {
'type-enum': [1, 2],
'header-max-length': [1, {}]
})
);
test('throws for rule with invalid condition', async () => {
const error = lint('type(scope): foo', {
'type-enum': [1, 2],
'header-max-length': [1, {}]
});

t.true(error.message.indexOf('type-enum must be string') > -1);
t.true(error.message.indexOf('header-max-length must be string') > -1);
await expect(error).rejects.toThrow('type-enum must be string');
await expect(error).rejects.toThrow('header-max-length must be string');
});

test('throws for rule with out of range condition', async t => {
const error = await t.throwsAsync(
lint('type(scope): foo', {
'type-enum': [1, 'foo'],
'header-max-length': [1, 'bar']
})
);
test('throws for rule with out of range condition', async () => {
const error = lint('type(scope): foo', {
'type-enum': [1, 'foo'],
'header-max-length': [1, 'bar']
});

t.true(error.message.indexOf('type-enum must be "always" or "never"') > -1);
t.true(
error.message.indexOf('header-max-length must be "always" or "never"') > -1
await expect(error).rejects.toThrow('type-enum must be "always" or "never"');
await expect(error).rejects.toThrow(
'header-max-length must be "always" or "never"'
);
});

test('succeds for issue', async t => {
test('succeds for issue', async () => {
const report = await lint('somehting #1', {
'references-empty': [2, 'never']
});

t.true(report.valid);
expect(report.valid).toBe(true);
});

test('fails for issue', async t => {
test('fails for issue', async () => {
const report = await lint('somehting #1', {
'references-empty': [2, 'always']
});

t.false(report.valid);
expect(report.valid).toBe(false);
});

test('succeds for custom issue prefix', async t => {
test('succeds for custom issue prefix', async () => {
const report = await lint(
'somehting REF-1',
{
Expand All @@ -193,10 +186,10 @@ test('succeds for custom issue prefix', async t => {
}
);

t.true(report.valid);
expect(report.valid).toBe(true);
});

test('fails for custom issue prefix', async t => {
test('fails for custom issue prefix', async () => {
const report = await lint(
'somehting #1',
{
Expand All @@ -209,10 +202,10 @@ test('fails for custom issue prefix', async t => {
}
);

t.false(report.valid);
expect(report.valid).toBe(false);
});

test('fails for custom plugin rule', async t => {
test('fails for custom plugin rule', async () => {
const report = await lint(
'somehting #1',
{
Expand All @@ -229,10 +222,10 @@ test('fails for custom plugin rule', async t => {
}
);

t.false(report.valid);
expect(report.valid).toBe(false);
});

test('passes for custom plugin rule', async t => {
test('passes for custom plugin rule', async () => {
const report = await lint(
'somehting #1',
{
Expand All @@ -249,31 +242,31 @@ test('passes for custom plugin rule', async t => {
}
);

t.true(report.valid);
expect(report.valid).toBe(true);
});

test('returns original message only with commit header', async t => {
test('returns original message only with commit header', async () => {
const message = 'foo: bar';
const report = await lint(message);

t.is(report.input, message);
expect(report.input).toBe(message);
});

test('returns original message with commit header and body', async t => {
test('returns original message with commit header and body', async () => {
const message = 'foo: bar/n/nFoo bar bizz buzz.';
const report = await lint(message);

t.is(report.input, message);
expect(report.input).toBe(message);
});

test('returns original message with commit header, body and footer', async t => {
test('returns original message with commit header, body and footer', async () => {
const message = 'foo: bar/n/nFoo bar bizz buzz./n/nCloses #1';
const report = await lint(message);

t.is(report.input, message);
expect(report.input).toBe(message);
});

test('returns original message with commit header, body and footer, parsing comments', async t => {
test('returns original message with commit header, body and footer, parsing comments', async () => {
const expected = 'foo: bar/n/nFoo bar bizz buzz./n/nCloses #1';
const message = `${expected}\n\n# Some comment to ignore`;
const report = await lint(
Expand All @@ -288,5 +281,5 @@ test('returns original message with commit header, body and footer, parsing comm
}
);

t.is(report.input, expected);
expect(report.input).toBe(expected);
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
testRegex: undefined,
testMatch: [
'**/*.test.ts?(x)',
'**/@commitlint/lint/src/*.test.js?(x)',
'**/@commitlint/read/src/*.test.js?(x)',
'**/@commitlint/cli/src/*.test.js?(x)'
]
Expand Down