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

Access to testRunInfo in testRun before and after hooks #8106

Closed
codambro opened this issue Dec 19, 2023 · 6 comments
Closed

Access to testRunInfo in testRun before and after hooks #8106

codambro opened this issue Dec 19, 2023 · 6 comments
Labels
TYPE: enhancement The accepted proposal for future implementation.

Comments

@codambro
Copy link

codambro commented Dec 19, 2023

What is your Scenario?

I would like to access the testRun object within the testRun before/after hooks. My specific use case is I have some setup that only needs to be run if certain test fixtures are executed, and I need the testRun info to be able to determine what fixtures are run.

What are you suggesting?

the fixture.before global hook takes two arguments:
async (ctx, fixtureInfo) => {}

But the testRun.before global hook only takes 1:
async ctx => {}

I believe the testRun before hook should be the same as the fixture level hooks:
async (ctx, testRunInfo) => {}

What alternatives have you considered?

Parsing command line args

Additional context

Realize I don't know exactly where in testRunInfo the tests would be or if that info even exists

@codambro codambro added the TYPE: enhancement The accepted proposal for future implementation. label Dec 19, 2023
@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Dec 19, 2023
@PavelMor25
Copy link
Collaborator

Hello @codambro,

What information do you expect to retrieve from testRunInfo?

Please describe your usage scenario in greater detail. There might already be an existing functionality or workaround in TestCafe that aligns with your scenario.

@PavelMor25 PavelMor25 added STATE: Need clarification An issue lacks information for further research. and removed STATE: Need response An issue that requires a response or attention from the team. labels Dec 21, 2023
@codambro
Copy link
Author

Hello. Yeah I think I realized testRunInfo may not even have what I need.

I'm defining a global testRun.before hook that sets something up for specific tests to use. But it takes a long time to run. So I only want to set it up if one or more of the specific tests is being executed in the testRun.

Ideally in testRun.before I can determine which exact tests are part of the run, including with respect to .meta filtering, along with .skip and .only modifiers.

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Dec 21, 2023
@github-actions github-actions bot removed the STATE: Need clarification An issue lacks information for further research. label Dec 21, 2023
@PavelMor25
Copy link
Collaborator

Perhaps, this workaround can solve the issue or allow you to create your own solution:

module.exports = {
    hooks: {
        test: {
            before: async t => {
                const testfile = t.testRun.test.testFile;
            }
        },
    },
};

From the testFile, you can access the following information:

  • .collectedTests: a list of tests from the current test file. From each list item, you can get information such as meta, name, only, and skip;
  • .currentFixture: information about the current fixture with the same properties as the test item from collectedTests.

However, It's not part of our public API and can be changed at any time. Please use it with caution.

@PavelMor25 PavelMor25 removed the STATE: Need response An issue that requires a response or attention from the team. label Dec 25, 2023
@codambro
Copy link
Author

Thanks for the suggestion. Unfortunately it wouldn't help since I need the information at the start of the testRun and not at the start of each individual fixture.

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Dec 25, 2023
@PavelMor25
Copy link
Collaborator

We can consider your enhancement request, but we need to understand if this enhancement has practical application. Please describe how it should work and provide a detailed example of how this enhancement would be applied in practice.

@PavelMor25 PavelMor25 added STATE: Need clarification An issue lacks information for further research. and removed STATE: Need response An issue that requires a response or attention from the team. labels Dec 27, 2023
@codambro
Copy link
Author

codambro commented Jan 5, 2024

Found a sufficient workaround. Sharing in case others have similar issues. I'm using the createRunner API with a filter function. In there, I'm tracking what tests would run.

export const testRunTests = [];

async function filterFn(testName, fixtureName, fixturePath, testMeta, fixtureMeta) {
  // Some logic to return true if test should run, false otherwise
  // ...
  
  // Before returning true, add test to testRun list
  testRunList.push(`${fixtureName}.${testName}`);
  return true;
}

const testcafe = await createTestCafe();
const runner = testcafe.createRunner();
runner.filter(filterFn);

Then my testRun hook functions can call that in testcaferc.js:

const { testRunTests } = require("../testcafeRunner");

module.exports = {
  hooks: {
    testRun: {
      before: async ctx => {
        if (testRunTests.includes("someFixtureName.someTestName")) {
          // perform slow, test specific setup
        }
      }
    }
  }
}

I believe the .only and .skip conditions are checked before applying the .filter function. So they are properly considered here as well

@codambro codambro closed this as completed Jan 5, 2024
@need-response-app need-response-app bot added STATE: Need response An issue that requires a response or attention from the team. and removed STATE: Need response An issue that requires a response or attention from the team. labels Jan 5, 2024
@github-actions github-actions bot removed the STATE: Need clarification An issue lacks information for further research. label Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TYPE: enhancement The accepted proposal for future implementation.
Projects
None yet
Development

No branches or pull requests

2 participants