[英]create-react-app testing “React is not defined”
我使用react-scripts@2.1.8
與typescript@3.3.3
與jest@.23.6.0
和運行我的測試中,他們失敗“的反應沒有定義”。
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:
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行似乎有時會使測試通過。 這很奇怪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.