-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
161 lines (123 loc) · 4.08 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
import path from 'path';
import fs from 'fs';
import test from 'ava';
import dotProp from 'dot-prop';
import execa from 'execa';
import tempWrite from 'temp-write';
import createAva from '.';
const {get} = dotProp;
const runWithoutInstall = async (packageJson, additionalOptions) => {
const filePath = tempWrite.sync(JSON.stringify(packageJson), 'package.json');
await createAva({
cwd: path.dirname(filePath),
skipInstall: true,
...additionalOptions
});
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
};
test('empty package.json', async t => {
t.is(get(await runWithoutInstall({}), 'scripts.test'), 'ava');
});
test('has scripts', async t => {
const pkg = await runWithoutInstall({
scripts: {
start: ''
}
});
t.is(get(pkg, 'scripts.test'), 'ava');
});
test('has default test', async t => {
const pkg = await runWithoutInstall({
scripts: {
test: 'echo "Error: no test specified" && exit 1'
}
});
t.is(get(pkg, 'scripts.test'), 'ava');
});
test('has only AVA', async t => {
const pkg = await runWithoutInstall({
scripts: {
test: 'ava'
}
});
t.is(get(pkg, 'scripts.test'), 'ava');
});
test('has test', async t => {
const pkg = await runWithoutInstall({
scripts: {
test: 'foo'
}
});
t.is(get(pkg, 'scripts.test'), 'foo && ava');
});
test('has cli args', async t => {
const args = ['--foo'];
const pkg = await runWithoutInstall({
scripts: {
start: ''
}
}, {args});
t.is(get(pkg, 'scripts.test'), 'ava --foo');
});
test('has cli args and existing binary', async t => {
const args = ['--foo', '--bar'];
const pkg = await runWithoutInstall({
scripts: {
test: 'foo'
}
}, {args});
t.is(get(pkg, 'scripts.test'), 'foo && ava --foo --bar');
});
test('does not remove empty dependency properties', async t => {
const pkg = await runWithoutInstall({
dependencies: {},
devDependencies: {},
optionalDependencies: {},
peerDependencies: {}
});
t.truthy(get(pkg, 'dependencies'));
t.truthy(get(pkg, 'devDependencies'));
t.truthy(get(pkg, 'optionalDependencies'));
t.truthy(get(pkg, 'peerDependencies'));
});
test.serial('installs the AVA dependency', async t => {
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await createAva({cwd: path.dirname(filepath)});
const installed = get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'devDependencies.ava');
t.truthy(installed);
t.regex(installed, /^\^/);
});
test.serial('installs AVA@next', async t => {
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await createAva({cwd: path.dirname(filepath), next: true});
const installed = get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'devDependencies.ava');
t.truthy(installed);
t.regex(installed, /^\d/);
});
test.serial('installs via yarn if there\'s a lockfile', async t => {
const yarnLock = tempWrite.sync('', 'yarn.lock');
await createAva({cwd: path.dirname(yarnLock)});
t.regex(fs.readFileSync(yarnLock, 'utf8'), /ava/);
});
test.serial('installs AVA@next via yarn if there\'s a lockfile', async t => {
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
const yarnLock = path.join(path.dirname(filepath), 'yarn.lock');
fs.writeFileSync(yarnLock, '');
await createAva({cwd: path.dirname(yarnLock), next: true});
t.regex(fs.readFileSync(yarnLock, 'utf8'), /ava/);
const installed = get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'devDependencies.ava');
t.truthy(installed);
t.regex(installed, /^\d/);
});
test.serial('invokes via cli', async t => {
const cliFilepath = path.resolve(__dirname, './cli.js');
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await execa(cliFilepath, [], {cwd: path.dirname(filepath)});
t.is(get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'scripts.test'), 'ava');
});
test.serial('interprets cli arguments', async t => {
const cliFilepath = path.resolve(__dirname, './cli.js');
const filepath = tempWrite.sync(JSON.stringify({}), 'package.json');
await execa(cliFilepath, ['--foo', '--bar'], {cwd: path.dirname(filepath)});
t.is(get(JSON.parse(fs.readFileSync(filepath, 'utf8')), 'scripts.test'), 'ava --foo --bar');
});