This repository contains the contents for a tech talk focused on writing test cases using Jest in JavaScript projects. It includes step-by-step instructions, sample code, and best practices for testing various types of functions.
In this example I have covered the basics of writing test cases with jest.
- Writing test cases for functions using test() and grouping using describe().
- Writing Postive and Negative testcases example.
- Mocking function calls and validating function invocation.
- Snapshot testing example.
- Generating Code Coverage reports.
Follow the steps below to clone the repository, install the required dependencies, and run the test cases.
To start, clone the repository to your local machine using the following command:
git clone https://github.com/SruthiKrish19/js-meetup-jest.git
Navigate to the project folder using the below command
cd jest
Install the project dependencies using npm:
npm install
This command will install all necessary packages listed in the package.json file, including Jest for running the tests.
Once the dependencies are installed, you can run the test cases with the following command:
npm test
This will execute all the test cases in the project. Jest will automatically detect and run any test files (typically files with the .test.js or .spec.js extension) and provide a report of the results.
To see more detailed information about each test, use the --verbose flag:
npm test -- --verbose
To re-run the tests automatically whenever you make changes, use Jest's watch mode:
npm test -- --watch
You can also run a specific test file by providing the file path:
npm test -- <file-path>