盡管在我的測驗中更改了字串和長度,但它總是通過。它記錄 console.log(false) 并通過測驗。
這是測驗
describe('newUsers', done => {
test('fetches the data from the API and correctly renders it', async () => {
render(<Input />)
setTimeout(async () => {
const items = await screen.findByPlaceholderText('Search...');
expect(items).toHaveLength(4);
done();
}, 100)
//screen.debug()
})
})
我要測驗的電話
const [Loading, setLoading] = useState(false)
const [name, setName] = useState('');
const [options, setOptions] = useState([])
const [data, setData] = useState(false)
useEffect(() => {
setLoading(true)
const newUsers = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/users')
.then((res) => res.json())
.then((data)=> {
setData(data)
});
};
newUsers();
}, [])
html
return (
<div>
<div className='flex justify-center mt-10'>
<label>Name</label>
<input id="input" className='w-96 h-16 border-1 shadow-lg rounded-lg' placeholder="Search..."/>
uj5u.com熱心網友回復:
發生這種情況是因為在測驗完成后呼叫了異步代碼。您可以查看jest docs以獲得完整的解釋。基本上,要修復它,您需要done在測驗函式中添加一個引數,而不是不使用任何引數。這樣,jest 將等到done被呼叫來完成測驗:
describe('newUsers', () => {
test('fetches the data from the API and correctly renders it', async (done) => {
render(<Input />)
setTimeout(async () => {
const items = await screen.findByPlaceholderText('Search...');
expect(items).toHaveLength(4);
done();
}, 100)
//screen.debug()
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/438727.html
上一篇:如何在django中測驗上傳檔案
