concept Jest in category javascript

appears as: Jest
Testing JavaScript Applications

This is an excerpt from Manning's book Testing JavaScript Applications.

Note

By default, Jest loads all files ending in .test.js, .spec.js, or tests inside folders named tests.

Prepare your tests for Jest to run by wrapping them into the test function that Jest adds to the global scope. You can use this function to organise multiple tests within a single file and indicate what should run. It takes the test’s name as its first argument and a callback function containing the actual test as the second argument.

Listing 6.12. package.json

{
  // ...
  "scripts": {
    // ...
    "build": "browserify main.js -o bundle.js"  #1
  },
  // ...
}
Figure 6.8. Your spy should only capture the calls which happen after executing main.js.
06 08

It’s important to see that, in these examples, we’re not trying to replicate our back-end’s behaviour in our tests. We’re separately checking both the request we send, and whether we can handle the messages we receive. Checking whether the back-end sends the right messages to the right clients is a validation that should happen within the server’s tests, not the client’s.

Now that you have learned how to set up a socket.io server that you can use within your tests, and how to validate your WebSockets integrations, try extending this application with new functionality and testing it. Remember that you write these tests at multiple different levels of integration, either by checking your handler functions individually or by pushing real messages through a test server. Try, for example, pushing live-updates when clients click the undo button, or maybe try adding a test which checks whether main.js connects to the server when the page loads.