Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe for Chrome Debugging with Angular CLI #2

Merged
merged 1 commit into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Angular-Debugging/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome with ng serve",
"url": "http://localhost:4200/#",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"trace": true,
"userDataDir": "${workspaceRoot}/.vscode/chrome"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "userDataDir" and "sourceMaps" options are no longer needed as they are set by default

Also we probably want to disable "trace" to avoid spamming peoples logs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed .. Removed these in PR #5: Remove sourceMaps, trace, userDataDir from launch.json.

},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome with ng test",
"url": "http://localhost:9876/debug.html",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"trace": true,
"userDataDir": "${workspaceRoot}/.vscode/chrome"
}
]
}
102 changes: 102 additions & 0 deletions Angular-Debugging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Chrome Debugging with Angular CLI

This recipe shows how to use the [Debugger for Chrome](https://github.com/Microsoft/vscode-chrome-debug) extension with VS Code to debug
an application generated by the [Angular CLI](https://cli.angular.io/).

## Getting Started

- Use [NPM](https://www.npmjs.com) to install Angular CLI globally.

```
npm install -g @angular/cli
```

- Install the [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome) extension for VS Code.
Press Cmd + P (MacOS) or Ctrl + P (Windows/Linux), then paste the following command and press Enter.

```
ext install debugger-for-chrome
```

- Use Angular CLI to create a new Angular application.

```
ng new my-dream-app
```

- Change to the app directory and open VS Code.

```
cd my-dream-app
code .
```

## Configure launch.json File

- Click on the Debugging icon in the Activity Bar to bring up the Debug view.
Then click on the *settings* icon to configure a launch.json file, selecting **Chrome** for the environment.

![add-chrome-debug](https://user-images.githubusercontent.com/2836367/27004175-77582668-4dca-11e7-9ce8-30ef3af64a36.png)

- Replace content of the the generated launch.json with the following two configurations.

```json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome with ng serve",
"url": "http://localhost:4200/#",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"trace": true,
"userDataDir": "${workspaceRoot}/.vscode/chrome"
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome with ng test",
"url": "http://localhost:9876/debug.html",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"trace": true,
"userDataDir": "${workspaceRoot}/.vscode/chrome"
}
]
}
```

## Start Debugging

- Set a breakpoint in **app.component.ts** on the line that sets the `title` property of `AppComponent`.

- Open a terminal at the root folder and serve the app using Angular CLI.

```
ng serve
```

- After the app is shown in the browser, go to the Debug view, select the 'Launch Chrome with ng serve' configuration, then press F5 or click the green button to start debugging.

- Refresh the browser and you should hit the breakpoint.

![angular-breakpoint](https://user-images.githubusercontent.com/2836367/27004337-40bca8d8-4dcd-11e7-837e-b7602a3a622a.png)

## Debug Unit Tests

- Set a breakpoint in **app.component.spec.ts** on a line in one of the unit tests.

- Open a terminal at the root folder and run the tests using Angular CLI.

```
ng test
```

- After the test run, go to the Debug view, select the 'Launch Chrome with ng test' configuration, then press F5 or click the green button to start debugging.

- When a browser opens with the test list, click the link for the test in which you placed the breakpoint. You should then hit the breakpoint.

![angular-test-breakpoint](https://user-images.githubusercontent.com/2836367/27004448-e5134ff8-4dce-11e7-8145-69de0956dd07.png)