
Course 2 – Programming with JavaScript quiz answers
Week 4: Testing
Meta Front-End Developer Professional Certificate
Complete Coursera Answers & Study Guide
Click to Enroll in Coursera Meta Front-End Professional Certificate
Testing INTRODUCTION
Node.js is a JavaScript runtime environment that allows developers to write server-side and networking applications using JavaScript. Node Package Manager (npm) is a package manager for the Node.js platform, which allows developers to install, manage & update packages from the npm registry.
In this section of the Meta Front-End Developer Professional Certificate program, you will explore how to install npm packages and how to work with package.json – a file that contains metadata associated with your project. We’ll also look at testing in JavaScript and code a simple unit test in Jest – one of the most popular unit test frameworks used in modern web development projects today.
Learning Objectives
- Describe Node.js and npm
- Explain how to install npm packages
- Describe how to work with package.json
- Explain the process of testing in JavaScript
- List the three most prevalent kinds of testing
- Demonstrate how to code a simple unit test in Jest
SELF REVIEW: WRITING A UNIT TEST
1. Did you complete the Writing a Unit Test exercise?
- Yes (Correct)
- No
Correct: Well done on completing the exercise!
2. Were there any parts of Writing a Unit Test exercise that you found difficult to complete?
- Yes (Correct)
- No
Correct: Well done on completing the exercise!
3. Would you say that you are able to explain, in your own words, what are unit tests?
- Yes (Correct)
- No
Correct: Well done on completing the exercise!
KNOWLEDGE CHECK: INTRODUCTION TO TESTING
1. What is the correct way to export the timesTwo function as a module so that Jest can use it in testing files?
- export module(timesTwo)
- module(exported(timesTwo))
- document.module.export = timesTwo
- module.exports = timesTwo (Correct)
Correct: The correct way to export the function is module.exports = timesTwo .
2. Testing is a way to verify the expectations you have regarding the behavior of your code.
- True (Correct)
- False
Correct
3. Node.js can be used to build multiple types of applications. Select all that apply.
- Command line applications (Correct)
- Desktop applications (Correct)
- Web application backends (Correct)
Correct: That’s correct! Command line applications can be built with using Node.js.
Correct: That’s correct! Desktop applications can be built with using Node.js.
Correct: That’s correct! Backends can be built with using Node.js.
4. When the following test executes, what will the test result be?
function add(a, b) {
return a + b;
}
expect(add(10, 5)).toBe(16);
- Success.
- Fail. (Correct)
Correct: The expectation will fail as the function will return 15 which does not equal 16.
5. Which of the following is the slowest and most expensive form of testing?
- Unit testing
- Integration testing
- End-to-end testing (e2e) (Correct)
Correct: Integration tests are more expensive than unit tests but End-to-end testing (e2e) is the most expensive as these tests take the most time to set up and run.
6. Mocking allows you to separate the code that you are testing from its related dependencies.
- True (Correct)
- False
Correct: That’s correct! Mocking allows you to isolate the code being tested in your unit tests.
7. Which of the following statements are true for Node.js?
- Node js is only active on the front-end.
- Node.js can run in multiple settings, for example, on the command line, in a desktop application, or on the back-end of a web app (on a server). (Correct)
- Node.js is a separate, standalone environment without ties to the JavaScript in the browser. (Correct)
Correct: Well done. Node.js can run in multiple settings, for example, on the command line, in a desktop application, or on the back-end of a web app (on a server). And it’s a completely separate, standalone environment without ties to the JavaScript in the browser.
Correct: Well done. Node.js can run in multiple settings, for example, on the command line, in a desktop application, or on the back-end of a web app (on a server). And it’s a completely separate, standalone environment without ties to the JavaScript in the browser.
8. Which of the following is a correct example of a Jest test?
- concatStrings(“123″,”456”); // “123456”
- expect(concatStrings(“123”, “456”)).toBe(“123456”); (Correct)
Correct: Yes, the above code is an example of a JavaScript testing framework syntax.
9. Which type of testing involves the process of testing the smallest units of your source code in isolation?
- End-to-end testing
- Integration testing
- Unit testing (Correct)
Correct: Yes, unit testing involves the process of testing the smallest units of your source code in isolation.
10. Which of the following features does Jest provide? Check all that apply.
- Jest supports code coverage. (Correct)
- A range of frameworks can be tested with Jest. (Correct)
- Jest can be used to write unit tests. (Correct)
Correct: That’s correct! Jest uses code coverage to measure what percentage of code is covered by tests.
Correct: That’s correct! Jest lets developers test a range of code types.
Correct: That’s correct! Jest allows you to easily work with unit tests.
11. Following the TDD approach has many benefits. Select all the benefits following the TDD approach has for you as a developer. Check all that apply.
- Minimize regressions. (Correct)
- Implementations can be tested using various inputs. (Correct)
- Stress-test your application.
- Run your infrastructure more efficiently.
Correct: That is correct! The benefits the TDD approach has for you, as a developer, is Minimizing regression and the fact that your implementations can be tested using various inputs.
Correct: That is correct! The benefits the TDD approach has for you, as a developer, is Minimizing regression and the fact that your implementations can be tested using various inputs.
12. What will happen when you run this piece of code on the command line at the root of your project’s folder: npm install –save-dev jest?
- It will add Jest globally to my machine.
- It will add Jest as a dependency inside package.json.
- It will add Jest as a devDependency inside package.json. (Correct)
Correct: You are correct! When you run this piece of code on the command line at the root of your project’s folder it will add Jest as a devDependency inside package.json.
MODULE QUIZ: TESTING
1. What is unit testing?
- Unit testing revolves around the idea of having separate, small pieces of code that are easy to test. (Correct)
- Unit testing tries to imitate how a user might interact with your app.
- Unit testing is testing how parts of your system interact with other parts of our system.
- Correct: That’s correct! Unit testing is focused on testing small, specific “units” of code.
Correct: Well done on completing the exercise!
2. When the following test executes, what will the test result be?
function subtract(a, b) {
return a - b;
}
expect(subtract(10, 4)).toBe(6);
- Success. (Correct)
- Fail.
Correct: That’s correct! The function will return 6 and the expectation will succeed.
3. What is End-to-end testing (e2e)?
- End-to-end testing revolves around the idea of having separate, small pieces of code that are easy to test.
- End-to-end testing tries to imitate how a user might interact with your application. (Correct)
- End-to-end testing is testing how parts of your system interact with other parts of our system.
Correct: That’s correct! End-to-end testing mimics how users will interact with your application.
4. What is Code Coverage?
- A measure of what percentage of your code has failing tests
- A measure of what percentage of your code is covered by tests. (Correct)
Correct: That’s correct! Code coverage shows what percentage of your code is covered by tests and where more testing may be required.
5. Node.js can be used to build web application backends.
- True (Correct)
- False
Correct: That’s correct! Node.js is a popular JavaScript runtime for building backends.
6. When the following test executes, what will the test result be?
function multiply(a, b) {
return a;
}
expect(multiply(2, 2)).toBe(4);
- Success.
- Fail. (Correct)
Correct: That’s correct! The function will fail because it has a bug which returns only the a variable. Therefore, 2 is returned from the function which does not match the expectation.
7. Which command is used to install a Node package?
- package
- pkg
- node
- npm (Correct)
Correct: That’s correct! The Node Package Manager (npm) is used to install packages.
8. Which file lists all your application’s required node packages?
- node.json
- npm.json
- package.json (Correct)
- pkg.json
Correct: That’s correct! package.json will store all the dependencies required for application.
9. A person on your team wants to help with testing. However, they are not a developer and cannot write code. What type of testing is most suited for them?
- Unit testing
- Integration testing
- End-to-end testing (Correct)
Correct: That’s correct! End-to-end tests can be performed in a web browser without writing code. This is because the tests mimic how a user will interact with the application.
10. What is the recommended way to separate the code that you are testing from its related dependencies?
- Mocking (Correct)
- module.exports
- End-to-end testing
Correct: That’s correct! Mocking allows developers to simulate the behavior of dependent code during tests.
Testing CONCLUSION
Node.js is a powerful framework that allows you to create server-side JavaScript applications. You can install npm packages to add functionality to your application, and you can use package.json to manage those dependencies. Testing is an important part of the development process, and Jest is a popular unit testing library for JavaScript.
Now that you know the basics of Node.js and npm, you’re ready to start building interesting applications. If you want to learn more, be sure to check out Coursera’s full course on Node.js
Subscribe to our site
Get new content delivered directly to your inbox.
Quiztudy Top Courses
Popular in Coursera
- Meta Marketing Analytics Professional Certificate.
- Google Digital Marketing & E-commerce Professional Certificate.
- Google UX Design Professional Certificate.
- Meta Social Media Marketing Professional Certificate
- Google Project Management Professional Certificate
- Meta Front-End Developer Professional Certificate
Liking our content? Then, don’t forget to ad us to your BOOKMARKS so you can find us easily!