-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.js
169 lines (139 loc) · 3.9 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
import path from 'node:path';
import fs from 'node:fs';
import process from 'node:process';
import tempWrite from 'temp-write';
import {getProperty} from 'dot-prop';
import test from 'ava';
import createXo from './index.js';
const originalArgv = [...process.argv];
async function run(pkg) {
const filepath = tempWrite.sync(JSON.stringify(pkg), 'package.json');
await createXo({
cwd: path.dirname(filepath),
skipInstall: true,
});
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
}
test('empty package.json', async t => {
const pkg = await run({});
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo'), undefined);
});
test('has scripts', async t => {
const pkg = await run({
scripts: {
start: '',
},
});
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo'), undefined);
});
test('has default test', async t => {
const pkg = await run({
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
});
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo'), undefined);
});
test('has only xo', async t => {
const pkg = await run({
scripts: {
test: 'xo',
},
});
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo'), undefined);
});
test('has test', async t => {
const pkg = await run({
scripts: {
test: 'ava',
},
});
t.is(getProperty(pkg, 'scripts.test'), 'xo && ava');
t.is(getProperty(pkg, 'xo'), undefined);
});
test('has cli args', async t => {
process.argv = [...originalArgv, '--space'];
const pkg = await run({
scripts: {
start: '',
},
});
process.argv = originalArgv;
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo.space'), true);
});
test('has cli args and test', async t => {
process.argv = [...originalArgv, '--env=node', '--env=browser'];
const pkg = await run({
scripts: {
test: 'ava',
},
});
process.argv = originalArgv;
t.is(getProperty(pkg, 'scripts.test'), 'xo && ava');
t.is(getProperty(pkg, 'xo.envs[0]'), 'node');
t.is(getProperty(pkg, 'xo.envs[1]'), 'browser');
});
test('has cli args and existing config', async t => {
process.argv = [...originalArgv, '--space'];
const pkg = await run({
xo: {
esnext: true,
},
});
process.argv = originalArgv;
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo.space'), true);
t.is(getProperty(pkg, 'xo.esnext'), true);
});
test('has existing config without cli args', async t => {
process.argv = originalArgv;
const pkg = await run({
xo: {
esnext: true,
},
});
process.argv = originalArgv;
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.deepEqual(getProperty(pkg, 'xo'), {esnext: true});
});
test('has everything covered when it comes to config', async t => {
process.argv = [
...originalArgv,
'--space',
'--esnext',
'--no-semicolon',
'--env=foo',
'--env=bar',
'--global=foo',
'--global=bar',
'--ignore=foo',
'--ignore=bar',
];
const pkg = await run({});
process.argv = originalArgv;
t.is(getProperty(pkg, 'scripts.test'), 'xo');
t.is(getProperty(pkg, 'xo.space'), true);
t.is(getProperty(pkg, 'xo.esnext'), true);
t.is(getProperty(pkg, 'xo.semicolon'), false);
t.is(getProperty(pkg, 'xo.envs[0]'), 'foo');
t.is(getProperty(pkg, 'xo.envs[1]'), 'bar');
t.is(getProperty(pkg, 'xo.globals[0]'), 'foo');
t.is(getProperty(pkg, 'xo.globals[1]'), 'bar');
t.is(getProperty(pkg, 'xo.ignores[0]'), 'foo');
t.is(getProperty(pkg, 'xo.ignores[1]'), 'bar');
});
test('installs the XO dependency', async t => {
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await createXo({cwd: path.dirname(filepath)});
t.truthy(getProperty(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'devDependencies.xo'));
});
test('installs via yarn if there\'s a lockfile', async t => {
const yarnLock = tempWrite.sync('', 'yarn.lock');
await createXo({cwd: path.dirname(yarnLock)});
t.regex(fs.readFileSync(yarnLock, 'utf8'), /xo/);
});