alsatian-test / alsatian

TypeScript testing framework with test cases
251 stars 35 forks source link

Expect().toThrowErrorAsync() not executing function and passing silently. #458

Open jhm-ciberman opened 6 years ago

jhm-ciberman commented 6 years ago

Hi!

This code works perfectly.

This code does not work:

This code also does not work:

pohy commented 6 years ago

I am using the async Expect the following way, and it works flawlessly.

@AsyncTest('Test name')
public async originalDoesNotExist() {
    await Expect(() => asyncMethod()).toThrowErrorAsync(Error, 'Original does not exist');
}
jamesadarich commented 6 years ago

Hey @jhm-ciberman did @pohy 's suggestion work for you? toThrowErrorAsync is an async function and must be awaited. I think we could potentially add a safeguard here in future when we handle stuff like multiple failures but we'd need some rework to do so.

jhm-ciberman commented 6 years ago

Sorry, I didn't touch my computer the last month due to personal problems. I will try it later. Maybe you can document that feature. I wasn't aware of how that function should be used. Thanks!

El vie., 20 de abr. de 2018 10:24 PM, James Adarich < notifications@github.com> escribió:

Hey @jhm-ciberman https://github.com/jhm-ciberman did @pohy https://github.com/pohy 's suggestion work for you? toThrowErrorAsync is an async function and must be awaited. I think we could potentially add a safeguard here in future when we handle stuff like multiple failures but we'd need some rework to do so.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/alsatian-test/alsatian/issues/458#issuecomment-383257228, or mute the thread https://github.com/notifications/unsubscribe-auth/AHnkf6ufumAIGgsmn2xaOfUZ7aDuAiDJks5tqopcgaJpZM4Ssyf1 .

jamesadarich commented 6 years ago

@jhm-ciberman sorry to hear that hope all is well. Yes great point we will add some documentation around that :)

jamesadarich commented 6 years ago

Updated https://github.com/alsatian-test/alsatian/wiki/Matchers :) all good?

pathurs commented 5 years ago

Hey @jamesadarich,

Seeing as asynchronous JavaScript is on the rise, I think it might be a good idea to handle these problems by waiting for the promise ourselves.

Proof Of Concept:

let expectations = [];

function Expect(func) {
    const prom = Promise.resolve(func());

    return {
        toThrowErrorAsync: function (error) {
            expectations.push({ promise: prom, toThrowErrorAsync: error });
        }
    };
}

async function Test(test) {
    expectations = [];

    test();

    try {
        const result = await Promise.all(expectations.map(expectation => {
            if ('toThrowErrorAsync' in expectation) {
                return expectation.promise.catch(err => {
                    if (err !== expectation.toThrowErrorAsync) {
                        throw new Error(`Expected to throw ${expectation.toThrowErrorAsync}`);
                    } else {
                        // Expectation was right :)
                    }
                });
            } else {
                // Handle other conditions
            }
        }));

        console.log(result);
    }
    catch (err) {
        console.error(err);
    }
}

Test(async function() {
    const error =  new Error('MyError');

    Expect(() => Promise.reject(error)).toThrowErrorAsync(error);
    Expect(() => Promise.reject(error)).toThrowErrorAsync(new Error('OtherError'));
});

Output:

Error: Expected to throw Error: OtherError
    at expectation.promise.catch.err (D:\Github\alsatian\foo.js:23:31)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
    at startup (internal/bootstrap/node.js:238:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
jamesadarich commented 5 years ago

Yep agree this should be handled as discussed in #455 👍