代码之家  ›  专栏  ›  技术社区  ›  bitten

create react app testing“未定义react”

  •  0
  • bitten  · 技术社区  · 6 年前

    我正在使用 react-scripts@2.1.8 具有 typescript@3.3.3 具有 jest@.23.6.0 在运行我的测试时,它们以“React is not defined”失败。

     FAIL  src/components/Button/test.tsx
      ● <Button> › renders text correctly
    
        ReferenceError: React is not defined
    
           6 |     it('renders text correctly', () => {
           7 |         const text = 'hello there i am a test'
        >  8 |         const { getByText } = customRender(<Button>{text}</Button>)
             |                                            ^
           9 |         expect(getByText(/hello there i am a test/)).toBeTruthy()
          10 |     })
          11 |     it('matches the snapshot', () => {
    
          at Object.it (src/components/Button/test.tsx:8:44)
    
      ● <Button> › matches the snapshot
    
        ReferenceError: React is not defined
    
          10 |     })
          11 |     it('matches the snapshot', () => {
        > 12 |         const { container } = customRender(<Button />)
             |                                            ^
          13 |         expect(container.firstChild).toMatchSnapshot()
          14 |     })
          15 | })
    
          at Object.it (src/components/Button/test.tsx:12:44)
    

    按钮.test.tsx:

    import React from 'react'
    import Button from '.'
    import { customRender } from '../../test-utils'
    
    describe('<Button>', () => {
        it('renders text correctly', () => {
            const text = 'hello there i am a test'
            const { getByText } = customRender(<Button>{text}</Button>)
            expect(getByText(/hello there i am a test/)).toBeTruthy()
        })
        it('matches the snapshot', () => {
            const { container } = customRender(<Button />)
            expect(container.firstChild).toMatchSnapshot()
        })
    })
    

    还可以使用React import,比如将它从第1行移动到第3行,有时似乎可以使测试通过。这很奇怪。

    0 回复  |  直到 6 年前
        1
  •  1
  •   bitten    6 年前

    这是jsx pragma和emotion 10的一个问题,如中所讨论和解释的 this issue .