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

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

Open
jhm-ciberman opened this issue Mar 15, 2018 · 7 comments

Comments

@jhm-ciberman
Copy link
Contributor

Hi!

This code works perfectly.

  • The coverage shows 100% for tl.loadFrom()
  • The test pass
return tl.loadFrom(dir).then(() => {
     throw new Error("load from not throw");
}, (e: Error) => {
     Expect(e.message).toContain("Error loading Template from");
});

This code does not work:

  • The method tl.loadFrom(dir) is NOT executed acording to the coverage
  • The test passes silently
Expect(() => tl.loadFrom(dir)).toThrowErrorAsync(Error, "Error loading Template from");

This code also does not work:

  • The method tl.loadFrom(dir) is NOT executed acording to the coverage
  • The test passes silently
Expect(() => tl.loadFrom(dir)).toThrowErrorAsync(Error, "IMPOSIBLE ERROR MESSAGE");
@pohy
Copy link

pohy commented Mar 19, 2018

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
Copy link
Member

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
Copy link
Contributor Author

jhm-ciberman commented Apr 21, 2018 via email

@jamesadarich
Copy link
Member

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

@jamesadarich
Copy link
Member

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

@pathurs
Copy link
Contributor

pathurs commented Jul 18, 2018

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants