Installing and running Jest

First, let's install the Jest command-line interface (Jest CLI):

npm install –-save-dev jest-cli

This command installs the Jest CLI, and adds it as a development dependency to our ~/snapterest/package.json file. Next, let's edit the package.json file. We'll replace the existing "script" object:

"scripts": {
  "test": "echo "Error: no test specified" && exit 1"
},

Replace the preceding object with the following one:

"scripts": {
  "test": "jest"
},

Now we're ready to run our test suit. Navigate to the ~/snapterest/ directory, and run the following command:

npm test

You should see the following message in your Terminal:

Using Jest CLI v0.4.18
 PASS  source/utils/__tests__/TweetUtils-test.js (0.065s)
1 test passed (1 total)
Run time: 0.295s

As you can see, I am using Version 0.4.18 of the Jest CLI. When you run your test, the Jest version is likely to be higher than this.

The key line in this message is as follows:

PASS  source/utils/__tests__/TweetUtils-test.js (0.065s)
  • PASS: This tells you that your test has passed
  • source/utils/__tests__/TweetUtils-test.js: This tells you what test it was running
  • (0.065s): This tells how long it took to run the test

That's all it takes to write and test a tiny unit test. Now, let's create another one!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.55.151