javascript 使用RedwoodJS和Jest,“语法错误:Jest遇到意外令牌”

toiithl6  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(191)

我想为我的Redwoodjs项目写一些测试。我正在使用FullCalendar.io包,当jest遇到这个包的import语句时,它会抱怨。上面是这么写的:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

我已经尝试了各种解决方案,但问题仍然存在。添加bable.config.js没有成功。
package.json

{
  "private": true,
  "workspaces": {
    "packages": [
      "api",
      "web",
      "packages/*"
    ]
  },
  "devDependencies": {
    "@redwoodjs/auth-dbauth-setup": "4.1.4",
    "@redwoodjs/core": "4.1.4"
  },
  "eslintConfig": {
    "extends": "@redwoodjs/eslint-config",
    "root": true
  },
  "engines": {
    "node": ">=14.19 <=16.x",
    "yarn": ">=1.15"
  },
  "prisma": {
    "seed": "yarn rw exec seed"
  },
  "packageManager": "yarn@3.3.0",
  "dependencies": {
    "@fullcalendar/core": "^6.1.4",
    "@fullcalendar/daygrid": "^6.1.4",
    "@fullcalendar/interaction": "^6.1.4",
    "@fullcalendar/react": "^6.1.4",
    "@fullcalendar/timegrid": "^6.1.4",
    "react-calendar": "^4.0.0"
  }
}

jest.config.js

module.exports = {
  rootDir: '.',
  projects: ['<rootDir>/{*,!(node_modules)/**/}/jest.config.js'],
}

web/jest.config.js

const config = {
  rootDir: '../',
  preset: '@redwoodjs/testing/config/jest/web',
}

module.exports = config

它抱怨的文件:web\src\components\AppointmentList\AppointmentList.js

import { Box } from '@chakra-ui/react'
import interactionPlugin from '@fullcalendar/interaction'
import FullCalendar from '@fullcalendar/react'
import timeGridPlugin from '@fullcalendar/timegrid'

const AppointmentList = () => {
  const arr = [
    //add appointment array outside the return statement for better integration
    //The events prop is an array of objects that represent the events to display on the calendar
    {
      id: 1,
      title: 'event 1',
      start: '2023-02-26T10:00:00',
      end: '2023-02-26T12:00:00',
    },
    {
      id: 1,
      title: 'event 2',
      start: '2023-02-26T14:00:00',
      end: '2023-02-26T14:30:00',
    },
  ]
  return (
    <Box>
      <FullCalendar
        plugins={[timeGridPlugin, interactionPlugin]}
        initialView="timeGridDay"
        initialDate="2023-02-26" //SHOULD BE A PROP FROM TASK VIEW GROUP
        slotMinTime="06:00:00"
        slotMaxTime="21:00:00"
        slotDuration="00:30:00"
        nowIndicator
        headerToolbar={{
          start: 'title',
          center: '',
          end: '',
        }}
        const
        events={arr}
      />
    </Box>
  )
}

export default AppointmentList

web\src\components\AppointmentList\AppointmentList.test.js

import { render } from '@redwoodjs/testing/web'

import { render } from '@redwoodjs/testing/web'

import AppointmentList from './AppointmentList'

//   Improve this test with help from the Redwood Testing Doc:
//    https://redwoodjs.com/docs/testing#testing-components

describe('AppointmentList', () => {
  it('renders successfully', () => {
    expect(() => {
      render(<AppointmentList />)
    }).not.toThrow()
  })
  it('renders a FullCalendar component with the correct props', () => {
    const { getByTestId } = render(<AppointmentList />)
    const calendar = getByTestId('appointment-calendar')

    expect(calendar).toBeInTheDocument()
    expect(calendar).toBeInstanceOf(FullCalendar)

    expect(calendar.props.plugins).toEqual(
      expect.arrayContaining(['@fullcalendar/timegrid', '@fullcalendar/interaction'])
    )
    expect(calendar.props.initialView).toBe('timeGridDay')
    expect(calendar.props.slotMinTime).toBe('06:00:00')
    expect(calendar.props.slotMaxTime).toBe('21:00:00')
    expect(calendar.props.slotDuration).toBe('00:30:00')
    expect(calendar.props.nowIndicator).toBe(true)
    expect(calendar.props.headerToolbar).toEqual({
      start: 'title',
      center: '',
      end: '',
    })
  })

  it('renders the correct events on the calendar', () => {
    const { getByText } = render(<AppointmentList />)
    const event1 = getByText('event 1')
    const event2 = getByText('event 2')

    expect(event1).toBeInTheDocument()
    expect(event2).toBeInTheDocument()
  })
})

尝试stackoverflow中的解决方案没有帮助。
我想让测试为AppointmentList组件工作。任何帮助将不胜感激…

lmvvr0a8

lmvvr0a81#

我也在RedwoodJS中使用@fullcalendar,遇到了同样的问题。我在fullcalendar GitHub repo上找到了this issue,虽然我不能说我完全理解这个解释,但这篇评论中的解决方案确实为我解决了这个问题。
将以下代码放入web工作区的jest.config.js文件中。

const config = {
  rootDir: '../',
  preset: '@redwoodjs/testing/config/jest/web',
  testEnvironment: 'jsdom',
  testEnvironmentOptions: {
    customExportConditions: [], // don't load "browser" field
  },
}

注意:我不能保证这没有其他副作用,但到目前为止,我的项目似乎一切正常。:)希望这对你有帮助!

相关问题