VueJs 如何删除全局错误处理程序以使用 vue-test-utils 进行测试
VueJs how to remove global error handler for testing with vue-test-utils
我是 运行 vue 中 canvas 的单元测试(测试通过)。但是我收到错误 console.error node_modules/@vue/test-utils/dist/vue-test-utils.js:1735 [vue-test-utils]: Global error handler detected (Vue.config.errorHandler). Vue Test Utils sets a custom error handler to throw errors thrown by instances. If you want this behavior in your tests, you must remove the global error handler.
我试过停用错误处理程序,但这似乎不受支持? https://vuejs.org/v2/api/#errorHandler 如何应用错误消息中的建议?
这里有一个类似的问题:How to disable the "Global error handler detected" warning in vue test utils,但没有有用的答案。
我的测试是使用 jest 和 fabric 编写的(针对 canvas):
import FabricCanvas from './FabricCanvas';
import { fabric } from 'fabric';
import { shallowMount } from '@vue/test-utils';
import PolygonDrawing from "@/components/labeling/editor/drawing/PolygonDrawing";
function getFabricCanvas(): fabric.Canvas {
const wrapper = shallowMount (FabricCanvas);
const canvas = wrapper.get('#main-canvas').html();
return new fabric.Canvas(canvas, {
width: 1024,
height: 1024,
} as any);
}
let fCanvas: fabric.Canvas | undefined;
beforeAll(() => {
fCanvas = getFabricCanvas();
})
test('add a Polygon to the canvas startDrawingPolygon and stopDrawingPolygon', () => {
const polygonDrawing = new PolygonDrawing();
if (fCanvas) {
expect(fCanvas._objects.length).toStrictEqual(0);
}
const objectClass = 'Vehicle';
const shapeType = 'polygon';
let startMousePoint = {x: 200, y: 200};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
startMousePoint = {x: 300, y: 300};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
startMousePoint = {x: 200, y: 300};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
if (fCanvas) {
polygonDrawing.stopDrawingPolygon(fCanvas, shapeType, objectClass);
expect(fCanvas._objects.length).toStrictEqual(1);
}
})
});
canvas 是使用文件 FabricCanvas.vue 模拟的:
<template>
<div id="canvas-container">
<canvas width="1024" height="1024" id="main-canvas"></canvas>
</div>
</template>
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
@Component
export default class FabricCanvas extends Vue {
}
</script>
<style scoped> </style>
感谢 @tony19 的提示,我找到了解决方案:在我的 router/index.js 中有一个来自 Elastic 的名为 APM(应用程序性能监控)的插件:import { ApmVuePlugin } from '@elastic/apm-rum-vue';
这对重复的全局错误处理程序负责,在测试期间停用它解决了错误消息。
我是 运行 vue 中 canvas 的单元测试(测试通过)。但是我收到错误 console.error node_modules/@vue/test-utils/dist/vue-test-utils.js:1735 [vue-test-utils]: Global error handler detected (Vue.config.errorHandler). Vue Test Utils sets a custom error handler to throw errors thrown by instances. If you want this behavior in your tests, you must remove the global error handler.
我试过停用错误处理程序,但这似乎不受支持? https://vuejs.org/v2/api/#errorHandler 如何应用错误消息中的建议?
这里有一个类似的问题:How to disable the "Global error handler detected" warning in vue test utils,但没有有用的答案。
我的测试是使用 jest 和 fabric 编写的(针对 canvas):
import FabricCanvas from './FabricCanvas';
import { fabric } from 'fabric';
import { shallowMount } from '@vue/test-utils';
import PolygonDrawing from "@/components/labeling/editor/drawing/PolygonDrawing";
function getFabricCanvas(): fabric.Canvas {
const wrapper = shallowMount (FabricCanvas);
const canvas = wrapper.get('#main-canvas').html();
return new fabric.Canvas(canvas, {
width: 1024,
height: 1024,
} as any);
}
let fCanvas: fabric.Canvas | undefined;
beforeAll(() => {
fCanvas = getFabricCanvas();
})
test('add a Polygon to the canvas startDrawingPolygon and stopDrawingPolygon', () => {
const polygonDrawing = new PolygonDrawing();
if (fCanvas) {
expect(fCanvas._objects.length).toStrictEqual(0);
}
const objectClass = 'Vehicle';
const shapeType = 'polygon';
let startMousePoint = {x: 200, y: 200};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
startMousePoint = {x: 300, y: 300};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
startMousePoint = {x: 200, y: 300};
polygonDrawing.startDrawingPolygon(fCanvas, startMousePoint, objectClass, shapeType);
if (fCanvas) {
polygonDrawing.stopDrawingPolygon(fCanvas, shapeType, objectClass);
expect(fCanvas._objects.length).toStrictEqual(1);
}
})
});
canvas 是使用文件 FabricCanvas.vue 模拟的:
<template>
<div id="canvas-container">
<canvas width="1024" height="1024" id="main-canvas"></canvas>
</div>
</template>
<script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
@Component
export default class FabricCanvas extends Vue {
}
</script>
<style scoped> </style>
感谢 @tony19 的提示,我找到了解决方案:在我的 router/index.js 中有一个来自 Elastic 的名为 APM(应用程序性能监控)的插件:import { ApmVuePlugin } from '@elastic/apm-rum-vue';
这对重复的全局错误处理程序负责,在测试期间停用它解决了错误消息。