你想请求一个_feature_还是报告一个_bug_?
报告错误
当前的行为是什么?
在 Jest 16 上:当使用 0 个参数调用间谍时,使用 0 个参数测试toHaveBeenCalledWith
不会通过
在 Jest 15 上:当使用 0 个参数调用间谍时,使用 0 个参数测试toHaveBeenCalledWith
通过
如果当前行为是错误,请提供重现的步骤,如果可能的话,请提供 GitHub 上的最小存储库,我们可以npm install
和npm test
。
最小测试:
it('can check that a function is called without arguments', () => {
let fn = jasmine.createSpy();
fn();
expect(fn).toHaveBeenCalledWith();
});
回购: https ://github.com/benmccormick/jest-no-args/tree/master
预期的行为是什么?
当使用 0 个参数调用间谍时,使用带有 0 个参数的toHaveBeenCalledWith
进行测试应该传递一个断言
使用--debug
再次运行 Jest 并提供它打印的完整配置。
> 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.
节点版本:6.7.0
NPM 版本:3.10.7
好的,已经潜水了一点。 当没有参数传递给函数时,匹配器本身(在/Users/ben/Code/experiments/jest/packages/jest-matchers/src/spyMatchers.js
中定义)传递了第二个“未定义”参数。 所以这个问题可能更深(我猜你想区分无参数和显式传递未定义)。
我还不清楚那个论点在哪里传递,但如果可以的话,我会试着弄清楚并打开一个 PR
更新:我想我需要一些帮助来追踪它。 在最后一个版本中,这些匹配器已经发生了足够的变化,我无法追查到哪里出了问题。
只是出于好奇:你为什么不使用toHaveBeenCalled()
?
'toHaveBeenCalled' 检查函数是否被调用。 我有一个测试专门测试一个没有参数调用的函数。
真实世界的用例:这个函数作用于一个列表,要么接受一个 id(作用于单个项目),要么在没有 id 的情况下调用(作用于整个列表)
最有用的评论
'toHaveBeenCalled' 检查函数是否被调用。 我有一个测试专门测试一个没有参数调用的函数。
真实世界的用例:这个函数作用于一个列表,要么接受一个 id(作用于单个项目),要么在没有 id 的情况下调用(作用于整个列表)