V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
sonders
V2EX  ›  问与答

Jest 测试 Vue 组件报错问题

  •  
  •   sonders · Feb 21, 2023 · 616 views
    This topic created in 1167 days ago, the information mentioned may be changed or developed.

    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
       Feb 21, 2023
    已解决。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2695 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 13:39 · PVG 21:39 · LAX 06:39 · JFK 09:39
    ♥ Do have faith in what you're doing.