test: utilise toThrow() to catch error

- try...catch would (incorrectly) pass the test, despite having no error
This commit is contained in:
curbengh 2020-01-09 15:14:25 +00:00
parent b9f4cd6173
commit 81ab472fe1
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
5 changed files with 12 additions and 27 deletions

View File

@ -65,20 +65,15 @@ describe('css', () => {
}
hexo.config.minify.css = customOpt
let result, expected
try {
await c(input, { path })
} catch (err) {
result = err.message
}
let expected
try {
await new CleanCSS(customOpt).minify(input)
} catch (err) {
expected = err.message
}
expect(result).toContain(expected)
expect(expected).toBeDefined()
await expect(c(input, { path })).rejects.toThrow(expected)
})
test('exclude - *.min.css', async () => {

View File

@ -99,11 +99,8 @@ describe('gzip', () => {
} catch (err) {
expected = err.message
}
try {
await g()
} catch (err) {
expect(err.message).toContain(expected)
}
expect(expected).toBeDefined()
await expect(g()).rejects.toThrow(expected)
})
test('include - exclude non-text file by default', async () => {

View File

@ -69,11 +69,11 @@ describe('js', () => {
hexo.config.minify.js = customOpt
const { error } = Terser.minify(input, customOpt)
try {
expect(error.message).toBeDefined()
expect(() => {
j(input, { path })
} catch (err) {
expect(err.message).toContain(error.message)
}
}).toThrow(error.message)
})
test('exclude - *.min.js', () => {

View File

@ -51,11 +51,7 @@ describe('xml', () => {
test('invalid input', async () => {
hexo.route.set(path, 'foo')
try {
await jsonFn()
} catch (err) {
expect(err.message).toContain('SyntaxError')
}
await expect(jsonFn()).rejects.toThrow(/SyntaxError/)
})
test('empty file', async () => {

View File

@ -81,11 +81,8 @@ describe('svg', () => {
} catch (err) {
expected = err
}
try {
await s()
} catch (err) {
expect(err.message).toContain(expected)
}
expect(expected).toBeDefined()
await expect(s()).rejects.toThrow(expected)
})
test('include - exclude *.min.svg by default', async () => {