首页 > 解决方案 > React 测试未运行 - 运行时自动时无法设置 pragma 和 pragmaFrag

问题描述

直到最近,我的测试都运行良好,除了顶级App组件冒烟测试外,它们都仍然可以正常运行并通过。

测试代码:

import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render, waitFor } from '@testing-library/react';
import { ToastProvider } from 'react-toast-notifications'
import App from './App';

it('renders without crashing', async () => {
  const { getByText } = render(
    <MemoryRouter>
      <ToastProvider>
        <App />
      </ToastProvider>
    </MemoryRouter>
  );
  await waitFor(() => {
    expect(getByText("You'll need to sign in to use this feature.")).toBeInTheDocument();
  });
});

错误:

Test suite failed to run

    SyntaxError: /Users/druserkes/Desktop/zephyrx/zephyrx-dr-dashboard/node_modules/react-toast-notifications/dist/ToastContainer.js: pragma and pragmaFrag cannot be set when runtime is automatic.
    > 1 | 'use strict';
        | ^
      2 |
      3 | Object.defineProperty(exports, "__esModule", {
      4 |   value: true

      at File.buildCodeFrameError (node_modules/react-scripts/node_modules/@babel/core/lib/transformation/file/file.js:250:12)
      at NodePath.buildCodeFrameError (node_modules/@babel/traverse/lib/path/index.js:138:21)
      at PluginPass.enter (node_modules/babel-preset-react-app/node_modules/@babel/plugin-transform-react-jsx/lib/create-plugin.js:163:28)
      at newFn (node_modules/@babel/traverse/lib/visitors.js:175:21)
      at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:55:20)
      at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:42:17)
      at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:92:31)
      at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:116:16)
      at TraversalContext.visitSingle (node_modules/@babel/traverse/lib/context.js:85:19)
      at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:144:19)

注释掉'use strict'文件中的行node_modules不会改变任何东西。

任何帮助将不胜感激。

标签: reactjstestingjestjsreact-testing-librarypragma

解决方案


推荐阅读