jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.21k stars 6.46k forks source link

[Feature request] Could see the original line of assert statement #3715

Closed Albert-Gao closed 6 years ago

Albert-Gao commented 7 years ago

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

What is the current behavior?

test('Should return jwt after successfully registration', async () => {
        await removeTestUser();
        const response = await request(app).post(signupAPI).send(testUser);
        expect(response.statusCode).toBe(200);
        expect(response.body).not.toHaveProperty('error');
        expect(response.body).toHaveProperty('user');
        expect(response.body.user.name).toBeDefined();
        expect(response.body.user.email).toBeDefined();
        expect(response.body.user.token).toBeDefined();
        await removeTestUser();
    });

I use supertest + jest to test my express.js application, all use the latest version. The test runs fine. The problem is, if there is anything wrong, the output would be:

 ● [API Test] /api/auth/register › Should return jwt after successfully registration

    expect(received).toBeDefined()

    Expected value to be defined, instead received
      undefined

      at _callee8$ (tests/backend/integration/signup.test.js:98:57)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)

Some situation like this, If the value is undefined, And I happen to have multiple same assert, There is no way to see the reason in the bash, I need to track back to the file to see which line threw the error. Yes, I should track back, but wouldn't it better to show a result like below:

What is the expected behavior?

 ● [API Test] /api/auth/register › Should return jwt after successfully registration

    expect(response.body.user.name).toBeDefined()

    Expected response.body.user.name to be defined, instead received
      undefined

      at _callee8$ (tests/backend/integration/signup.test.js:98:57)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)

Thanks :) Love jest.

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

{
  "verbose": true,
  "rootDir": "../",
  "testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/tests/acceptance/", "<rootDir>/tests/coverageReport/", "<rootDir>/dist/"],
  "collectCoverage": false,
  "coverageDirectory": "tests/coverageReport",
  "collectCoverageFrom" : ["**/src/**"],
  "coveragePathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/configs/", "<rootDir>/src/config/appconfig.json", "<rootDir>/dist/"],
  "coverageReporters": ["text", "text-summary"]
}
thymikee commented 7 years ago

Jest now only has access to a value carried by response.body.user.name, that's why it shows generic message. Wonder how we could make it better.

kimjoar commented 7 years ago

Maybe it's possible to do something along the lines of Ava, which displays the code around the failed assertion?

ava

See e.g. https://github.com/avajs/ava#magic-assert and https://github.com/avajs/ava#snapshot-testing

SimenB commented 6 years ago

EDIT: I misunderstood the issue - you want more info on what failed without having to look up the code manually, that's tracked in https://github.com/facebook/jest/issues/3415

github-actions[bot] commented 3 years ago

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.