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.throws RegExp not matching correctly #269

Closed
Jameskmonger opened this issue Mar 3, 2016 · 5 comments
Closed

t.throws RegExp not matching correctly #269

Jameskmonger opened this issue Mar 3, 2016 · 5 comments

Comments

@Jameskmonger
Copy link

I have two tests:

test('this should pass', t => {
    t.plan(1);

    t.throws(() => {
        throw new Error('This is my error message.This text here should still match.')
    }, /This is my error message./);
});

test('this should fail', t => {
    t.plan(1);

    t.throws(() => {
        throw new Error('This is my error message.This text here should now fail.');
    }, /^This is my error message.$/);
});

The first test passes, as expected, because I don't specify the start and end of the RegExp matcher using ^ and $. The second, however, should no longer match the whole error message and should fail. These two behave as expected.

When I add a third test, however:

test('this should however pass', t => {
    t.plan(1);

    t.throws(() => {
        throw new Error('This is my error message.');
    }, /^This is my error message.$/);
});

I expect this to pass, because the whole string matches the RegExp. The test fails with the following output:

# this should however pass
not ok 8 should throw
  ---
    operator: throws
    expected: |-
      '/^This is my error message.$/'
    actual: |-
      { [Error: This is my error message.] message: 'This is my error message.' }
    at: Test.<anonymous> (/home/james/Development/jenie/test/jenie/jenie.spec.js:67:7)
  ...
@ljharb
Copy link
Collaborator

ljharb commented Mar 6, 2016

@Jameskmonger This works with the $ anchor - but the reason it's not working with the ^ anchor is that with an Error object, the message isn't what's being tested - but rather, the string version of the Error instance itself - which includes the type.

so, in your case, it would be /^Error: This is my error message.$/ that passes.

@ljharb
Copy link
Collaborator

ljharb commented Mar 6, 2016

I've added some tests that demonstrate this, and will close the issue.

@ljharb
Copy link
Collaborator

ljharb commented Mar 6, 2016

(also, dupe of #182)

@Jameskmonger
Copy link
Author

Thanks @ljharb

@jtlapp
Copy link
Contributor

jtlapp commented Jul 16, 2016

This behavior really should be in the documentation so everyone isn't having to rediscover it. Right now all it says is "expected, if present, must be a RegExp or Function."

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

No branches or pull requests

3 participants