Deseja solicitar um _recurso_ ou relatar um _bug_?
Reportar um erro
Qual é o comportamento atual?
No Jest 16: testar toHaveBeenCalledWith
com 0 argumentos não passa quando um espião é chamado com 0 argumentos
No Jest 15: testando toHaveBeenCalledWith
com 0 argumentos passa quando um espião é chamado com 0 argumentos
Se o comportamento atual for um bug, forneça as etapas para reproduzir e, se possível, um repositório mínimo no GitHub que possamos npm install
e npm test
.
Teste mínimo:
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
Qual é o comportamento esperado?
Testar usando toHaveBeenCalledWith
com 0 argumentos deve passar uma asserção quando um espião é chamado com 0 argumentos
Execute o Jest novamente com --debug
e forneça a configuração completa que ele imprime.
> 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.
Versão do nó: 6.7.0
Versão NPM: 3.10.7
Ok, tenho mergulhado um pouco. O próprio matcher (definido em /Users/ben/Code/experiments/jest/packages/jest-matchers/src/spyMatchers.js
) recebe um segundo argumento "indefinido" quando nenhum argumento é passado para a função. Portanto, o problema é presumivelmente mais profundo (suponho que você queira distinguir entre nenhum argumento e passar explicitamente indefinido).
Ainda não tenho certeza de onde essa passagem de argumento está acontecendo, mas tentarei descobrir e abrir um PR se puder
Atualização: Acho que vou precisar de ajuda para rastrear isso. O suficiente mudou com esses matchers durante esta última versão que não consigo rastrear onde isso quebrou.
Só por curiosidade: por que você não usa toHaveBeenCalled()
?
'toHaveBeenCalled' verifica se a função foi chamada. Eu tenho um teste que testa especificamente uma função foi chamada sem argumentos.
O caso de uso do mundo real: Esta função atua em uma lista e recebe um id (e atua em um único item) ou é chamada sem um id (e atua em toda a lista)
Comentários muito úteis
'toHaveBeenCalled' verifica se a função foi chamada. Eu tenho um teste que testa especificamente uma função foi chamada sem argumentos.
O caso de uso do mundo real: Esta função atua em uma lista e recebe um id (e atua em um único item) ou é chamada sem um id (e atua em toda a lista)