https://cypress.io logo
My cy.task() command is returning null when it sho...
# i-need-help
c
Copy code
cy.task('renameFile', file).then((result) => {
  const renamed_file = result;
cy.wrap(renamed_file).as('updated_file');
cy.log('@updated_file');
cy.log('renamed_file);
}
my cy.task() function, renameFile, simply takes a file path and name and renames it for me and then returns me the new file name as a String. In my test, i'm trying to retrieve that renamed file and THEN assign it to a variable I made in the test called 'renamed_file'. I tried to log updated_file and renamed_file to see if either one is going to be the new string but they both return Null! How can I fix my cy.task() to get it to return the text value instead of a null object? I know i'm missing something but I just can't put my finger on it.. Thanks
e
Hard to say without seeing what the
renameFile
task actually looks like.
c
I just added all the code. I seriously don't know what i'm doing wrong 😦. Thanks for your help.
c
So this is a classic thing people struggle with when it comes to the Async nature of Cypress. One way to get around this is to wrap and reference the value you need.
E.g.
Copy code
cy.wrap(uniqueFilename).as('uniqueFilename');
// ... some more code
cy.wrap(res).as('uniqueFilename);
// ... some more stuff
cy.get('@uniqueFilename').then((value) => cy.log(value));