Google News
logo
How to Integrate Jest into an Angular application and library?
Admin

Publisher : Admin


How to Integrate Jest into an Angular application and library?

To integrate Jest into an Angular application or library, follow these steps :
1. Install Jest : Run the following command in the terminal to install Jest as a dev dependency in your Angular project:
npm install --save-dev jest​

 

2. Configure Jest : Create a jest.config.js file in the root directory of your project and configure Jest. A basic configuration for an Angular project would look like this :
module.exports = {
  roots: ['<rootDir>/src'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest'
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};​

3. Set up testing scripts : Add the following scripts to your package.json file :

"scripts": {
  "test": "jest",
  "test:watch": "jest --watch"
}​
4. Create test files : Create a __tests__ directory in your project and write test files for your components and services. The test files should have the extension .spec.ts.


5. Run tests : You can now run the tests by running the following command in the terminal :
npm run test​

Jest will automatically detect and run the tests in your __tests__ directory and display the results in the terminal. You can also run the tests in watch mode by using the test:watch script.
LATEST ARTICLES