Доступ к захваченному выводу stderr в Jest - PullRequest
0 голосов
/ 18 января 2019

Jest захватывает вывод stdout и stderr. Можно ли получить доступ к этой захваченной информации в тесте?

С уважением, нидкил

1 Ответ

0 голосов
/ 21 января 2019

Я поступил об этом неправильно. Вместо того, чтобы использовать spy / mock, я пытался перехватить stdout / stderr напрямую. Я решил это, используя следующую функцию.

/* eslint-disable no-undef */
export function spyConsole() {
  let spy = {}

  beforeEach(() => {
    spy.console = jest.spyOn(console, 'error').mockImplementation(() => {})
  })

  afterEach(() => {
    spy.console.mockClear()
  })

  afterAll(() => {
    spy.console.mockRestore()
  })

  return spy
}

Который используется следующим образом:

import { createLocalVue, mount } from '@vue/test-utils'
import { spyConsole } from '@tst/helpers/test-utils'
import Vuetify from 'vuetify'
import VBtnPlus from '@/components/common/VBtnPlus.vue'

describe('VStatsCard.vue', () => {
  let localVue = null

  beforeEach(() => {
    localVue = createLocalVue()
    localVue.use(Vuetify)
  })

  describe('test prop warnings', () => {
    let spy = spyConsole()

    it('displays warning messages when both label and icon are not specified', () => {
      mount(VBtnPlus, {
        localVue: localVue
      })
      expect(console.error).toHaveBeenCalledTimes(1)
      expect(spy.console.mock.calls[0][0]).toContain(
        '[Vue warn]: Missing required prop, specify at least one of the following: "label" or "icon"'
      )
    })
  })
})

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...