Skip to content

Commit f3af938

Browse files
armano2byCedric
andcommitted
test(travis-cli): migrate tests from ava to jest (#910)
* test(travis-cli): migrate tests from ava to jest * test(travis-cli): remove redundant tests * test(travis-cli): fix and enable mocked tests * test(travis-cli): restore some original code Co-authored-by: Cedric van Putten <[email protected]>
1 parent 4889cee commit f3af938

File tree

9 files changed

+110
-220
lines changed

9 files changed

+110
-220
lines changed

Diff for: @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 = '') => {

Diff for: @commitlint/travis-cli/fixtures/commitlint.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env node
2-
console.log(process.argv);
2+
3+
const args = process.argv;
4+
args.shift(); // remove node
5+
console.log(
6+
args.map((item, index) => {
7+
return index === 0 ? 'commitlint' : item;
8+
})
9+
);

Diff for: @commitlint/travis-cli/fixtures/git.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
#!/usr/bin/env node
2-
console.log(process.argv);
2+
3+
const args = process.argv;
4+
args.shift(); // remove node
5+
console.log(
6+
args.map((item, index) => {
7+
return index === 0 ? 'git' : item;
8+
})
9+
);

Diff for: @commitlint/travis-cli/package.json

+2-23
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"
@@ -61,15 +42,13 @@
6142
},
6243
"license": "MIT",
6344
"devDependencies": {
64-
"@babel/core": "7.7.7",
6545
"@babel/cli": "7.7.7",
46+
"@babel/core": "7.7.7",
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",
71-
"cross-env": "6.0.3",
72-
"which": "2.0.1"
51+
"cross-env": "6.0.3"
7352
},
7453
"dependencies": {
7554
"@commitlint/cli": "^8.3.5",

Diff for: @commitlint/travis-cli/src/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function lint(args, options) {
7777
}
7878

7979
async function log(hash) {
80-
const result = await execa('git', [
80+
const result = await execa(GIT, [
8181
'log',
8282
'-n',
8383
'1',

Diff for: @commitlint/travis-cli/src/cli.test.js

+74-178
Original file line numberDiff line numberDiff line change
@@ -1,218 +1,114 @@
1-
// Disable ftb
2-
// const os = require('os');
3-
// const {git} = require('@commitlint/test');
4-
import test from 'ava';
51
import execa from 'execa';
6-
// Disable ftb
7-
// const which = require('which');
8-
9-
// Disable ftb
10-
// const NODE_BIN = which.sync('node');
11-
const BIN = require.resolve('../lib/cli.js');
12-
13-
// Disable ftb
14-
// const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
15-
// const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
16-
// const TRAVIS_BRANCH = 'TRAVIS_BRANCH';
17-
// const TRAVIS_COMMIT = 'TRAVIS_COMMIT';
2+
import {git} from '@commitlint/test';
3+
4+
const bin = require.resolve('../lib/cli.js');
5+
6+
const TRAVIS_COMMITLINT_BIN = require.resolve('../fixtures/commitlint');
7+
const TRAVIS_COMMITLINT_GIT_BIN = require.resolve('../fixtures/git');
8+
9+
const validBaseEnv = {
10+
TRAVIS: true,
11+
CI: true,
12+
TRAVIS_COMMIT: 'TRAVIS_COMMIT',
13+
TRAVIS_COMMITLINT_BIN: TRAVIS_COMMITLINT_BIN,
14+
TRAVIS_COMMITLINT_GIT_BIN: TRAVIS_COMMITLINT_GIT_BIN,
15+
TRAVIS_COMMIT_RANGE: 'TRAVIS_COMMIT_A.TRAVIS_COMMIT_B',
16+
TRAVIS_EVENT_TYPE: 'TRAVIS_EVENT_TYPE',
17+
TRAVIS_REPO_SLUG: 'TRAVIS_REPO_SLUG',
18+
TRAVIS_PULL_REQUEST_SLUG: 'TRAVIS_PULL_REQUEST_SLUG'
19+
};
1820

19-
const bin = async (config = {}) => {
21+
const cli = async (config = {}) => {
2022
try {
21-
return await execa(BIN, Object.assign({extendEnv: false}, config));
23+
return await execa(bin, [], config);
2224
} catch (err) {
2325
throw new Error([err.stdout, err.stderr].join('\n'));
2426
}
2527
};
2628

27-
test('should throw when not on travis ci', async t => {
29+
test('should throw when not on travis ci', async () => {
2830
const env = {
2931
CI: false,
3032
TRAVIS: false
3133
};
3234

33-
await t.throwsAsync(
34-
bin({env}),
35-
/@commitlint\/travis-cli is intended to be used on Travis CI/
35+
await expect(cli({env})).rejects.toThrow(
36+
'@commitlint/travis-cli is intended to be used on Travis CI'
3637
);
3738
});
3839

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-
};
46-
47-
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT, TRAVIS_BRANCH/);
48-
}
49-
); */
50-
51-
test('should throw when on travis ci, but TRAVIS_COMMIT is missing', async t => {
40+
test('should throw when on travis ci, but env vars are missing', async () => {
5241
const env = {
5342
TRAVIS: true,
5443
CI: true
5544
};
5645

57-
await t.throwsAsync(bin({env}), /TRAVIS_COMMIT/);
46+
await expect(cli({env})).rejects.toThrow(
47+
'TRAVIS_COMMIT, TRAVIS_COMMIT_RANGE, TRAVIS_EVENT_TYPE, TRAVIS_REPO_SLUG, TRAVIS_PULL_REQUEST_SLUG'
48+
);
5849
});
5950

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-
};
51+
test('should call git with expected args', async () => {
52+
const cwd = await git.clone(
53+
'https://github.com/conventional-changelog/commitlint.git',
54+
['--depth=10'],
55+
__dirname,
56+
TRAVIS_COMMITLINT_GIT_BIN
57+
);
6758

68-
await t.throwsAsync(bin({env}), /TRAVIS_BRANCH/);
69-
}
70-
);
59+
const result = await cli({
60+
cwd,
61+
env: validBaseEnv
62+
});
63+
const invocations = await getInvocations(result.stdout);
64+
expect(invocations.length).toBe(3);
7165

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-
}
66+
const [stash, branches, commilint] = invocations;
7767

78-
const cwd = await git.clone('https://github.com/conventional-changelog/commitlint.git', [
79-
'--depth=10'
80-
]);
68+
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
69+
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
70+
expect(commilint).toEqual(['commitlint']);
71+
});
8172

82-
const env = {
83-
TRAVIS: true,
84-
CI: true,
85-
TRAVIS_BRANCH,
86-
TRAVIS_COMMIT,
87-
TRAVIS_COMMITLINT_BIN,
73+
test('should call git with expected args on pull_request', async () => {
74+
const cwd = await git.clone(
75+
'https://github.com/conventional-changelog/commitlint.git',
76+
['--depth=10'],
77+
__dirname,
8878
TRAVIS_COMMITLINT_GIT_BIN
89-
};
79+
);
9080

91-
const result = await bin({cwd, env});
81+
const result = await cli({
82+
cwd,
83+
env: {...validBaseEnv, TRAVIS_EVENT_TYPE: 'pull_request'}
84+
});
9285
const invocations = await getInvocations(result.stdout);
93-
t.is(invocations.length, 7);
94-
95-
const [
96-
stash,
97-
branches,
98-
unshallow,
99-
checkout,
100-
back,
101-
pop,
102-
commilint
103-
] = invocations;
104-
105-
t.deepEqual(stash, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash']);
106-
t.deepEqual(branches, [
107-
NODE_BIN,
108-
TRAVIS_COMMITLINT_GIT_BIN,
109-
'remote',
110-
'set-branches',
111-
'origin',
112-
TRAVIS_BRANCH
113-
]);
114-
t.deepEqual(unshallow, [
115-
NODE_BIN,
116-
TRAVIS_COMMITLINT_GIT_BIN,
117-
'fetch',
118-
'--unshallow',
119-
'--quiet'
120-
]);
121-
t.deepEqual(checkout, [
122-
NODE_BIN,
123-
TRAVIS_COMMITLINT_GIT_BIN,
124-
'checkout',
125-
TRAVIS_BRANCH,
126-
'--quiet'
127-
]);
128-
t.deepEqual(back, [
129-
NODE_BIN,
130-
TRAVIS_COMMITLINT_GIT_BIN,
131-
'checkout',
132-
'-',
133-
'--quiet'
134-
]);
135-
t.deepEqual(pop, [NODE_BIN, TRAVIS_COMMITLINT_GIT_BIN, 'stash', 'pop']);
136-
t.deepEqual(commilint, [
137-
NODE_BIN,
138-
TRAVIS_COMMITLINT_BIN,
86+
expect(invocations.length).toBe(3);
87+
88+
const [stash, branches, commilint] = invocations;
89+
90+
expect(stash).toEqual(['git', 'stash', '-k', '-u', '--quiet']);
91+
expect(branches).toEqual(['git', 'stash', 'pop', '--quiet']);
92+
expect(commilint).toEqual([
93+
'commitlint',
13994
'--from',
140-
TRAVIS_BRANCH,
95+
'TRAVIS_COMMIT_A',
14196
'--to',
142-
TRAVIS_COMMIT
97+
'TRAVIS_COMMIT_B'
14398
]);
14499
});
145100

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-
);
205-
206101
function getInvocations(stdout) {
207102
const matches = stdout.match(/[^[\]]+/g);
208103
const raw = Array.isArray(matches) ? matches : [];
209104

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-
);
105+
return raw
106+
.filter(invocation => invocation !== '\n')
107+
.map(invocation =>
108+
invocation
109+
.split(',')
110+
.map(fragment => fragment.trim())
111+
.map(fragment => fragment.substring(1, fragment.length - 1))
112+
.filter(Boolean)
113+
);
217114
}
218-
*/

0 commit comments

Comments
 (0)