V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sonders
V2EX  ›  问与答

Jest 测试 Vue 组件报错问题

  •  
  •   sonders · 2023-02-21 14:28:45 +08:00 · 234 次点击
    这是一个创建于 402 天前的主题,其中的信息可能已经有所发展或是发生改变。

    test.spec.ts

    // test.spec.ts
    // import add from "./add";
    import { mount } from "@vue/test-utils";
    import HelloWorld from "../components/HelloWorld.vue";
    describe("test", () => {
      it("first", () => {d);
        expect(wrapper.text()).toConta
        const wrapper = mount(HelloWorlin("Hello world");
      });
    });
    
    

    image

    jest.config.js

    module.exports = {
        roots: ["<rootDir>/src", "<rootDir>/packages", "<rootDir>/test"],
        preset: 'ts-jest',
        moduleFileExtensions: ['vue', 'js', 'json', 'jsx', 'ts', 'tsx', 'node'],
        transform: {
          '^.+\\.vue$': '@vue/vue3-jest', // vue 文件用 vue-jest 转换
          '^.+\\.ts$': 'ts-jest', // ts 文件用 ts-jest 转换
          '^.+\\js$': 'babel-jest',
        },
        // 为了修复 Consider using the "jsdom" test environment. 问题
        testEnvironment: "jsdom",
        collectCoverage: true,
        /** 全局 alias */
        moduleNameMapper: {
          "^@/(.*)$": "<rootDir>/src/$1"
        },
        collectCoverageFrom: [
          "**/*.{js,vue}",
          "!**/node_modules/**",
          "!**/vendor/**",
          "!**/coverage/**",
          "!**/*.config.*",
          "!src/main.js"
        ],
        globals: {
          'ts-jest': {
            useESM: true,
            tsConfig: {
              target: 'ES2019'
            },
          }
        }
      };
    
    sonders
        1
    sonders  
    OP
       2023-02-21 18:20:54 +08:00
    已解决。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5530 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 06:38 · PVG 14:38 · LAX 23:38 · JFK 02:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.