2017-09-07 5 views
1

jest을 사용하여 다음 단위 테스트를 만들려고합니다.`TypeError : jest.fn이 함수가 아닙니다. '

jest.dontMock("pointsAwardingActions.js"); 
describe("points awarding actions",() => { 
    describe("award points",() => { 
    it("should dispatch begin ajax action",() => { 
     var pointsAwardingActions = require("pointsAwardingActions.js"); 
     const mockedDispatch = jest.fn(); 
    }); 
    }); 
}); 

그러나 npm test을 실행 한 후에 다음 오류가 발생합니다.

TypeError: jest.fn is not a function

package.json의 일부 섹션입니다.

{ 
    "scripts": { 
    "test": "jest" 
    }, 
    "author": "alayor", 
    "license": "ISC", 
    "jest": { 
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest", 
    "testFileExtensions": ["spec.js"], 
    "moduleFileExtensions": ["js"], 
    "collectCoverage": "true" 
    }, 
    "dependencies": { 
    "babel-cli": "6.8.0", 
    "babel-core": "6.8.0", 
    "babel-jest": "^6.0.1", 
    "babel-loader": "6.2.4", 
    "babel-plugin-react-display-name": "2.0.0", 
    "babel-polyfill": "6.8.0", 
    "babel-preset-es2015": "6.6.0", 
    "babel-preset-react": "6.5.0", 
    "babel-preset-react-hmre": "1.1.1", 
    "expect": "1.19.0", 
    "express": "4.13.4", 
    "jest": "^0.1.40", 
    "jest-cli": "^0.8.1", 
    ... 
    } 
} 

내가 그 오류를 표시하는 이유가 무엇일 수 있습니다.

답변

3

매우 오래된 Jest 버전 don’t support jest.fn을 사용하고 있습니다. Jest는 그 이후로 크게 개선되었으며 최신 버전으로 업데이트하는 것이 좋습니다.

또한 지금은 자동 조롱을하지 않습니다. 그래서,

const jest = require('jest-mock'); 
const spy = jest.fn(); 
+1

이 같은 오류가 표시 : 직접 농담 개체를 가져올 경우, 당신은 경유하여 농담-모의 모듈이 아닌 농담-CLI 모듈을 가져올. – Swordfish0321

+1

나는 21.2.1에 있는데이 같은 오류를 보았습니다 – TFischer

-1

다음 코드는 V22에서 작동해야하는 문서 jestrequire('jest');하지만 require('jest-mock') 아니라고 언급되지 않은 비트 이상한 명시 적으로 가져올 필요가 없습니다.

// Not necessary inside a Jest test file 
import jest from 'jest-mock'; 

const mock = jest.fn(); 
내가 17.0.2에있어
+1

"jest 객체는 모든 테스트 파일 내에서 자동으로 범위 내에 있습니다." https://facebook.github.io/jest/docs/en/jest-object.html –

1

jest object 모든 테스트 파일 내의 범위에 자동적 :