I'm trying to do a jest test in vanilla javascript that checks if a list element is being deleted after click() is being triggered. However I can't make it to work. I'm very new to jest and testing, this is actually my first test, so any help and suggestion is highly appreciated. What am I doing wrong? Is there a better way to test if the deleteTask() is being successfully triggered by click()?
This is the test.js file
test('check if clicking the delete button deletes listelement', () => {
document.body.innerHTML = `
<ul class="todolist">
<li>
<input type="checkbox">
<label>Pay rent</label>
<button type="button">Delete</button>
</li>
<li id="listelement">
<input type="checkbox">
<label>Go grocery shopping</label>
<button type="button" id="delete" onclick="deleteTask()">Delete</button>
</li>
</ul>
`;
// require('../tests/deletefunction.js');
var deleteTask=function(){
var listelement=document.getElementById('listelement');
var listelement=this.parentNode;
var ul=listelement.parentNode;
ul.removeChild(listelement);
}
var deleteButton=document.getElementById("delete").click();
const listelementTofind = screen.queryAllByText('listelement');
expect(listelementTofind).not.toBeInTheDocument();
});
When I run npm test I get the following errors: error screenshot
I've already tried to import '@testing-library/jest-dom/extend-expect', as I thought it was the solution for
'screen queryAllByText is not a function'
but it didn't work out. With regard to the first error, I also tried to create a file deletefunction.js,where I stored deleteTask(), (imported inside test.js with require) but it still gave me
'deleteTask is not defined'
I have already seen the answers given here for these problems but the test still fails. Thanks in advance for your help and patience, I'm an absolute beginner on testing.
Aucun commentaire:
Enregistrer un commentaire