From 575bcaedd933cc15a67997578f7f109a3a1d4537 Mon Sep 17 00:00:00 2001 From: Armano Date: Mon, 27 Jan 2020 21:32:40 +0100 Subject: [PATCH] test(lint): migrate ava tests to jest --- @commitlint/lint/package.json | 25 +--- @commitlint/lint/src/index.test.js | 197 ++++++++++++++--------------- jest.config.js | 1 + 3 files changed, 97 insertions(+), 126 deletions(-) diff --git a/@commitlint/lint/package.json b/@commitlint/lint/package.json index 6868e42474..074716f4dd 100644 --- a/@commitlint/lint/package.json +++ b/@commitlint/lint/package.json @@ -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" @@ -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", diff --git a/@commitlint/lint/src/index.test.js b/@commitlint/lint/src/index.test.js index 7c9ca8bed5..60dbc6d37b 100644 --- a/@commitlint/lint/src/index.test.js +++ b/@commitlint/lint/src/index.test.js @@ -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"', { @@ -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, @@ -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', { @@ -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); +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); + 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', { @@ -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', { @@ -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', { @@ -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', { @@ -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( @@ -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); }); diff --git a/jest.config.js b/jest.config.js index 28a58f4c92..7c32aee52f 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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)' ]