-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
test.js
90 lines (72 loc) · 2.82 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
import test from 'ava';
import uniqueString from 'unique-string';
import npmName, {npmNameMany, InvalidNameError} from './index.js';
const registryUrl = 'https://registry.yarnpkg.com/';
const options = {registryUrl};
test('returns true when package name is available', async t => {
const moduleName = uniqueString();
t.true(await npmName(moduleName));
t.true(await npmName(moduleName, options));
await t.throwsAsync(npmName(moduleName, {registryUrl: null}));
});
test('returns true when organization name is available', async t => {
const moduleName = uniqueString();
t.true(await npmName(`@${moduleName}`));
t.true(await npmName(`@${moduleName}/`));
});
test('returns false when package name is taken', async t => {
t.false(await npmName('chalk'));
t.false(await npmName('recursive-readdir'));
t.false(await npmName('np', options));
});
test.failing('returns false when package name is taken, regardless of punctuation', async t => {
t.false(await npmName('ch-alk'));
t.false(await npmName('recursivereaddir'));
});
test('returns false when organization name is taken', async t => {
t.false(await npmName('@ava'));
t.false(await npmName('@ava/'));
t.false(await npmName('@angular/'));
});
test('registry url is normalized', async t => {
const moduleName = uniqueString();
t.true(await npmName(moduleName, options));
t.true(await npmName(moduleName, {
registryUrl: registryUrl.slice(0, -1), // The `.slice()` removes the trailing `/` from the URL
}));
});
test('returns a map of multiple package names', async t => {
const name1 = 'chalk';
const name2 = uniqueString();
const result = await npmNameMany([name1, name2]);
t.false(result.get(name1));
t.true(result.get(name2));
await t.throwsAsync(npmNameMany([name1, name2], {registryUrl: null}));
});
test('returns true when scoped package name is not taken', async t => {
t.true(await npmName(`@${uniqueString()}/${uniqueString()}`));
});
test('returns false when scoped package name is taken', async t => {
t.false(await npmName('@sindresorhus/is'));
});
test('throws when package name is invalid', async t => {
await t.throwsAsync(npmName('_ABC'), {
instanceOf: InvalidNameError,
message: `Invalid package name: _ABC
- name can no longer contain capital letters
- name cannot start with an underscore`,
});
});
test('should return an iterable error capturing multiple errors when appropriate', async t => {
const name1 = 'chalk'; // False
const name2 = uniqueString(); // True
const name3 = '_ABC'; // Error
const name4 = 'CapitalsAreBad'; // Error
const aggregateError = await t.throwsAsync(npmNameMany([name1, name2, name3, name4]), {
instanceOf: AggregateError,
});
const errors = [...aggregateError.errors];
t.is(errors.length, 2);
t.regex(errors[0].message, /Invalid package name: _ABC/);
t.regex(errors[1].message, /Invalid package name: CapitalsAreBad/);
});