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

Added recipe for debugging tests in VS Code #53

Merged
merged 1 commit into from Jan 2, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -16,6 +16,9 @@ A collection of recipes for using VS Code with particular technologies.

- [Debugging Angular/C# apps with AspNetCore.SpaTemplates](https://github.com/Microsoft/vscode-recipes/tree/master/Angular-SpaTemplates)

- [Debugging Mocha tests](https://github.com/Microsoft/vscode-recipes/tree/master/debugging-mocha-tests)

- [Debugging Jest tests](https://github.com/Microsoft/vscode-recipes/tree/master/debugging-jest-tests)
## Container Technology

- [Debugging TypeScript in a Docker Container](https://github.com/weinand/vscode-recipes/tree/master/Docker-TypeScript)
Expand Down
27 changes: 27 additions & 0 deletions debugging-jest-tests/.vscode/launch.json
@@ -0,0 +1,27 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": [
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
70 changes: 70 additions & 0 deletions debugging-jest-tests/README.md
@@ -0,0 +1,70 @@
# Debugging tests in VS Code

by [Jag Reehal](https://twitter.com/jagreehal)

This recipe shows how to use the built-in Node Debugger to debug [Jest](https://facebook.github.io/jest/) tests.

## The example

The test folder contains two files that test the lib/calc.js file.

To try the example you'll need to install dependencies by running:

`npm install`

## Configure launch.json File for yur test framework

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

* Replace content of the generated launch.json with the following configurations:

```json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceRoot}/node_modules/jest/bin/jest",
"args": ["${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
```

## Debugging all tests

You can debug all tests by following the steps below:

1. Set a breakpoint in a test file or files

2. Go to the Debug view, select the **'Jest All'** configuration, then press F5 or click the green play button.

3. Your breakpoint will now be hit

![all](all.gif)

## Debugging the current test

You can debug the test you're editing by following the steps below:

1. Set a breakpoint in a test file

2. Go to the Debug view, select the **'Jest Current File'** configuration, then press F5 or click the green play button.

3. Your breakpoint will now be hit

![current](current.gif)
Binary file added debugging-jest-tests/all.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added debugging-jest-tests/current.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions debugging-jest-tests/lib/calc.js
@@ -0,0 +1,7 @@
exports.add = (x, y) => {
return x + y;
};

exports.subtract = (x, y) => {
return x - y;
};
13 changes: 13 additions & 0 deletions debugging-jest-tests/package.json
@@ -0,0 +1,13 @@
{
"name": "vscode-recipes-debugging-jest-tests",
"version": "1.0.0",
"description": "This recipe shows how to use the built-in Node Debugger to debug Jest tests",
"scripts": {
"test": "jest"
},
"author": "Jag Reehal",
"license": "ISC",
"dependencies": {
"jest": "22.0.4"
}
}
8 changes: 8 additions & 0 deletions debugging-jest-tests/test/add.spec.js
@@ -0,0 +1,8 @@
const { add } = require('../lib/calc');

describe('When adding numbers', () => {
it('Shoud return correct result', () => {
const result = add(1, 2);
expect(result).toEqual(3);
});
});
8 changes: 8 additions & 0 deletions debugging-jest-tests/test/subtract.spec.js
@@ -0,0 +1,8 @@
const { subtract } = require('../lib/calc');

describe('When subtracing numbers', () => {
it('Shoud return correct result', () => {
const result = subtract(3, 2);
expect(result).toEqual(1);
});
});
33 changes: 33 additions & 0 deletions debugging-mocha-tests/.vscode/launch.json
@@ -0,0 +1,33 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
80 changes: 80 additions & 0 deletions debugging-mocha-tests/README.md
@@ -0,0 +1,80 @@
# Debugging tests in VS Code

by [Jag Reehal](https://twitter.com/jagreehal)

This recipe shows how to use the built-in Node Debugger to debug [Mocha](https://mochajs.org/) tests.

## The example

The test folder contains two files that test the lib/calc.js file.

To try the example you'll need to install dependencies by running:

`npm install`

## Configure launch.json File for your test framework

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

* Replace content of the generated launch.json with the following configurations:

```json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
```

## Debugging all tests

You can debug all tests by following the steps below:

1. Set a breakpoint in a test file or files

2. Go to the Debug view, select the **'Mocha All'** configuration, then press F5 or click the green play button.

3. Your breakpoint will now be hit

![all](all.gif)

## Debugging the current test

You can debug the test you're editing by following the steps below:

1. Set a breakpoint in a test file

2. Go to the Debug view, select the **'Mocha Current File'** configuration, then press F5 or click the green play button.

3. Your breakpoint will now be hit

![current](current.gif)
Binary file added debugging-mocha-tests/all.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added debugging-mocha-tests/current.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions debugging-mocha-tests/lib/calc.js
@@ -0,0 +1,7 @@
exports.add = (x, y) => {
return x + y;
};

exports.subtract = (x, y) => {
return x - y;
};
14 changes: 14 additions & 0 deletions debugging-mocha-tests/package.json
@@ -0,0 +1,14 @@
{
"name": "vscode-recipes-debugging-mocha-tests",
"version": "1.0.0",
"description": "This recipe shows how to use the built-in Node Debugger to debug Mocha tests",
"scripts": {
"test": "mocha 'test/**/*.spec.js'"
},
"author": "Jag Reehal",
"license": "ISC",
"dependencies": {
"chai": "4.1.2",
"mocha": "4.1.0"
}
}
9 changes: 9 additions & 0 deletions debugging-mocha-tests/test/add.spec.js
@@ -0,0 +1,9 @@
const assert = require('chai').assert;
const { add } = require('../lib/calc');

describe('When adding numbers', () => {
it('Shoud return correct result', () => {
const result = add(1, 2);
assert.equal(result, 3);
});
});
9 changes: 9 additions & 0 deletions debugging-mocha-tests/test/subtract.spec.js
@@ -0,0 +1,9 @@
const assert = require('chai').assert;
const { subtract } = require('../lib/calc');

describe('When subtracing numbers', () => {
it('Shoud return correct result', () => {
const result = subtract(3, 2);
assert.equal(result, 1);
});
});