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 hexo.config.minify.css = customOpt
let result, expected let expected
try {
await c(input, { path })
} catch (err) {
result = err.message
}
try { try {
await new CleanCSS(customOpt).minify(input) await new CleanCSS(customOpt).minify(input)
} catch (err) { } catch (err) {
expected = err.message expected = err.message
} }
expect(result).toContain(expected) expect(expected).toBeDefined()
await expect(c(input, { path })).rejects.toThrow(expected)
}) })
test('exclude - *.min.css', async () => { test('exclude - *.min.css', async () => {

View File

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

View File

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

View File

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

View File

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