Câu hỏi phỏng vấn Angular
Câu hỏi

How would you run unit test?

Câu trả lời

To run unit tests in an Angular application, you typically use the Angular CLI (Command Line Interface), which is a powerful tool that simplifies many tasks in the development workflow, including testing. Here’s a step-by-step guide on how to run unit tests in Angular:

  1. Set Up Your Angular Environment:
    Ensure that you have the Angular CLI installed. If it's not installed, you can install it globally using npm (Node Package Manager) by running:

    bash Copy
    npm install -g @angular/cli
  2. Navigate to Your Project Directory:
    Open a terminal or command prompt, and navigate to the directory of your Angular project where you want to run the tests.

  3. Run the Tests:
    Execute the following command in the terminal:

    bash Copy
    ng test

    This command initiates the Karma test runner, which is configured by default when you create a new Angular project using the Angular CLI. Karma launches a browser instance (commonly Chrome) and runs the unit tests written in your project[14][17][18].

  4. View the Results:
    The results of the tests will be displayed in the terminal or command line interface. Karma also opens up a browser window where you can see if the tests pass or fail, along with other detailed information such as logs and errors.

  5. Optional: Continuous Testing:
    By default, the ng test command will keep watching for any changes in your files and re-run the tests whenever you save changes. This is very useful during development to get immediate feedback on your changes.

  6. Code Coverage (optional):
    If you want to generate a code coverage report to see how much of your codebase is covered by tests, you can run:

    bash Copy
    ng test --code...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

Why would you use a spy in a test?

middle

Explain how Custom Elements works internally?

senior

What is Reactive Programming and how does it relate to Angular?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào