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

Error trace cryptic when a method reference not passed to afterEach #6789

Closed
anuragkapur opened this issue Jul 31, 2018 · 11 comments
Closed

Comments

@anuragkapur
Copy link

anuragkapur commented Jul 31, 2018

🐛 Bug Report

The error trace generated when a method reference is not passed as expected to afterEach is cryptic and hard to debug.

To Reproduce

import React from 'react';
import {cleanup} from 'react-testing-library';
import 'jest-dom/extend-expect';

afterEach(cleanup());

test("Hello World", () => {});
// several other tests but they they are not relevant to this example and have been excluded to keep the question simple

Expected behavior

An appropriate error message that suggests that the problem is in the afterEach line so that as a developer I can know what went wrong and fix it, without manually guessing which of my 100 lines of test code is problematic.

Link to repl or repo (highly encouraged)

https://repl.it/@anuragkapur/ProudDependablePhysics

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-6660U CPU @ 2.40GHz
  Binaries:
    Node: 8.2.0 - /usr/local/bin/node
    npm: 5.3.0 - /usr/local/bin/npm
@thymikee
Copy link
Collaborator

Would you like to contribute this change? We're happy to review once ready :)

@natealcedo
Copy link

@thymikee Hey! I'd love to take a look into this. Could you possibly point me in the right direction to get started? :)

@natealcedo
Copy link

natealcedo commented Aug 4, 2018

Hey @SimenB, thanks for pointing me in the right direction. From what I've observed so far, this extends to any of the hook functions. IE, beforeEach, afterEach, etc.

What I've tried so far is do a type check in jest/packages/jest-jasmine2/src/jasmine/Env.js, something like this.

  this.beforeEach = function(beforeEachFunction, timeout) {
      if (typeof beforeEachFunction !== 'function') {
        throw new Error(
          `Invalid argument ${beforeEachFunction}. It must be a callback function.`,
        );
      }
      currentDeclarationSuite.beforeEach({
        fn: beforeEachFunction,
        timeout() {
          return timeout || j$._DEFAULT_TIMEOUT_INTERVAL;
        },
      });
    };

So far in some cases where the inputs are a string or undefined, it works as expected. But when the input is a number or a boolean, it instead goes into jasmine_async and doesnt throw an error. What I mean by this is, in actual usage, is throws a type error TypeError: wrappedFn.call is not a function and in the test cases, it doesnt throw anything at all.

Having said that, how strict should we be with checking the argument being provided into the hook function since the behaviour differs based on arguments passed.

Here are some sample tests to test the Env.js changes.
On a side note: Is this the right way to test the hook functions?

describe('hooks error testing', () => {
  // Works 
  it('it throws error when a callback function is not passed in beforeEach', () => {
    const input = 'sdfdsf';
    expect(() => beforeEach(input)).toThrowError(
      `Invalid argument ${input}. It must be a callback function.`,
    );
  });

// Doest throw anything 
  it('it throws error when a callback function is not passed in beforeEach', () => {
    const input = 1;
    expect(() => beforeEach(input)).toThrowError(
      `Invalid argument ${input}. It must be a callback function.`,
    );
  });
});

Actual usage of the updated code

// Throws TypeError: wrappedFn.call is not a function
//  at Object.asyncFn (../jest/packages/jest-jasmine2/build/jasmine_async.js:63:37)

describe("test", () => {
  beforeEach(1);

  it("should work", () => {
    expect(2).toEqual(2);
  });
});

Would love your input on how we should proceed!

@SimenB
Copy link
Member

SimenB commented Aug 30, 2018

@natealcedo sorry I never got back to you. However, I think this has been solved by #6917, and this I'm closing this. Do you agree?

@SimenB SimenB closed this as completed Aug 30, 2018
@natealcedo
Copy link

Hey @SimenB, thanks for reaching out. I actually don't think this is solved satisfactorily enough. I had similar looking code as #6917 however jest will only throw the helpful error message if you pass in a string in the hook functions. Numbers, symbols, arrays. I didn't proceed further as I didn't have enough context. Fixing the code just in jest-jasmine didn't fix it and I didn't know where to proceed next.

I can replicate the behaviour where this will not work.

jest/packages/jest-jasmine2/src/__tests__/hooks_error.test.js

describe('hooks error throwing', () => {
  test.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
    '%s throws an error when the first argument is not a function',
    fn => {
      expect(() => {
        // This doesnt work for anything other than strings. (numbers, symbols, etc)
		// Throws TypeError: wrappedFn.call is not a function
        global[fn](2);
      }).toThrowError(
        'Invalid first argument, param. It must be a callback function.',
      );
    },
  );
}); 

Having said that, its not really a big bug. More like a quality of life improvement. I defer to your judgement here as to whether or not you believe this warrants further investigation. :)

@SimenB
Copy link
Member

SimenB commented Aug 31, 2018

Good point! It's fixed for circus, but not jasmine.

Moving the checks into jasmine-light.js instead of Env.js should fix it. Wanna send a PR doing that?

@SimenB SimenB reopened this Aug 31, 2018
@natealcedo
Copy link

@SimenB I'd love to send in a PR. Will do when I get the chance to do so!

@natealcedo
Copy link

natealcedo commented Sep 1, 2018

Let's close this issue now since I believe this is solved satisfactorily enough with #6931 :)

@SimenB
Copy link
Member

SimenB commented Sep 1, 2018

Right!

@SimenB SimenB closed this as completed Sep 1, 2018
@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 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants