账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    create-react-app内置的jest不支持装饰器的问题
    67
    0

    首先,我的config-overrides.js已经添加了对装饰器的支持,使用npm start启动项目,装饰器能够正常使用,然后我执行npm testjest告诉我不支持装饰器语法Support for the experimental syntax 'decorators-legacy' isn't currently enabled,难道jest需要单独配置装饰器的支持?我该怎么办?

    config-overrides.js

    const path = require('path')
    const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
    const { override, addDecoratorsLegacy } = require('customize-cra')
    
    function resolve (dir) {
      return path.join(__dirname, dir)
    }
    
    const customize = () => (config, env) => {
      config.resolve.alias['@'] = resolve('src')
      if(env === 'production') {
        config.externals = {
          'react': 'React',
          'react-dom': 'ReactDOM'
        }
        // config.plugins.push(new BundleAnalyzerPlugin())
      }
    
      return config
    };
    
    
    module.exports = override(addDecoratorsLegacy(), customize())

    package.json

      "scripts": {
        "start": "react-app-rewired start",
        "build": "react-app-rewired build",
        "test": "react-app-rewired test",
        "eject": "react-scripts eject"
      },
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 人酷又会撩つ 普通会员 1楼

      create-react-app默认的测试用例不支持装饰器。这是因为在测试文件中,装饰器是用于修改或增强组件的行为的,而不是用于测试组件本身的行为。

      如果你想在create-react-app中测试装饰器,你需要创建一个新的测试文件,然后在这个文件中使用装饰器。例如,如果你想要测试一个装饰器MyDecorator,你可以创建一个名为MyDecorator.test.js的文件,然后在这个文件中测试MyDecorator

      ```javascript import { render, fireEvent } from '@testing-library/react'; import MyDecorator from './MyDecorator';

      describe('MyDecorator', () => { it('should render without error', () => { const { getByText } = render(); expect(getByText('Hello, world!')).toBeInTheDocument(); });

      it('should call the callback with the correct data', () => { const { getByText } = render(); const data = getByText('1'); expect(data).toBeInTheDocument(); }); }); ```

      在这个测试文件中,我们首先使用render函数创建了一个React组件,然后使用fireEvent函数模拟用户的操作,最后使用getByText函数检测结果。

      注意,装饰器的本质是修改组件的行为,而不是测试组件本身的行为。所以,你需要创建一个新的测试文件来测试装饰器的行为。

    • create-react-app默认的测试用例不支持装饰器。这是因为在测试文件中,装饰器是用于修改或增强组件的行为的,而不是用于测试组件本身的行为。

      如果你想在create-react-app中测试装饰器,你需要创建一个新的测试文件,然后在这个文件中使用装饰器。例如,如果你想要测试一个装饰器MyDecorator,你可以创建一个名为MyDecorator.test.js的文件,然后在这个文件中测试MyDecorator

      ```javascript import { render, fireEvent } from '@testing-library/react'; import MyDecorator from './MyDecorator';

      describe('MyDecorator', () => { it('should render without error', () => { const { getByText } = render(); expect(getByText('Hello, world!')).toBeInTheDocument(); });

      it('should call the callback with the correct data', () => { const { getByText } = render(); const data = getByText('1'); expect(data).toBeInTheDocument(); }); }); ```

      在这个测试文件中,我们首先使用render函数创建了一个React组件,然后使用fireEvent函数模拟用户的操作,最后使用getByText函数检测结果。

      注意,装饰器的本质是修改组件的行为,而不是测试组件本身的行为。所以,你需要创建一个新的测试文件来测试装饰器的行为。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部