You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Vitest测试集成Framer Motion的React-ts应用?

Vitest渲染含Framer-Motion的React组件时出现tslib导出错误

环境配置

Vite + Yarn + React-ts + Framer-Motion

问题描述

项目原本运行正常,Framer-Motion功能也能正常工作,但在给App组件添加简单的<motion.button>后,使用Vitest渲染App组件时出现以下错误:

FAIL  tests/unit/components.spec.tsx [ tests/unit/components.spec.tsx ]
SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'tslib';
const { __assign } = pkg;

添加Framer-Motion组件前,测试可以正常运行,测试代码如下:

import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import App from '../../src/App';

describe('App.vue', () => {
  it('should render the main input', () => {
    render(<App />);
    expect(screen.getByTestId('mainInput')).toBeDefined();
  });
});

内容的提问来源于stack exchange,提问作者Duarte Elvas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 00:45:11