Skip to content

Instantly share code, notes, and snippets.

@ivawzh
Last active October 13, 2016 06:11
Show Gist options
  • Save ivawzh/c3fc7a732864613936553fbd53b40a6a to your computer and use it in GitHub Desktop.
Save ivawzh/c3fc7a732864613936553fbd53b40a6a to your computer and use it in GitHub Desktop.
Jest test 101
// @ package.json
{
"scripts": {
"jest": "NODE_ENV=test DB_USER=ivan-wang ./node_modules/jest/bin/jest.js",
"jest-watch": "NODE_ENV=test DB_USER=ivan-wang ./node_modules/jest/bin/jest.js --watch --onlyChanged"
},
"jest": {
"testEnvironment": "node",
"automock": true,
"rootDir": "",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/lodash/",
"<rootDir>/node_modules/firebase/"
]
},
"devDependencies": {
"babel-jest": "^16.0.0",
"jest": "^16.0.1"
}
}
// @ test.js
jest.unmock('./dummy')
import doSomething from './dummy'
import { plusOne } from './module'
describe('Do something', () => {
plusOne.mockImplementation(num => num + 1)
it('returns 6', () => {
expect(plusOne(0)).toEqual(1)
expect(doSomething()).toEqual(6)
})
})
// @ dummy.js
import { plusOne } from './module'
export default function doSomething() {
const num = 5
return plusOne(5)
}
// @ module.js
export function plusOne (num) {
return num + 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment