Skip to content

Commit f1a2c36

Browse files
authored
fix(validator): do not log non-existent errors on higher log levels (#247)
1 parent af38489 commit f1a2c36

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

src/runtime/validator.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ export const useChecker = (
2929
html = typeof html === 'string' ? html.replace(/ ?data-v-[-a-z0-9]+\b/g, '') : html
3030
const { valid, results } = validator.validateString(html)
3131

32-
if (valid && !results.length && logLevel === 'verbose') {
33-
return console.log(
34-
`No HTML validation errors found for ${chalk.bold(url)}`
35-
)
32+
if (valid && !results.length) {
33+
if (logLevel === 'verbose') {
34+
console.log(`No HTML validation errors found for ${chalk.bold(url)}`)
35+
}
36+
37+
return
3638
}
3739

3840
if (!valid) { invalidPages.push(url) }
@@ -43,7 +45,7 @@ export const useChecker = (
4345
const message = [
4446
`HTML validation errors found for ${chalk.bold(url)}`,
4547
formattedResult
46-
].join('\n')
48+
].filter(Boolean).join('\n')
4749

4850
if (valid) {
4951
if (logLevel === 'verbose' || logLevel === 'warning') {

test/checker.test.ts

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,79 @@ describe('useChecker', () => {
2323
vi.clearAllMocks()
2424
})
2525

26-
it('logs an error', async () => {
27-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: false, results: [] }))
28-
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any)
29-
30-
await checker('https://test.com/', '<a><a>Link</a></a>')
31-
expect(mockValidator).toHaveBeenCalled()
32-
expect(console.error).toHaveBeenCalled()
33-
})
34-
35-
it('prints an error when invalid html is provided', async () => {
36-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: false, results: [] }))
26+
it('logs valid output', async () => {
27+
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
3728
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false)
3829

39-
await checker('https://test.com/', '<a>Link</a>')
40-
expect(mockValidator).toHaveBeenCalled()
41-
expect(console.error).toHaveBeenCalled()
42-
})
43-
44-
it('prints a warning when invalid html is provided and log level is set to verbose', async () => {
45-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [{ messages: [{ message: '' }] }] }))
46-
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'verbose')
47-
48-
await checker('https://test.com/', '<a>Link</a>')
49-
expect(mockValidator).toHaveBeenCalled()
50-
expect(console.warn).toHaveBeenCalled()
30+
await checker('https://test.com/', Symbol as any)
31+
expect(console.log).toHaveBeenCalledWith(
32+
`No HTML validation errors found for ${chalk.bold('https://test.com/')}`
33+
)
34+
expect(console.warn).not.toHaveBeenCalled()
5135
expect(console.error).not.toHaveBeenCalled()
5236
})
5337

54-
it('prints a warning when invalid html is provided and log level is set to warning', async () => {
38+
it('logs valid output', async () => {
5539
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
56-
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'warning')
40+
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'verbose')
5741

58-
await checker('https://test.com/', '<a>Link</a>')
59-
expect(mockValidator).toHaveBeenCalled()
60-
expect(console.warn).toHaveBeenCalled()
42+
await checker('https://test.com/', Symbol as any)
43+
expect(console.log).toHaveBeenCalledWith(
44+
`No HTML validation errors found for ${chalk.bold('https://test.com/')}`
45+
)
46+
expect(console.warn).not.toHaveBeenCalled()
6147
expect(console.error).not.toHaveBeenCalled()
6248
})
6349

64-
it('prints no warning when invalid html is provided and log level is set to error', async () => {
65-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
50+
for (const logLevel of ['warning', 'error'] as const) {
51+
it(`does not log valid output when logging on level ${logLevel}`, async () => {
52+
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
53+
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, logLevel)
54+
55+
await checker('https://test.com/', Symbol as any)
56+
expect(console.log).not.toHaveBeenCalled()
57+
expect(console.warn).not.toHaveBeenCalled()
58+
expect(console.error).not.toHaveBeenCalled()
59+
})
60+
}
61+
62+
for (const logLevel of [undefined, 'verbose', 'warning'] as const) {
63+
it(`logs a warning when valid html is provided with warnings and log level is set to ${logLevel}`, async () => {
64+
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [{ messages: [{ message: '' }] }] }))
65+
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, logLevel)
66+
67+
await checker('https://test.com/', Symbol as any)
68+
expect(console.log).not.toHaveBeenCalled()
69+
expect(console.warn).toHaveBeenCalled()
70+
expect(console.error).not.toHaveBeenCalled()
71+
})
72+
}
73+
74+
it('does not log a warning when valid html is provided with warnings and log level is set to error', async () => {
75+
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [{ messages: [{ message: '' }] }] }))
6676
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'error')
6777

68-
await checker('https://test.com/', '<a>Link</a>')
69-
expect(mockValidator).toHaveBeenCalled()
78+
await checker('https://test.com/', Symbol as any)
79+
expect(console.log).not.toHaveBeenCalled()
7080
expect(console.warn).not.toHaveBeenCalled()
7181
expect(console.error).not.toHaveBeenCalled()
7282
})
7383

84+
for (const logLevel of [undefined, 'verbose', 'warning', 'error'] as const) {
85+
it(`logs an error when invalid html is provided and log level is set to ${logLevel}`, async () => {
86+
const mockValidator = vi.fn().mockImplementation(() => ({ valid: false, results: [{ messages: [{ message: '' }] }] }))
87+
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, logLevel)
88+
89+
await checker('https://test.com/', '<a>Link</a>')
90+
expect(mockValidator).toHaveBeenCalled()
91+
expect(console.log).not.toHaveBeenCalled()
92+
expect(console.warn).not.toHaveBeenCalled()
93+
expect(console.error).toHaveBeenCalledWith(
94+
`HTML validation errors found for ${chalk.bold('https://test.com/')}`
95+
)
96+
})
97+
}
98+
7499
it('records urls when invalid html is provided', async () => {
75100
const mockValidator = vi.fn().mockImplementation(() => ({ valid: false, results: [] }))
76101
const { checkHTML: checker, invalidPages } = useChecker({ validateString: mockValidator } as any, false)
@@ -90,6 +115,10 @@ describe('useChecker', () => {
90115
expect(mockValidator).toHaveBeenCalledWith(
91116
'<a>Link</a>'
92117
)
118+
expect(console.log).toHaveBeenCalledWith(
119+
`No HTML validation errors found for ${chalk.bold('https://test.com/')}`
120+
)
121+
expect(console.warn).not.toHaveBeenCalled()
93122
expect(console.error).not.toHaveBeenCalled()
94123
})
95124

@@ -112,27 +141,13 @@ describe('useChecker', () => {
112141
await checker('https://test.com/', Symbol as any)
113142
const { format } = await import('prettier')
114143
expect(format).toHaveBeenCalledWith(Symbol, { parser: 'html' })
115-
expect(console.error).toHaveBeenCalled()
144+
expect(console.log).not.toHaveBeenCalled()
145+
expect(console.warn).not.toHaveBeenCalled()
146+
expect(console.error).toHaveBeenCalledWith(
147+
`HTML validation errors found for ${chalk.bold('https://test.com/')}`
148+
)
116149

117150
const validate = await import('html-validate')
118151
expect(validate.formatterFactory).not.toHaveBeenCalledWith('codeframe')
119152
})
120-
121-
it('logs valid output', async () => {
122-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
123-
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'verbose')
124-
125-
await checker('https://test.com/', Symbol as any)
126-
expect(console.log).toHaveBeenCalledWith(
127-
`No HTML validation errors found for ${chalk.bold('https://test.com/')}`
128-
)
129-
})
130-
131-
it('does not log valid output when logging on level warning', async () => {
132-
const mockValidator = vi.fn().mockImplementation(() => ({ valid: true, results: [] }))
133-
const { checkHTML: checker } = useChecker({ validateString: mockValidator } as any, false, 'warning')
134-
135-
await checker('https://test.com/', Symbol as any)
136-
expect(console.log).not.toHaveBeenCalled()
137-
})
138153
})

0 commit comments

Comments
 (0)