首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >create-react-app测试“未定义React”

create-react-app测试“未定义React”
EN

Stack Overflow用户
提问于 2019-04-02 20:39:44
回答 1查看 1.4K关注 0票数 0

我正在使用带有jest@.23.6.0react-scripts@2.1.8typescript@3.3.3,并运行我的测试,它们失败了,并显示"React is not defined“。

代码语言:javascript
复制
 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)

Button.test.tsx:

代码语言:javascript
复制
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导入,就像将其从第1行移动到第3行,有时似乎会使测试通过。这很奇怪。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-22 19:18:44

这是this issue中讨论和解释的jsx杂注和情感10的一个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55475022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档