All Coursera Quiz Answers

Programming Assignment: Writing a Unit Test Solution

In this article i am gone to share Programming with JavaScript by meta Week 4 | Programming Assignment: Writing a Unit Test Solution with you..

Enroll Link: Programming with JavaScript


Visit this link:ย  Programming Assignment: Array and object iteration Solution


 

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.

 


 

Programming Assignment: Writing a Unit Test Solution

  • Copy & paste this code on Files..
  • And save your file and then submit.

First Change in this file timesTwo.js

// Task 1: Code the timesTwo function declaration
function timesTwo(num){
    return num * 2;
}

// Task 2: Export the timesTwo function as a module
module.exports = timesTwo;

 


Second Change in this file timesTwo.test.js

const timesTwo = require('./timesTwo');

// Write the first test
test('returns the number times 2', () => {
    expect(timesTwo(10)).toBe(20);
});