Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log specs in separate files doesn't work #124

Closed
mikhaillazko opened this issue Oct 20, 2021 · 11 comments
Closed

Log specs in separate files doesn't work #124

mikhaillazko opened this issue Oct 20, 2021 · 11 comments

Comments

@mikhaillazko
Copy link

Hi. I have a problem with plugin setup to write logs into separate file.
My configuration. Version of packages:

  "cypress": "^8.6.0",
  "cypress-fail-fast": "^3.0.0",
  "cypress-terminal-report": "^3.3.4",

cypress.json

{
    "projectId": "**",
    "baseUrl": "http://localhost:3000",
    "integrationFolder": "cypress/tests",
    "testFiles": ["**/*.spec.ts"],
    "nodeVersion": "system",
    "env": {
        "apiUrl": "http://localhost:7787/api",
        "FAIL_FAST_STRATEGY": "spec"
    }
}

plugin/index.js

    const options = {
        outputRoot: config.projectRoot + '/cypress/logs/',
        printLogsToFile: 'always',
        specRoot: path.relative(config.fileServerFolder, config.integrationFolder),
        outputTarget: {
            'cypress-logs|json': 'json',
        },
        logToFilesOnAfterRun: true,
    }
    require('cypress-terminal-report/src/installLogsPrinter')(on, options)

support/index.ts

require('cypress-terminal-report/src/installLogsCollector')({
    xhr: { printRequestData: true },
});

Example name and path of spec test
cypress/tests/signin.spec.ts

As result i see file into cypress folder undefined.json with content

{
  "undefined": {
    "Sign in cases -> Existing user with correct credentials": [
      {
        "type": "cy:command",
        "severity": "success",
        "message": "visit\t/auth"
      },
      {
        "type": "cy:command",
        "severity": "success",
        "message": "get\t[data-test=signin-username]"
      }
    ]
  }
}

What am I doing wrong?

@archfz
Copy link
Owner

archfz commented Oct 27, 2021

Hello. I have tried to replicate the issue on this branch https://github.com/archfz/cypress-terminal-report-demo/pull/2/files but could not reproduce. I need more information.

That undefined means that the plugin somehow cannot determine the spec name. But I am really unsure how that is possible.

@mikhaillazko
Copy link
Author

mikhaillazko commented Nov 3, 2021

Thanks for attempt. Do have any ideas what the additional information can help to detect problem?
I'm using also next plugins that i skipped in first comment

@cypress/code-coverage/task
@cypress/webpack-preprocessor
@archfz
Copy link
Owner

archfz commented Nov 3, 2021

Unfortunately not really. You can fork the branch i posted above and try to reproduce on that. Or you could also debug this yourself in your own project, if you know how to use a debugger.

@robfreundlich
Copy link

I'm having the same problem. I inserted some console.log() statements into the [CONSTANTS.TASK_NAME]: function (data) function to show the contents of data. One is at the beginning, and one is just before calling collectTestLogs(). It's a simple "for each property, show the name and the value" loop. Here's what I got:

===== In the task function
===== data.test: Quick Test -> true assertion
===== data.messages: .... lots of messages ....
===== data.state: passed
===== data.level: 1

===== In the task function, about to call collectTestLogs
===== data.test: Quick Test -> true assertion
===== data.messages: .... lots of messages ....
===== data.state: passed
===== data.level: 1

Notice that there's no spec property in data. Could this be a versioning problem of some kind, where Cypress isn't providing the information you're expecting? I'm on Cypress 7.2.0.

@archfz
Copy link
Owner

archfz commented Dec 10, 2021

Can you try logging before this line

let spec = (mochaRunnable.parent.invocationDetails || mochaRunnable.invocationDetails).relativeFile;

These logs would appear only in browser console while in gui mode, as its on support side.

@robfreundlich
Copy link

Both mochaRunnable.parent.invocationDetails and mochaRunnable.invocationDetails are defined. However, relativeFile is present and set to undefined on both of them. In my super-simple test case (one describe() containing one it()), they look like this:

  "mochaRunnable.parent.invocationDetails": {
    "function": "Object../cypress/integration/expressit/ace/e2e/QuickTest.spec.ts",
    "absoluteFile": undefined,
    "fileUrl": "http://localhost:8765/__cypress/tests?p=cypress\\integration\\expressit\\ace\\e2e\\QuickTest.spec.ts",
    "originalFile": "http://localhost:8765/__cypress/tests?p=cypress\\integration\\expressit\\ace\\e2e\\QuickTest.spec.ts",
    "line": 94,
    "column": 2,
    "relativeFile": undefined,
    "whitespace": "    "
  },
  "mochaRunnable.invocationDetails": {
    "function": "Suite.eval",
    "absoluteFile": undefined,
    "fileUrl": "http://localhost:8765/__cypress/tests?p=cypress\\integration\\expressit\\ace\\e2e\\QuickTest.spec.ts",
    "originalFile": "http://localhost:8765/__cypress/tests?p=cypress\\integration\\expressit\\ace\\e2e\\QuickTest.spec.ts",
    "line": 95,
    "column": 6,
    "relativeFile": undefined,
    "whitespace": "    "
  }

I've tried this with enableExtendedCollector set to both true and false, and saw the same results in both LogCollectSimpleControl.js and LogCollectExtendedControl.js

My config looks like this:

(plugins)

  installLogsPrinter(on, {
    // Values: "onFail" | "always" | "never"
    printLogsToConsole: "always",
    collectTestLogs: (context /* {spec, test, state} */,
                      logs    /* {type, message, severity}[] */) => {
      console.log(`===== End of logs for ${context.test}`);
      console.log(`=====          State: ${context.state}`);
    },
    // Values: "onFail" | "always" | "never"
    printLogsToFile: "always",
    outputRoot: config.projectRoot + "/logs/",
    // Used to trim the base path of specs and reduce nesting in the
    // generated output directory.
    specRoot: path.relative(config.projectRoot, config.integrationFolder),
    // Format of these are either:
    //  "{filename}": "format (txt or json)"      e.g. "massive_console_log.txt": "txt"
    //  "{directory}|{extension}": "format (txt or json)"   e.g. "console-logs|txt": "txt"
    outputTarget: {
      "logs|txt": "txt",
      "logs|json": "json"
    }
  });

(support)

installLogsCollector({
                       enableExtendedCollector: false,
                       debug: true
                     });
@archfz
Copy link
Owner

archfz commented Dec 10, 2021

Well. That object is not controlled by us. That is part of mocha and or cypress. So I am really not sure why those props are not set.

You could attempt to update cypress, see if it fixes, maybe mocha if you have it installed. Other than that you could try to find another place to take that information from and put a PR.

@robfreundlich
Copy link

Thanks, that's good information. I know we're not current on cypress, and I'm not sure about mocha. I'll see what I can figure out. Unfortunately, my team is one of many sharing common infrastructure, so even if I do discover that we need to upgrade one or both of them, it's going to be a hassle. sigh.

@archfz
Copy link
Owner

archfz commented Dec 14, 2021

Found a fallback. Basically we can use the fileUrl. We just need to sanitize it. It's not very stable and cypress could easily break it. But let's see if this helps you. Will do a release soon with this.

@archfz
Copy link
Owner

archfz commented Dec 14, 2021

Released in 3.4.1. Please test and see if it works.

@robfreundlich
Copy link

Success! Thanks very much!

@archfz archfz closed this as completed Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants