Skip to content

This repository offers a quick guide to unit testing with Jest, covering setup, adding Jest as a devDependency, creating a timesTwo function, and writing and running your first test.

Notifications You must be signed in to change notification settings

mohammadnazm/unit-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab Instructions: Unit Testing

Tips: Before you Begin

To view your code and instructions side-by-side, select the following in your VSCode toolbar:

  • View -> Editor Layout -> Two Columns
  • To view this file in Preview mode, right click on this README.md file and Open Preview
  • Select your code file in the code tree, which will open it up in a new VSCode tab.
  • Drag your assessment code files over to the second column.
  • Great work! You can now see instructions and code at the same time.
  • Questions about using VSCode? Please see our support resources here:
    Visual Studio Code on Coursera

To run your JavaScript code

  • Select your JavaScript file
  • Select the "Run Code" button in the upper right hand toolbar of VSCode.
    Ex: It looks like a triangular "Play" button.

Task 1: Add Jest as a devDependency

Open terminal. Make sure that it's pointing to jest-testing directory.
Install the jest npm package using the npm install command and the --save-dev flag.
Verify that the installation was completed successfully by opening the package.json file and confirming that the "devDependencies" entry lists jest similar to the following:

"devDependencies": {
    "jest": "^28.0.0"
}

Task 2: Update the test entry

In the package.json file, locate the "scripts" entry, and inside of it, update the test entry to jest.

Task 3: Code the timesTwo function

Open the timesTwo.js file and add a function named timesTwo. The function should take number as input and return the value 2 multiplied by the number. Export the timesTwo function as a module.

Task 4: Write the first test

Code a test call with the following arguments:

  1. The description that reads: "returns the number times 2".
  2. The second argument should expect the call to the timesTwo function, when passed the number 10, to be 20.

Task 5: Run the first test

With the terminal pointed at the jest-testing directory, run the test script using npm.

About

This repository offers a quick guide to unit testing with Jest, covering setup, adding Jest as a devDependency, creating a timesTwo function, and writing and running your first test.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published