VueJs如何删除全局错误处理程序以使用vue-test-utils进行测试

gdx19jrr  于 2023-10-23  发布在  Vue.js
关注(0)|答案(2)|浏览(107)

我正在vue中运行画布的单元测试(测试通过)。返回错误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://v2.vuejs.org/v2/api/#errorHandler如何应用错误消息中的建议?
这里有一个类似的问题:How to disable the "Global error handler detected" warning in vue test utils,但没有有用的答案。
我的测试是使用jest和fabric编写的(对于画布):

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);
    }
  })
});

画布使用文件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>
sczxawaw

sczxawaw1#

感谢@tony19的提示,我找到了解决方案:Elastic有一个名为APM(应用程序性能监控)的插件,它位于我的router/index.js中:import { ApmVuePlugin } from '@elastic/apm-rum-vue';
这是重复的全局错误处理程序的原因,在测试期间将其停用解决了错误消息。

7jmck4yq

7jmck4yq2#

在我的项目中,我使用vue2@vitejs/plugin-vue2vitest。我也犯过类似的错误。我通过删除vitest.config.js中的'threads'-选项修复了它

// vitest.config.js
{
  ...
  threads: true,
  ...
}

相关问题