August 2017
Beginner
374 pages
10h 41m
English
This is the oldest and most common way to deal with asynchronous code in JavaScript. You have a function that accepts a callback function as an argument; for example, fetchPosts(callback). When the asynchronous request finishes, the callback function is executed:
function fetchPosts (callback) { // usually, we would fetch posts in an asynchronous request here // in this example, we simply execute the callback callback([ { title: 'test' }, { title: 'hello world' } ])}
By default, Jest marks a test as complete when it reaches the end of the test function, which means that the following test will finish too early and not execute the matcher:
test('this finishes too early', () => { fetchPosts(posts => expect(posts).toContain({ title: ...Read now
Unlock full access