Skip to content

Commit dc7faab

Browse files
committed
test(travis-cli): migrate tests from ava to jest
1 parent b512ac7 commit dc7faab

File tree

4 files changed

+98
-132
lines changed

4 files changed

+98
-132
lines changed

@commitlint/cli/src/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {merge} from 'lodash';
55
import * as sander from 'sander';
66
import stream from 'string-to-stream';
77

8-
const bin = path.normalize(path.join(__dirname, '../lib/cli.js'));
8+
const bin = require.resolve('../lib/cli.js');
99

1010
const cli = (args, options) => {
1111
return (input = '') => {

@commitlint/travis-cli/package.json

-20
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,8 @@
1313
"deps": "dep-check",
1414
"pkg": "pkg-check --skip-main",
1515
"start": "ava -c 4 --verbose --watch",
16-
"test": "ava -c 4 --verbose",
1716
"watch": "babel src --out-dir lib --watch --source-maps"
1817
},
19-
"ava": {
20-
"files": [
21-
"src/**/*.test.js"
22-
],
23-
"source": [
24-
"lib/**/*.js"
25-
],
26-
"babel": {
27-
"testOptions": {
28-
"presets": [
29-
"babel-preset-commitlint"
30-
]
31-
}
32-
},
33-
"require": [
34-
"@babel/register"
35-
]
36-
},
3718
"babel": {
3819
"presets": [
3920
"babel-preset-commitlint"
@@ -66,7 +47,6 @@
6647
"@babel/register": "7.7.7",
6748
"@commitlint/test": "8.2.0",
6849
"@commitlint/utils": "^8.3.4",
69-
"ava": "2.4.0",
7050
"babel-preset-commitlint": "^8.2.0",
7151
"cross-env": "6.0.3",
7252
"which": "2.0.1"
+96-111
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,72 @@
1-
// Disable ftb
2-
// const os = require('os');
3-
// const {git} = require('@commitlint/test');
4-
import test from 'ava';
51
import execa from 'execa';
62
// Disable ftb
7-
// const which = require('which');
3+
// import {git} from '@commitlint/test';
4+
// import which from 'which';
85

96
// Disable ftb
107
// const NODE_BIN = which.sync('node');
11-
const BIN = require.resolve('../lib/cli.js');
8+
const bin = require.resolve('../lib/cli.js');
129

1310
// Disable ftb
1411
// const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
1512
// const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
1613
// const TRAVIS_BRANCH = 'TRAVIS_BRANCH';
1714
// const TRAVIS_COMMIT = 'TRAVIS_COMMIT';
1815

19-
const bin = async (config = {}) => {
16+
const cli = async (config = {}) => {
2017
try {
21-
return await execa(BIN, Object.assign({extendEnv: false}, config));
18+
return await execa(bin, [], config);
2219
} catch (err) {
23-
throw new Error([err.stdout, err.stderr].join('\n'));
20+
return Promise.reject([err.stdout, err.stderr].join('\n'));
2421
}
2522
};
2623

27-
test('should throw when not on travis ci', async t => {
24+
test('should throw when not on travis ci', async () => {
2825
const env = {
2926
CI: false,
3027
TRAVIS: false
3128
};
3229

33-
await t.throwsAsync(
34-
bin({env}),
35-
/@commitlint\/travis-cli is intended to be used on Travis CI/
30+
await expect(cli({env})).rejects.toContain(
31+
'@commitlint/travis-cli is intended to be used on Travis CI'
3632
);
3733
});
3834

39-
/* Test.failing(
40-
'should throw when on travis ci, but env vars are missing',
41-
async t => {
42-
const env = {
43-
TRAVIS: true,
44-
CI: true
45-
};
35+
test('should throw when on travis ci, but env vars are missing', async () => {
36+
const env = {
37+
TRAVIS: true,
38+
CI: true
39+
};
4640

47-
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT, TRAVIS_BRANCH/);
48-
}
49-
); */
41+
await expect(cli({env})).rejects.toContain(
42+
'TRAVIS_COMMIT, TRAVIS_COMMIT_RANGE, TRAVIS_EVENT_TYPE, TRAVIS_REPO_SLUG, TRAVIS_PULL_REQUEST_SLUG'
43+
);
44+
});
5045

51-
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => {
46+
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async () => {
5247
const env = {
5348
TRAVIS: true,
5449
CI: true
5550
};
5651

57-
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT/);
52+
await expect(cli({env})).rejects.toContain('TRAVIS_COMMIT');
5853
});
5954

60-
/* Test.failing(
61-
'should throw when on travis ci, but TRAVIS_BRANCH is missing',
62-
async t => {
63-
const env = {
64-
TRAVIS: true,
65-
CI: true
66-
};
67-
68-
await t.throwsAsync(bin({env}), /TRAVIS_BRANCH/);
69-
}
70-
);
55+
test('should throw when on travis ci, but TRAVIS_BRANCH is missing', async () => {
56+
const env = {
57+
TRAVIS: true,
58+
CI: true
59+
};
7160

72-
test.failing('should call git with expected args on shallow repo', async t => {
73-
if (os.platform() === 'win32') {
74-
t.pass();
75-
return;
76-
}
61+
await expect(cli({env})).rejects.toContain('TRAVIS_BRANCH');
62+
});
7763

78-
const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git', [
79-
'--depth=10'
80-
]);
64+
/*
65+
test('should call git with expected args on shallow repo', async () => {
66+
const cwd = await git.clone(
67+
'https://github.com/conventional-changelog/commitlint.git',
68+
['--depth=10']
69+
);
8170
8271
const env = {
8372
TRAVIS: true,
@@ -88,7 +77,7 @@ test.failing('should call git with expected args on shallow repo', async t => {
8877
TRAVIS_COMMITLINT_GIT_BIN
8978
};
9079
91-
const result = await bin({cwd, env});
80+
const result = await cli({cwd, env});
9281
const invocations = await getInvocations(result.stdout);
9382
t.is(invocations.length, 7);
9483
@@ -143,76 +132,72 @@ test.failing('should call git with expected args on shallow repo', async t => {
143132
]);
144133
});
145134
146-
test.failing(
147-
'should call git with expected args on unshallow repo',
148-
async t => {
149-
if (os.platform() === 'win32') {
150-
t.pass();
151-
return;
152-
}
153-
154-
const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git');
155-
156-
const env = {
157-
TRAVIS: true,
158-
CI: true,
159-
TRAVIS_BRANCH,
160-
TRAVIS_COMMIT,
161-
TRAVIS_COMMITLINT_BIN,
162-
TRAVIS_COMMITLINT_GIT_BIN
163-
};
164-
165-
const result = await bin({cwd, env});
166-
const invocations = await getInvocations(result.stdout);
167-
t.is(invocations.length, 6);
168-
169-
const [stash, branches, checkout, back, pop, commilint] = invocations;
170-
171-
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
172-
t.deepEqual(branches, [
173-
NODE_BIN,
174-
TRAVIS_COMMITLINT_GIT_BIN,
175-
'remote',
176-
'set-branches',
177-
'origin',
178-
TRAVIS_BRANCH
179-
]);
180-
t.deepEqual(checkout, [
181-
NODE_BIN,
182-
TRAVIS_COMMITLINT_GIT_BIN,
183-
'checkout',
184-
TRAVIS_BRANCH,
185-
'--quiet'
186-
]);
187-
t.deepEqual(back, [
188-
NODE_BIN,
189-
TRAVIS_COMMITLINT_GIT_BIN,
190-
'checkout',
191-
'-',
192-
'--quiet'
193-
]);
194-
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
195-
t.deepEqual(commilint, [
196-
NODE_BIN,
197-
TRAVIS_COMMITLINT_BIN,
198-
'--from',
199-
TRAVIS_BRANCH,
200-
'--to',
201-
TRAVIS_COMMIT
202-
]);
203-
}
204-
);
135+
test('should call git with expected args on unshallow repo', async () => {
136+
const cwd = await git.clone(
137+
'https://github.com/conventional-changelog/commitlint.git'
138+
);
139+
140+
const env = {
141+
TRAVIS: true,
142+
CI: true,
143+
TRAVIS_BRANCH,
144+
TRAVIS_COMMIT,
145+
TRAVIS_COMMITLINT_BIN,
146+
TRAVIS_COMMITLINT_GIT_BIN
147+
};
148+
149+
const result = await cli({cwd, env});
150+
const invocations = await getInvocations(result.stdout);
151+
t.is(invocations.length, 6);
152+
153+
const [stash, branches, checkout, back, pop, commilint] = invocations;
154+
155+
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
156+
t.deepEqual(branches, [
157+
NODE_BIN,
158+
TRAVIS_COMMITLINT_GIT_BIN,
159+
'remote',
160+
'set-branches',
161+
'origin',
162+
TRAVIS_BRANCH
163+
]);
164+
t.deepEqual(checkout, [
165+
NODE_BIN,
166+
TRAVIS_COMMITLINT_GIT_BIN,
167+
'checkout',
168+
TRAVIS_BRANCH,
169+
'--quiet'
170+
]);
171+
t.deepEqual(back, [
172+
NODE_BIN,
173+
TRAVIS_COMMITLINT_GIT_BIN,
174+
'checkout',
175+
'-',
176+
'--quiet'
177+
]);
178+
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
179+
t.deepEqual(commilint, [
180+
NODE_BIN,
181+
TRAVIS_COMMITLINT_BIN,
182+
'--from',
183+
TRAVIS_BRANCH,
184+
'--to',
185+
TRAVIS_COMMIT
186+
]);
187+
});
205188
206189
function getInvocations(stdout) {
207190
const matches = stdout.match(/[^[\]]+/g);
208191
const raw = Array.isArray(matches) ? matches : [];
209192
210-
return raw.filter(invocation => invocation !== '\n').map(invocation =>
211-
invocation
212-
.split(',')
213-
.map(fragment => fragment.trim())
214-
.map(fragment => fragment.substring(1, fragment.length - 1))
215-
.filter(Boolean)
216-
);
193+
return raw
194+
.filter(invocation => invocation !== '\n')
195+
.map(invocation =>
196+
invocation
197+
.split(',')
198+
.map(fragment => fragment.trim())
199+
.map(fragment => fragment.substring(1, fragment.length - 1))
200+
.filter(Boolean)
201+
);
217202
}
218203
*/

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
testMatch: [
66
'**/*.test.ts?(x)',
77
'**/@commitlint/read/src/*.test.js?(x)',
8+
'**/@commitlint/travis-cli/src/*.test.js?(x)',
89
'**/@commitlint/cli/src/*.test.js?(x)'
910
]
1011
};

0 commit comments

Comments
 (0)