0

As suggested in this post , I tried the steps to create dynamic tests , but I see the actual test(test.getMochaTest()in my below implementation) not getting executed. What's that I'm missing here, the call on test.getMochaTest() does not get executed in the before block.

describe('Dynamic Tests for Mocha', async () => {
    let outcome ;
    before(async() => {
        await init().catch(() => console.error('Puppeteer environment initialization failed'));
        return collectTests().then(async(collectTests) => {
            console.info('4.Executing tests :');
            describe('Dynamic test execution', async() => {
                collectTests.forEach(async(test) => {
                    console.info(`\tModule under test : ${test.name}`);

        // impl. of test.getMochaTest() DOES NOT get executed. 

                    it(test.name, async() => outcome = await test.getMochaTest().catch(async () => {
                        console.error(`error while executing test:\t${test.name}`);
                    }));
                });
            }) ;
        });
    });

    after(async () => {
        console.info('5. Exiting tests...');
        await HelperUtils.delay(10000).then(async () => { await browser.close(); });
        console.log('executing after block');
    });

    it('placeholder',  async() =>  {
        await
        console.log('place holder hack - skip it');
    });

});

Array of tests is returned here :

async function collectTests():Promise<Array<ITest>> {
    console.info('3.Collecting tests to execute ...');
    testArray = new Array<ITest>();
    const login:ITest = new SLogin('Login Check');
    testArray.push(login);
    
    return testArray;
}

The below implementation of getMochaTest in SLogin -> does not get executed .

export default class SLogin extends BTest implements ITest {
    constructor(name: string) {
        super(name);
    }
    async getMochaTest():Promise<Mocha.Func> {
        return async () => {
            console.log('Running Login check');
            expect(true).to.equal(true);
        };
    }
}
Alferd Nobel
  • 3,185
  • 2
  • 30
  • 35

1 Answers1

1

It doesn't look like you're actually invoking the test.

Calling test.getMochaTest() only returns the async test function in a Promise, it doesn't execute it. So your catch block is catching errors while obtaining the function, not while executing it.

Breaking it out across multiple lines will hopefully make things clearer.

Here's what your code sample does. Notice it never executes the returned test function:

it(test.name, async () => {
    const testFn = await test.getMochaTest().catch(() => 
        console.error(`error while ***obtaining*** test: \t${test.name}`));

    // oops - testFn never gets called!
});

And here's a corrected version where the test actually gets called:

it(test.name, async () => {
    const testFn = await test.getMochaTest().catch(() => 
        console.error(`error while ***obtaining*** test: \t${test.name}`));

    const outcome = await testFn().catch(() => 
        console.error(`error while ***executing*** test: \t${test.name}`));
});

Note: I wrote it that way with await and catch() to better match the format of your code sample. However, it's worth pointing out that it mixes async/await and Promise syntax. More idiomatic would be to catch errors with a try/catch block when using async/await.

rob3c
  • 1,936
  • 1
  • 21
  • 20
  • @thanks for pointing that out . However it complains with `node:4164) UnhandledPromiseRejectionWarning: TypeError: testFn is not a function` – Alferd Nobel Feb 23 '21 at 05:28
  • => thanks ! that worked, fixed that by changing the declaration `const testFn` to `let testFn` before the describe. Also its good to use `for..of `instead of `forEach` loop , while executing test in sequence. – Alferd Nobel Feb 23 '21 at 06:08