Search code examples
typescripttestingintegration-testinge2e-testingtestcafe

Run setup before each fixture using before


I would like to run a fixture or a set of testController methods before each fixture using testcafe's before function

fixture I want to run before each fixture extracted into a separate function

// testSetup.ts
export async function initialiseTestEnv(t: testController) {

    console.log("Initialise working env");
    
    const docNameInput =  Selector('input#documentName');
    const createBtn =  Selector('button.ui.primary.button');

    await t
        .useRole(roles.adminUser)
        .maximizeWindow()
        .navigateTo(testConfig.standardListPage)
        .typeText(docNameInput, testConfig.repoName)
        .click(createBtn);

And I would like to to run this before each fixture like:

// tests.ts
// Tests that rely on env init
fixture `Tests that rely on init event`

    .before(async t => {
        
            await initialiseTestEnv(t)
    })
    .requestHooks(logger)
    .beforeEach(async t => {
        cookies.setCookies;
        await t
            .useRole(roles.adminUser)
        })

    test('Test that relies on test env init', async t => {...}

The problem is that before does not have a test controller that I could parse to the init function. Is there another way maybe to run this init event before each fixture I run (I don't want to run in before each test, just before a fixture)


Solution

  • You can do that like so:

    let alreadyRun = false;
    fixture `Tests that rely on init event`
        .beforeEach(async t => {
            if (!alreadyRun) {
                await initialiseTestEnv(t);
                alreadyRun = true;
            }
        })
    

    I think this very question is posted somewhere on TestCafe GitHub page, so a better solution might be available in future versions. But you can easily achieve it using a variable and a beforeEach hook.