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

Created on 9 Oct 2016  ·  3Comments  ·  Source: facebook/jest

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

Most helpful comment

'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)

All 3 comments

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.

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

'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)

Was this page helpful?
0 / 5 - 0 ratings