'Cypress doesn't take screenshots in CLI mode

I'm trying to take a simple screenshot using Cypress, however, it only works in the Cypress GUI (cypress open).
Whenever I run cypress run, it shows that the test succeeded but there's not screenshot saved.

This my code:

describe('Snapshot', () => 
    it.only('Take snapshot', async () => {
       cy.visit("http://www.google.com");
       cy.screenshot();
    });
});


Solution 1:[1]

Actually, even in the GUI you may get inconsistent behaviour with the code you've provided: you should try to refactor your tests to avoid the use of async.

Cypress already does a lot of async stuff itself. See this answer for more information about how cypress uses promises, and avoiding async, as well as a few things you could try if you really want to use async.

Your code will successfully create screenshots under cypress run if you remove the axync keyword:

describe('Snapshot', () => {
    it.only('Take snapshot', () => {
       cy.visit("http://www.google.com");
       cy.screenshot();
    });
});

Note: I also added the missing opening brace { at the end of the first line. Since you say this code ran for you in the GUI, I'm assuming the brace was not the issue but was simply lost while posting.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1