简体   繁体   English

Mirage/Pretender 在 Vitest 中导致错误

[英]Mirage/Pretender causes errors in Vitest

I'm working on migrating from Jest to Vitest (alongside CRA > Vite migration), and I think I've got everything working, except that using Mirage causes errors.我正在从 Jest 迁移到 Vitest(与 CRA > Vite 迁移一起),我认为我已经完成了所有工作,除了使用 Mirage 会导致错误。 Setting the vite config test environment between happy-dom and 'jsdom' both give different errors, though they appear to be related or similar or the same (just with happy-dom giving much more useful information!happy-dom和'jsdom' 之间设置vite 配置测试环境都会给出不同的错误,尽管它们看起来是相关的或相似的或相同的(只是happy-dom提供了更多有用的信息!

My very simplified test:我非常简化的测试:

import { describe, expect, it } from "vitest"
import { createServer } from "miragejs";

describe('tests', () => {
  createServer({})
  it('works', () => {
    expect(true).toEqual(true)
  })
})

happydom error快乐世界错误

TypeError: Cannot read properties of undefined (reading 'prototype')
 ❯ interceptor node_modules/pretender/dist/pretender.js:1540:46
 ❯ new Pretender node_modules/pretender/dist/pretender.js:1638:32
 ❯ PretenderConfig._create node_modules/miragejs/dist/mirage-cjs.js:6398:14
 ❯ PretenderConfig.create node_modules/miragejs/dist/mirage-cjs.js:6259:27
 ❯ Server.config node_modules/miragejs/dist/mirage-cjs.js:6824:24
 ❯ new Server node_modules/miragejs/dist/mirage-cjs.js:6760:10
 ❯ Proxy.createServer node_modules/miragejs/dist/mirage-cjs.js:6725:10
 ❯ src/test.test.tsx:5:2
      3| 
      4| describe('tests', () => {
      5|   createServer({})
       |  ^
      6|   it('works', () => {
      7|     expect(true).toEqual(true)

jsdom error jsdom错误

Error: Errors occurred while running tests. For more information, see serialized error.
 ❯ Object.runTests node_modules/vitest/dist/chunk-vite-node-externalize.6956d2d9.mjs:7048:17
 ❯ processTicksAndRejections node:internal/process/task_queues:96:5
 ❯ async file:/Users/jtuzman-superdraft/dev/superdraft-core-admin/NEW-vite-admin/node_modules/vitest/dist/chunk-vite-node-externalize.6956d2d9.mjs:10545:9
 ❯ Vitest.runFiles node_modules/vitest/dist/chunk-vite-node-externalize.6956d2d9.mjs:10558:12
 ❯ Vitest.start node_modules/vitest/dist/chunk-vite-node-externalize.6956d2d9.mjs:10479:5
 ❯ startVitest node_modules/vitest/dist/chunk-vite-node-externalize.6956d2d9.mjs:11204:5
 ❯ start node_modules/vitest/dist/cli.mjs:666:9
 ❯ CAC.run node_modules/vitest/dist/cli.mjs:662:3

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: {
  "errors": [
    [Error: Internal error: Error constructor is not present on the given global object.],
  ],
}

vite.config.ts

/// <reference types="vitest" />

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'
import svgrPlugin from 'vite-plugin-svgr';
import macrosPlugin from "vite-plugin-babel-macros"
import checker from 'vite-plugin-checker'

// https://vitejs.dev/config/
export default defineConfig({
  test: {
    globals: true,
    environment: "jsdom", // or "happy-dom"
  },
  define: {
    global: {},
  },
  esbuild: {
    logOverride: { 'this-is-undefined-in-esm': 'silent' }
  },
  plugins: [
    react(),
    svgrPlugin({
      svgrOptions: {
        icon: true,
      },
    }),
    macrosPlugin(),
    checker({
      typescript: true,
      overlay: {
        panelStyle: 'height: 100vh; max-height: unset;'
      }
    })
  ],
});

This GitHub issue appears to deal with the same question but doesn't appear to solve it.这个 GitHub 问题似乎处理了同样的问题,但似乎没有解决它。

Ran into the same thing.遇到同样的事情。 It's the global: {} statement.它是global: {}语句。 When I commented it out in my vite.config.ts file, my tests ran successfully.当我在我的 vite.config.ts 文件中注释掉它时,我的测试运行成功。 I haven't figured out another way to solve the global error yet that caused me to set that in the first place though.我还没有想出另一种解决全局错误的方法,但这导致我首先设置它。

happy-dom does not provide an implementation for XMLHttpRequest, which is used by pretenderjs. happy-dom 没有提供 XMLHttpRequest 的实现,它是由 fakeerjs 使用的。 The most straightforward walk around is to put the following line at the very top of your test code, before the the vitest environment:最直接的做法是将以下行放在测试代码的最顶部,在 vitest 环境之前:

this.XMLHttpRequest = vi.fn() this.XMLHttpRequest = vi.fn()

//@vitest-environment happy-dom //@vitest-环境快乐-dom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM