bigbite / wp-cypress

WordPress end to end testing with Cypress.io.
MIT License
86 stars 19 forks source link

Feature request - Write command output to `stdout` #113

Closed over-engineer closed 1 year ago

over-engineer commented 1 year ago

Is your feature request related to a problem? Please describe. WP Cypress includes commands like wp, seed, switchUser(), etc. We can run a command in an e2e spec, like this:

cy.wp('media import /path/to/media --porcelain');

which is going to yield an object (as described in cy.exec()) with the following properties:

However, due to WP Cypress using the ora spinner—which, by default, is using stderr as the stream to write the output to—the result of those commands will be written to stderr.

An example of a yielded object would look like this:

{
  code: 0,
  stderr: "- Running wp media import /path/to/media --porcelain\nwp-cypress \u001b[32m✔\u001b[39m 31",
  stdout: ""
}

I was expecting stdout to contain 31 (i.e. the attachment ID), but instead I got an empty string, since there was nothing written to stdout.

Describe the solution you'd like I'd like to be able to get the actual command output (i.e. 31 in the above example) by accessing the stdout property of the yielded object.

I'd expect the yielded object, in this case, to be:

{
  code: 0,
  stderr: "- Running wp media import /path/to/media --porcelain\nwp-cypress \u001b[32m✔\u001b[39m 31",
  stdout: "31"
}

I think that, for backwards compatibility reasons, the stderr should be kept as it is, since someone might be relying on that to get the result of a command.

Describe alternatives you've considered The value of the stderr property contains the command result—the 31, at the end of the string, is the attachment ID:

"- Running wp media import /path/to/media --porcelain\nwp-cypress \u001b[32m✔\u001b[39m 31",

It is possible to work around the current implementation, by getting the value of the stderr property and doing something like this:

cy.wp('media import /path/to/media --porcelain')
  .then((result) => {
    const attachmentID = result.stderr.split(' ').slice(-1)[0];
  });

That may work, but it's not ideal. It's highly dependent on the output of the WP-CLI command, looks confusing and is error-prone.

For clarity, the proposed behavior would look like this:

cy.wp('media import /path/to/media --porcelain')
  .then((result) => {
    const attachmentID = result.stdout;
  });

which, IMHO, is much clearer and more readable.

Additional context N/A