Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit 604edfa

Browse files
committed
feat: automatically fail Cypress test on Vue-caught error
1 parent e543ea6 commit 604edfa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cypress/component/basic/translated-message-spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
/// <reference types="cypress" />
2+
13
// testing i18n component
24
// http://kazupon.github.io/vue-i18n
35
import TranslatedMessage from './TranslatedMessage.vue'
46
import VueI18n from 'vue-i18n'
57
import { mountCallback } from 'cypress-vue-unit-test'
68

7-
/* eslint-env mocha */
89
// https://github.com/bahmutov/cypress-vue-unit-test/issues/332
910
describe.skip('VueI18n', () => {
1011
// need to use VueI18n as a plugin

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ interface MountOptions {
286286
*/
287287
type MountOptionsArgument = Partial<ComponentOptions & MountOptions>
288288

289+
/**
290+
* Direct Vue errors to the top error handler
291+
* where they will fail Cypress test
292+
* @see https://vuejs.org/v2/api/#errorHandler
293+
* @see https://github.com/cypress-io/cypress/issues/7910
294+
*/
295+
function failTestOnVueError(err, vm, info) {
296+
console.error(`Vue error 🔥`)
297+
console.error(err)
298+
console.error('component:', vm)
299+
console.error('info:', info)
300+
window.top.onerror(err)
301+
}
302+
289303
/**
290304
* Mounts a Vue component inside Cypress browser.
291305
* @param {object} component imported from Vue file
@@ -351,6 +365,8 @@ export const mount = (
351365
.then((win) => {
352366
// @ts-ignore
353367
win.Vue = Vue
368+
// @ts-ignore
369+
win.Vue.config.errorHandler = failTestOnVueError
354370

355371
// @ts-ignore
356372
const document: Document = cy.state('document')

0 commit comments

Comments
 (0)