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

StackTrace is cut when nesting matchers #4515

Closed
denniske opened this issue Sep 20, 2017 · 3 comments · Fixed by #4516
Closed

StackTrace is cut when nesting matchers #4515

denniske opened this issue Sep 20, 2017 · 3 comments · Fixed by #4516

Comments

@denniske
Copy link
Contributor

denniske commented Sep 20, 2017

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

Minimal Repository:
https://github.com/denniske/jest-stacktrace-demo

// greeter.test.js

expect.extend({
    toHaveBeenCalledWithSome: function (received, argument) {
        var a = received.calls.allArgs()[0][0];
        argument(a);
        return {
            message: function () { return ""; },
            pass: true
        };
    }
});

var greeter = {
    welcome: function (name) {
        var action = {
            message: "Hi " + name
        };
        console.log(action);
    }
};

describe("greeter", function () {
    it("'welcome' creates action with welcome message", function () {
        spyOn(console, "log");
        var name = "John";
        greeter.welcome(name);
        expect(console.log).toHaveBeenCalled();
        expect(console.log).toHaveBeenCalledWithSome(function (action) {
            expect(action).toBeTruthy();
            expect(action.message).toContain("Welcome");
            expect(action.message).toContain(name);
            expect(action.message).toBe("Welcome " + name);
        });
    });
});

yarn test will produce the following:

 FAIL  .\greeter.test.js
  ● greeter › 'welcome' creates action with welcome message

    expect(string).toContain(value)

    Expected string:
      "Hi John"
    To contain value:
      "Welcome"

      at Object.<anonymous> (greeter.test.js:25:29)

  greeter
    × 'welcome' creates action with welcome message (7ms)

The error does not occur in greeter.test.js:25, but in greeter.test.js:27.

The problem is that I am using expect statements inside the callback of a custom matcher toHaveBeenCalledWithSome.

This causes the stacktrace to be cut in the outer matcher toHaveBeenCalledWithSome here:
https://github.com/facebook/jest/blob/a397abaf9f08e691f8739899819fc4da41c1e476/packages/expect/src/index.js#L204

What is the expected behavior?

Do not cut the stacktrace such that error is reported correctly at greeter.test.js:27.

I would suggest to only cur stacktrace when error is no JestAssertionError:

if(!(error instanceof JestAssertionError)) {
    Error.captureStackTrace(error, throwingMatcher);
}

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

No jest config, versions are:

Jest - 21.1.0,
Node - 7.10.0
npm - 4.6.1
OS - Windows 10

@cpojer
Copy link
Member

cpojer commented Sep 20, 2017

Feel free to send a PR including tests :)

@denniske
Copy link
Contributor Author

Created pull request with tests. Can somebody review?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants