Application setup
Basic application with jest setup
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Lesson Transcript
[00:00 - 00:32] In this lesson, I'll be walking through setting up our sample Node.js project that we're used to learn automated testing with Jest. The first thing we need to do is create a folder for our project and initialize it with npm. So open your terminal and run this command. Next, cd into my recipe and then run npm init.
[00:33 - 00:48] We need to fill out this prompt for the most part I'll be going along with the default options. For the entry point, I'll use source up the Jest.
[00:49 - 01:31] And for the author, I'll enter my name. Next, I need to open this in Visual Studio Code. The next thing I need to do is create a source folder where our application code will leave. And inside the source folder, I 'll create a file up the Jest that will be our entry point. Having created our up the Jest file, the next thing is to add our testing framework Jest. I'll open the terminal in Visual Studio Code .
[01:32 - 02:38] This installs Jest as a dev dependency. Once we have Jest installed, we need to initialize Jest, which will create our Jest configuration file. Please command. We need to fill this prompt. Would I like to use Jest when running test scripts in package adjacent? Yes. Whether or not we'll be using TypeScript? No. As for the testing environment, I'll select node. Whether or not we want to add test coverage? Yes. As for the provider, I'll go with V8. And whether or not we want to automatically clear marks. For now, I'll select no. After running this command, this creates a new file, jest.config.js. In this file, you have all the configuration options for Jest.
[02:39 - 03:03] I'll go ahead and close this file and then open my up.js file. In the up.js file, we export a new function, getRecipes. The getRecipes function will return a hardcode at least. So, I'll go ahead and close this file. This is the test. I'll go ahead and close this file.
[03:04 - 03:24] I'll go ahead and close this file. I'll go ahead and close this file. I'll go ahead and close this file. Okay. So, I'll go ahead and close this file. I'll go ahead and close this file.
[03:25 - 03:44] Finally, we need to create a test folder where all of our tests will leave. And in the test folder, I'll create a file up.test.js.
[03:45 - 04:09] I'll import the getRecipes function from up.js. [silence] To create a test case, we need to use the test function.
[04:10 - 04:47] And this test function requires two arguments. The first is a string describing what the test is all about and the second is a callback function. The first thing I'll need to do in this test method is to execute the getRec ipes function.
[04:48 - 05:06] [silence] Afterwards, I need to make an assertion to check that recipes.length is greater than 0. To accomplish that, I'll use the expect function to check that recipes.length is not equals to 0.
[05:07 - 05:59] [silence] And there you have it. We have a basic test here. To execute this test or run this test, we need to open our terminal and run the npm test script. [silence] And there you have it. We have a basic test in our app.test.js and we're able to run this test using the npm run test script. And we have a test report in our terminal. In the next module, we'll discuss mocking and unit testing. So you're in the next lesson.
[00:00 - 00:32] In this lesson, I'll be walking through setting up our sample Node.js project that we're used to learn automated testing with Jest. The first thing we need to do is create a folder for our project and initialize it with npm. So open your terminal and run this command. Next, cd into my recipe and then run npm init.
[00:33 - 00:48] We need to fill out this prompt for the most part I'll be going along with the default options. For the entry point, I'll use source up the Jest.
[00:49 - 01:31] And for the author, I'll enter my name. Next, I need to open this in Visual Studio Code. The next thing I need to do is create a source folder where our application code will leave. And inside the source folder, I 'll create a file up the Jest that will be our entry point. Having created our up the Jest file, the next thing is to add our testing framework Jest. I'll open the terminal in Visual Studio Code .
[01:32 - 02:38] This installs Jest as a dev dependency. Once we have Jest installed, we need to initialize Jest, which will create our Jest configuration file. Please command. We need to fill this prompt. Would I like to use Jest when running test scripts in package adjacent? Yes. Whether or not we'll be using TypeScript? No. As for the testing environment, I'll select node. Whether or not we want to add test coverage? Yes. As for the provider, I'll go with V8. And whether or not we want to automatically clear marks. For now, I'll select no. After running this command, this creates a new file, jest.config.js. In this file, you have all the configuration options for Jest.
[02:39 - 03:03] I'll go ahead and close this file and then open my up.js file. In the up.js file, we export a new function, getRecipes. The getRecipes function will return a hardcode at least. So, I'll go ahead and close this file. This is the test. I'll go ahead and close this file.
[03:04 - 03:24] I'll go ahead and close this file. I'll go ahead and close this file. I'll go ahead and close this file. Okay. So, I'll go ahead and close this file. I'll go ahead and close this file.
[03:25 - 03:44] Finally, we need to create a test folder where all of our tests will leave. And in the test folder, I'll create a file up.test.js.
[03:45 - 04:09] I'll import the getRecipes function from up.js. [silence] To create a test case, we need to use the test function.
[04:10 - 04:47] And this test function requires two arguments. The first is a string describing what the test is all about and the second is a callback function. The first thing I'll need to do in this test method is to execute the getRec ipes function.
[04:48 - 05:06] [silence] Afterwards, I need to make an assertion to check that recipes.length is greater than 0. To accomplish that, I'll use the expect function to check that recipes.length is not equals to 0.
[05:07 - 05:59] [silence] And there you have it. We have a basic test here. To execute this test or run this test, we need to open our terminal and run the npm test script. [silence] And there you have it. We have a basic test in our app.test.js and we're able to run this test using the npm run test script. And we have a test report in our terminal. In the next module, we'll discuss mocking and unit testing. So you're in the next lesson.