-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
181 lines (149 loc) · 4 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const {dirname, join} = require('path');
const {execFile} = require('child_process');
const {mkdir, writeFile} = require('fs').promises;
const {promisify} = require('util');
const attempt = require('lodash/attempt');
const rmfr = require('rmfr');
const test = require('tape');
const coverage = require.resolve('.');
const execNode = promisify(execFile).bind(null, process.execPath);
const timeout = 1000000;
test('A `coverage` command with a command', async t => {
const {stderr, stdout} = await execNode([coverage, 'node', '-e', 'console.error("Hi")'], {timeout});
t.ok(
stdout.includes('All files'),
'should print a coverage report.'
);
t.ok(
stderr.startsWith('Hi'),
'should print output of the original command.'
);
const result = attempt(require.resolve, join(__dirname, 'coverage', 'index.html'));
if (/^1|true$/ui.test(process.env.CI) || !!process.env.GITHUB_ACTION) {
t.equal(
result.code,
'MODULE_NOT_FOUND',
'should write no HTML reports to ./coverage.'
);
} else {
t.equal(
typeof result,
'string',
'should write HTML reports to ./coverage.'
);
}
t.end();
});
test('A `coverage` command with a file path', async t => {
const {stdout} = await execNode([coverage, require.resolve('./package.json')], {timeout});
t.ok(
stdout.includes('All files'),
'should print a coverage report.'
);
t.end();
});
test('A `coverage` command with c8 flags', async t => {
try {
await execNode([coverage, '--reporter=unknown', 'node', '--version'], {timeout});
t.fail('Unexpectedly succeeded.');
} catch ({stderr}) {
t.ok(
stderr.includes('Cannot find module \'unknown\''),
'should pass flags to the underlying `c8` command.'
);
}
t.end();
});
test('A `coverage` command with a non-executable path', async t => {
try {
await execNode([coverage, require.resolve('./.gitattributes')], {timeout});
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.ok(
stderr.includes('SyntaxError'),
'should write error messages to the stderr.'
);
t.ok(
stdout.includes('All files'),
'should print a coverage report.'
);
t.equal(
code,
1,
'should reflect exit code of the spawned process.'
);
}
t.end();
});
test('A `coverage` command with a non-existing command', async t => {
try {
await execNode([coverage, 'this-command-does-not-exist'], {timeout});
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.equal(
stderr,
`Both a command \`this-command-does-not-exist\` and a Node.js entry point ${
join(__dirname, 'this-command-does-not-exist')
} don't exist.\n`,
'should write an error message to the stderr.'
);
t.equal(
stdout,
'',
'should write nothing to the stdout.'
);
t.equal(
code,
127,
'should exit with code 127.'
);
}
t.end();
});
test('A `coverage` command with no arguments', async t => {
try {
await execNode([coverage], {timeout});
t.fail('Unexpectedly succeeded.');
} catch ({code, stderr, stdout}) {
t.ok(
stderr.includes('check whether coverage is within thresholds provided'),
'should write help to the stderr.'
);
t.equal(
stdout,
'',
'should write nothing to the stderr.'
);
t.equal(
code,
1,
'should exit with code 1.'
);
}
t.end();
});
test('A `coverage report` command', async t => {
const {stdout} = await execNode([coverage, 'report', '--reporter=text-summary'], {timeout});
t.ok(
stdout.includes('Coverage summary'),
'should report coverage.'
);
t.end();
});
if (Number(process.versions.node.split('.')[0]) >= 12) {
test('A `coverage` command with a .mjs file path', async t => {
const tmpFile = join(__dirname, 'tmp', 'tmp.mjs');
await rmfr(dirname(tmpFile));
await mkdir(dirname(tmpFile));
await writeFile(tmpFile, 'import pkg from "../package"');
await execNode([
coverage,
'--exclude=tmp.mjs',
`--temp-directory=${join(dirname(tmpFile), '_')}`,
tmpFile
], {timeout});
t.pass('should enable ECMAScript modules for .mjs files automatically.');
t.end();
});
}