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

t.doesNotThrow() expected parameters seems to be ignored #553

Open
m-r-r opened this issue Apr 19, 2021 · 14 comments
Open

t.doesNotThrow() expected parameters seems to be ignored #553

m-r-r opened this issue Apr 19, 2021 · 14 comments

Comments

@m-r-r
Copy link

m-r-r commented Apr 19, 2021

Hello,

I read the code of the method t.doesNotThrow() and I don't understand the purpose of the expected parameter.

The documentation says :

expected, if present, limits what should not be thrown, and must be a RegExp or Function. The RegExp matches the string representation of the exception, as generated by err.toString(). For example, if you set expected to /user/, the test will fail only if the string representation of the exception contains the word user. Any other exception will result in a passed test. The Function is the exception thrown (e.g. Error).

So, if my understanding is correct, the following tests should not fail :

const test = require("tape");

test('should not fail', (t) => {
    t.doesNotThrow(
        () => { throw new Error("Foo bar") },
        TypeError,
        "the callback does not throw a TypeError"
    );
    t.end();
});

test('should not fail', (t) => {
    t.doesNotThrow(
        () => { throw new Error("Foo bar") },
        /baz/,
        "the callback does not throw 'baz'"
    );
    t.end();
});

But the current behavior of t.doesNotThrow() is to always fail if the callback throws an exception.

Should the method t.doesNotThrow() be updated to match the documentation, or is my understanding of the documentation incorrect ?

@ljharb
Copy link
Collaborator

ljharb commented Apr 19, 2021

node's assert seems to work the same way:

assert.doesNotThrow(() => { throw new Error("Foo bar") }, TypeError); // throws
assert.doesNotThrow(() => { throw new Error("Foo bar") }, /baz/); // thros

node's docs seem to reinforce this.

Thus, i think tape's docs may need to be updated to clarify how it works?

@marvin-martian

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@ljharb

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@ljharb

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@ljharb

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@ljharb

This comment has been minimized.

@marvin-martian

This comment has been minimized.

@marvin-martian

This comment has been minimized.

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

Successfully merging a pull request may close this issue.

3 participants