Skip to content

Force test to fail when stderr prints out stuffs #1948

Answered by sheremet-va
ahnpnl asked this question in Q&A
Discussion options

You must be logged in to vote

You can do something like this, add to test.setupFiles:

beforeEach((context) => {
  context.failOnConsole = true // so you can modify this behaviour inside a test, if you don't want to fail
  ['log', 'warn', 'error'].forEach(method => {
    vi.spyOn(console, method).mockClear()
  })
})

afterEach((context) => {
  if (!context.failOnConsole) return
  ['log', 'warn', 'error'].forEach(method => {
    expect(console[method]).not.toHaveBeenCalled()
  })
})

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ahnpnl
Comment options

Answer selected by ahnpnl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants