Painless JavaScript Unit Testing
-
Familiar Approach: Built on top of Jasmine test framework, a familiar BDD testing environment
-
Mock by Default: Automatically mocks CommonJS modules returned by require(), making most existing code testable
-
Short Feedback Loop: Tests run in parallel and DOM apis are mocked so you can run tests on the command line
Check out the Getting Started tutorial. It's pretty simple!
<generated_api_start />
jest.autoMockOff()jest.autoMockOn()jest.clearAllTimers()jest.dontMock(module)jest.genMockFromModule(moduleObj)jest.genMockFunction()jest.genMockFn()jest.mock(moduleName)jest.runAllTicks()jest.runAllTimers()jest.runOnlyPendingTimers()jest.setMock(moduleName, moduleExports)
mockFn.mock.callsmockFn.mock.instancesmockFn.mockClear()mockFn.mockImplementation(fn)mockFn.mockImpl(fn)mockFn.mockReturnThis()mockFn.mockReturnValue(value)mockFn.mockReturnValueOnce(value)
config.collectCoverage[boolean]config.collectCoverageOnlyFrom[object]config.modulePathIgnorePatterns[array]config.rootDir[string]config.scriptPreprocessor[string]config.setupEnvScriptFile[string]config.setupTestFrameworkScriptFile[string]config.testFileExtensions[array]config.testPathDirs[array]config.testPathIgnorePatterns[array]config.unmockedModulePathPatterns[array]
jestrequire(module)require.requireActual(module)describe(name, fn)beforeEach(fn)afterEach(fn)it(name, fn)it.only(name, fn)executes only this test. Useful when investigating a failurepit(name, fn)helper for promises
.notinverse the next comparison.toThrow(?message).toBe(value)comparison using===.toEqual(value)deep comparison. Usejasmine.any(type)to be softer.toBeFalsy().toBeTruthy().toBeNull().toBeUndefined().toBeDefined().toMatch(regexp).toContain(string).toBeCloseTo(number, delta).toBeGreaterThan(number).toBeLessThan(number).toBeCalled().toBeCalledWith(arg, um, ents).lastCalledWith(arg, um, ents)
<generated_api_end />