Skip to content

Commit 5019bc5

Browse files
committed
fix: tweak error message when input is wrong type
1 parent 3cfdf1e commit 5019bc5

File tree

3 files changed

+81
-67
lines changed

3 files changed

+81
-67
lines changed

classes/semver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SemVer {
1616
version = version.version
1717
}
1818
} else if (typeof version !== 'string') {
19-
throw new TypeError(`Invalid Version: ${version}`)
19+
throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
2020
}
2121

2222
if (version.length > MAX_LENGTH) {

test/classes/semver.js

+79-65
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,60 @@ const comparisons = require('../fixtures/comparisons.js')
55
const equality = require('../fixtures/equality.js')
66
const invalidVersions = require('../fixtures/invalid-versions')
77

8-
test('comparisons', t => {
8+
test('comparisons', (t) => {
99
t.plan(comparisons.length)
10-
comparisons.forEach(([v0, v1, opt]) => t.test(`${v0} ${v1}`, t => {
11-
const s0 = new SemVer(v0, opt)
12-
const s1 = new SemVer(v1, opt)
13-
t.equal(s0.compare(s1), 1)
14-
t.equal(s0.compare(v1), 1)
15-
t.equal(s1.compare(s0), -1)
16-
t.equal(s1.compare(v0), -1)
17-
t.equal(s0.compare(v0), 0)
18-
t.equal(s1.compare(v1), 0)
19-
t.end()
20-
}))
10+
comparisons.forEach(([v0, v1, opt]) =>
11+
t.test(`${v0} ${v1}`, (t) => {
12+
const s0 = new SemVer(v0, opt)
13+
const s1 = new SemVer(v1, opt)
14+
t.equal(s0.compare(s1), 1)
15+
t.equal(s0.compare(v1), 1)
16+
t.equal(s1.compare(s0), -1)
17+
t.equal(s1.compare(v0), -1)
18+
t.equal(s0.compare(v0), 0)
19+
t.equal(s1.compare(v1), 0)
20+
t.end()
21+
})
22+
)
2123
})
2224

23-
test('equality', t => {
25+
test('equality', (t) => {
2426
t.plan(equality.length)
25-
equality.forEach(([v0, v1, loose]) => t.test(`${v0} ${v1} ${loose}`, t => {
26-
const s0 = new SemVer(v0, loose)
27-
const s1 = new SemVer(v1, loose)
28-
t.equal(s0.compare(s1), 0)
29-
t.equal(s1.compare(s0), 0)
30-
t.equal(s0.compare(v1), 0)
31-
t.equal(s1.compare(v0), 0)
32-
t.equal(s0.compare(s0), 0)
33-
t.equal(s1.compare(s1), 0)
34-
t.equal(s0.comparePre(s1), 0, 'comparePre just to hit that code path')
35-
t.end()
36-
}))
27+
equality.forEach(([v0, v1, loose]) =>
28+
t.test(`${v0} ${v1} ${loose}`, (t) => {
29+
const s0 = new SemVer(v0, loose)
30+
const s1 = new SemVer(v1, loose)
31+
t.equal(s0.compare(s1), 0)
32+
t.equal(s1.compare(s0), 0)
33+
t.equal(s0.compare(v1), 0)
34+
t.equal(s1.compare(v0), 0)
35+
t.equal(s0.compare(s0), 0)
36+
t.equal(s1.compare(s1), 0)
37+
t.equal(s0.comparePre(s1), 0, 'comparePre just to hit that code path')
38+
t.end()
39+
})
40+
)
3741
})
3842

39-
test('toString equals parsed version', t => {
43+
test('toString equals parsed version', (t) => {
4044
t.equal(String(new SemVer('v1.2.3')), '1.2.3')
4145
t.end()
4246
})
4347

44-
test('throws when presented with garbage', t => {
48+
test('throws when presented with garbage', (t) => {
4549
t.plan(invalidVersions.length)
4650
invalidVersions.forEach(([v, msg, opts]) =>
47-
t.throws(() => new SemVer(v, opts), msg))
51+
t.throws(() => new SemVer(v, opts), msg)
52+
)
4853
})
4954

50-
test('return SemVer arg to ctor if options match', t => {
55+
test('return SemVer arg to ctor if options match', (t) => {
5156
const s = new SemVer('1.2.3', { loose: true, includePrerelease: true })
52-
t.equal(new SemVer(s, { loose: true, includePrerelease: true }), s,
53-
'get same object when options match')
57+
t.equal(
58+
new SemVer(s, { loose: true, includePrerelease: true }),
59+
s,
60+
'get same object when options match'
61+
)
5462
t.not(new SemVer(s), s, 'get new object when options match')
5563
t.end()
5664
})
@@ -62,37 +70,39 @@ test('really big numeric prerelease value', (t) => {
6270
})
6371

6472
test('invalid version numbers', (t) => {
65-
['1.2.3.4',
66-
'NOT VALID',
67-
1.2,
68-
null,
69-
'Infinity.NaN.Infinity',
70-
].forEach((v) => {
71-
t.throws(() => {
72-
new SemVer(v) // eslint-disable-line no-new
73-
}, { name: 'TypeError', message: `Invalid Version: ${v}` })
73+
['1.2.3.4', 'NOT VALID', 1.2, null, 'Infinity.NaN.Infinity'].forEach((v) => {
74+
t.throws(
75+
() => {
76+
new SemVer(v) // eslint-disable-line no-new
77+
},
78+
{
79+
name: 'TypeError',
80+
message:
81+
typeof v === 'string'
82+
? `Invalid Version: ${v}`
83+
: `Invalid version. Must be a string. Got type "${typeof v}".`,
84+
}
85+
)
7486
})
7587

7688
t.end()
7789
})
7890

79-
test('incrementing', t => {
91+
test('incrementing', (t) => {
8092
t.plan(increments.length)
81-
increments.forEach(([
82-
version,
83-
inc,
84-
expect,
85-
options,
86-
id,
87-
base,
88-
]) => t.test(`${version} ${inc} ${id || ''}`.trim(), t => {
89-
t.plan(1)
90-
if (expect === null) {
91-
t.throws(() => new SemVer(version, options).inc(inc, id, base))
92-
} else {
93-
t.equal(new SemVer(version, options).inc(inc, id, base).version, expect)
94-
}
95-
}))
93+
increments.forEach(([version, inc, expect, options, id, base]) =>
94+
t.test(`${version} ${inc} ${id || ''}`.trim(), (t) => {
95+
t.plan(1)
96+
if (expect === null) {
97+
t.throws(() => new SemVer(version, options).inc(inc, id, base))
98+
} else {
99+
t.equal(
100+
new SemVer(version, options).inc(inc, id, base).version,
101+
expect
102+
)
103+
}
104+
})
105+
)
96106
})
97107

98108
test('compare main vs pre', (t) => {
@@ -113,15 +123,19 @@ test('compare main vs pre', (t) => {
113123
})
114124

115125
test('invalid version numbers', (t) => {
116-
['1.2.3.4',
117-
'NOT VALID',
118-
1.2,
119-
null,
120-
'Infinity.NaN.Infinity',
121-
].forEach((v) => {
122-
t.throws(() => {
123-
new SemVer(v) // eslint-disable-line no-new
124-
}, { name: 'TypeError', message: `Invalid Version: ${v}` })
126+
['1.2.3.4', 'NOT VALID', 1.2, null, 'Infinity.NaN.Infinity'].forEach((v) => {
127+
t.throws(
128+
() => {
129+
new SemVer(v) // eslint-disable-line no-new
130+
},
131+
{
132+
name: 'TypeError',
133+
message:
134+
typeof v === 'string'
135+
? `Invalid Version: ${v}`
136+
: `Invalid version. Must be a string. Got type "${typeof v}".`,
137+
}
138+
)
125139
})
126140

127141
t.end()

test/functions/parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ t.test('throw errors if asked to', t => {
2020
parse([], null, true)
2121
}, {
2222
name: 'TypeError',
23-
message: 'Invalid Version: ',
23+
message: 'Invalid version. Must be a string. Got type "object".',
2424
})
2525
t.end()
2626
})

0 commit comments

Comments
 (0)