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

[BUG] Jest 16 breaks using toHaveBeenCalledWith with 0 arguments to test for functions that have been called without arguments #1899

Closed
benmccormick opened this issue Oct 9, 2016 · 4 comments

Comments

@benmccormick
Copy link
Contributor

benmccormick commented Oct 9, 2016

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

Report a bug

What is the current behavior?

On Jest 16: testing toHaveBeenCalledWith with 0 arguments does not pass when a spy is called with 0 arguments

On Jest 15: testing toHaveBeenCalledWith with 0 arguments passes when a spy is called with 0 arguments

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can npm install and npm test.

Minimal test:

it('can check that a function is called without arguments', () => {
    let fn = jasmine.createSpy();

    fn();
    expect(fn).toHaveBeenCalledWith();
});

Repo: https://github.com/benmccormick/jest-no-args/tree/master

What is the expected behavior?

Testing using toHaveBeenCalledWith with 0 arguments should pass an assertion when a spy is called with 0 arguments

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.

> node_modules/.bin/jest --debug                               
jest version = 16.0.1
test framework = jasmine2
config = {
  "rootDir": "/Users/ben/Code/experiments/jest-no-args",
  "name": "-Users-ben-Code-experiments-jest-no-args",
  "setupFiles": [],
  "testRunner": "/Users/ben/Code/experiments/jest-no-args/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/ben/Code/experiments/jest-no-args/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "automock": false,
  "bail": false,
  "browser": false,
  "cacheDirectory": "/var/folders/wy/1r3js80s60q497r_lrjyb0bh0000gn/T/jest",
  "clearMocks": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "jsx",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "preprocessorIgnorePatterns": [
    "/node_modules/"
  ],
  "resetModules": false,
  "testEnvironment": "jest-environment-jsdom",
  "testPathDirs": [
    "/Users/ben/Code/experiments/jest-no-args"
  ],
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testRegex": "(/__tests__/.*|\\.(test|spec))\\.jsx?$",
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
 FAIL  __tests__/test.js
  ● can check that a function is called without arguments

    expect(spy).toHaveBeenCalledWith(expected)

    Expected spy to have been called with:
      [undefined]
    But it was called with:
      Array []

      at Object.<anonymous>.it (__tests__/test.js:6:16)
      at process._tickCallback (internal/process/next_tick.js:103:7)

  ✕ can check that a function is called without arguments (5ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.822s
Ran all test suites.

Node Version: 6.7.0
NPM Version: 3.10.7

@benmccormick
Copy link
Contributor Author

benmccormick commented Oct 9, 2016

Ok, have been diving in a bit. The matcher itself (defined in /Users/ben/Code/experiments/jest/packages/jest-matchers/src/spyMatchers.js) is passed a second "undefined" argument when no arguments are passed into the function. So the issue is presumably deeper (I'm guessing you want to distinguish between no arguments and explicitly passing undefined).

I'm not clear yet on where that argument passing is happening, but will try to figure it out and open a PR if I can

Update: I think I'll need some help to track this down. Enough changed with these matchers during this last release that I can't trace down where this broke.

@benmccormick benmccormick changed the title Jest 16 breaks using toHaveBeenCalledWith with 0 arguments to test for functions that have been called without arguments [BUG] Jest 16 breaks using toHaveBeenCalledWith with 0 arguments to test for functions that have been called without arguments Oct 10, 2016
@mthmulders
Copy link
Contributor

Just out of curiosity: why don't you use toHaveBeenCalled()?

@benmccormick
Copy link
Contributor Author

'toHaveBeenCalled' checks that the function was called at all. I have a test that specifically tests a function was called with no arguments.

The real world use case: This function acts on a list, and either takes an id (and acts on a single item) or is called without an id (and acts on the whole list)

@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 14, 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

No branches or pull requests

3 participants