Skip to main content
One-line solution
Source Link
Bassem
  • 4.2k
  • 4
  • 26
  • 56

Another solution: you could also use a try/catch block

expect.assertions(1) try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 

EDIT:

I found a one-line alternative:

expect(screen.queryByTestId("your-test-id")).not.toBeInTheDocument(); 

Another solution: you could also use a try/catch block

expect.assertions(1) try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 

Another solution: you could also use a try/catch block

expect.assertions(1) try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 

EDIT:

I found a one-line alternative:

expect(screen.queryByTestId("your-test-id")).not.toBeInTheDocument(); 
This will always pass the test until `expect.assertions(1)` is added
Source Link

Another solution: you could also use a try/catch block

expect.assertions(1) try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 

Another solution: you could also use a try/catch block

try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 

Another solution: you could also use a try/catch block

expect.assertions(1) try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); } 
Source Link
Bassem
  • 4.2k
  • 4
  • 26
  • 56

Another solution: you could also use a try/catch block

try { // if the element is found, the following expect will fail the test expect(getByTestId('your-test-id')).not.toBeVisible(); } catch (error) { // otherwise, the expect will throw, and the following expect will pass the test expect(true).toBeTruthy(); }