How to debug mocha unit tests in a vue application in VSCode? - unit-testing

I have a vue application with unit tests using unit-mocha. Tests run fine, but I can't debug them using VSCode debugger.
Here is what I've done:
First, I created a vue app in VSCode:
vue create hello-world
Then, I added unit-mocha:
cd hello-world
vue add unit-mocha
This generates file tests/unit/example.spec.js:
import { expect } from 'chai'
import { shallowMount } from '#vue/test-utils'
import HelloWorld from '#/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallowMount(HelloWorld, {
propsData: { msg }
})
expect(wrapper.text()).to.include(msg)
})
})
I ran the tests:
npm run test:unit
This works just fine.
But now, I would like to add breakpoints in the file and run the debugger.
So I did the following based on this thread: Configuration for Debugging Mocha Unit Tests in Vue.js with VSCode
I switched to Run tab on VSCode and clicked create a launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Unit Tests",
"program": "${workspaceFolder}/node_modules/#vue/cli-service/bin/vue-cli-service.js",
"args": ["test:unit", "--inspect-brk", "--watch", "--timeout", "999999"],
"port": 9229,
"console": "integratedTerminal"
}
]
}
I also added vue.config.js as suggested in the thread:
module.exports = {
transpileDependencies: ["vuetify"],
chainWebpack: (config) => {
if (process.env.NODE_ENV === "test") {
config.merge({
devtool: "cheap-module-eval-source-map",
});
}
},
};
Finally I set a breakpoint on the first statement of example.spec.js. and clicked Start debugging (with "Debug Unit Tests" selected). I have the following output in the terminal console:
Debugger listening on ws://127.0.0.1:53241/2faf3b6b-dd6f-476a-80bc-51b99df90ec3
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Debugger listening on ws://127.0.0.1:9229/ad9ccf9a-9e2c-4ed9-a379-3e942c68d185
For help, see: https://nodejs.org/en/docs/inspector
Debugger seems to start (even twice??) but the test code isn't executed. My breakpoint is not hit (it turned gray and say "Unbound breakpiont"). Then I stopped the debugger.
I then retried to rerun the tests (npm run test:unit) to make sure it's not broken but it's ok.
Can anyone tell me what I am doing wrong?

I've finally found the solution I was looking for:
launch.json (thanks to OmegaOdie):
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Unit Tests",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npx",
"console": "integratedTerminal",
"runtimeArgs": ["vue-cli-service", "test:unit", "--inspect-brk"]
}
]
}
vue.config.json (thanks to this thread):
module.exports = {
devServer: {
...
},
// should be false for production
productionSourceMap: true,
chainWebpack: (config) => {
if (process.env.NODE_ENV === "test") {
// if unit testing, keep original lines to make breakpoints in original source files work
config.merge({
devtool: "cheap-module-eval-source-map",
});
} else {
// if not unit testing, use normal source maps
config.merge({
devtool: "source-map",
});
}
},
};
With this configuration, no need of debugger; instruction for breakpoints to work.

It aint perfect but seems to be working for me, more details can be found #https://nodejs.org/en/docs/guides/debugging-getting-started/
but the launch.json config should look like this
{
"name": "TestBreak",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npx",
"console": "integratedTerminal",
"runtimeArgs": [
"vue-cli-service",
"test:unit",
"--inspect-brk"
],
}
I'm hitting breakpoints with this. However I have to add debugger statements like this
debugger; // eslint-disable-line no-debugger
The comment is required for me due to some unknown linter.

Related

breakpoints on django-rest-framework project in vscode are not hit in debug mode

I have django-rest project on my vscode.The problem is that when I run the project in debug mode the breakpoints are not catched!!. I just get response without being stopped on that specific breakpoint.
This is my launch.json file
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true,
"justMyCode": true
}
]
}
BTW I send the requests by swagger!
I have also tried postman and got the same result!
(just got the response without the code being stopped on this view )
I send the requests to ManageUserView

Setup the VSCode debugger for python 2.7 and Django 1.11

I already setup a debugger for an existing django 1,11 project on vscode with the Microsoft python extension, but this will only works on http://127.0.0.1:5000/ (the homepage) I need this debugger to be hooked to my already running server on http://localhost:8000/
I need a setup that allows me to debug on vscode, make the code stop at specific breakpoints, debug code for various URLs using google chrome.
This is my launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
}
]
}
Python 2.7
Django 1.11
Visual Studio Code
MacOS Catalina
Please help.
Use 8000 as shown below in your setup
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"8000",
"--noreload",
"--nothreading"
],
"django": true
}
]
}

FLASK won't reload after changing code in VS Code

My editor is vs code, I am running flask with the below config
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "application.py",
"FLASK_ENV": "development",
"DATABASE_URL": "postgres://localhost/cs50w_project1_development",
"FLASK_DEBUG": 1,
"SECRET_KEY": "abcefefe"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
It seems all good except flask won't hot reload when I am changing the code. E.G., add an action.
I have to manually reload the flask by clicking the restart button.
Is there any issue with my current config?
Remove the --no-reload in the launch.json. It's a very old thread but posting the answer here for future visitors.

Chutzpah and Jasmine typescript unit testing

I am trying to get Chutzpah and Jasmine working together in visual studio, my end goal is to get unit tests running with TeamCity integration.
On save, all of the typescript code generates a single .js file. This also causes Chutzpah to run my tests, so far so good.
My issue is Chutzpah reports 0 passed, 0 failed and 0 errors. The Jasmine html file that is generated lists out all of my tests correctly but Chutzpa doesn't seem to receive any information back from Jasmine.
Highlights of a trace log:
Trying to build test context for c:\.....\test.ts
Building test context for c:\.....\test.ts
...framework dependencies / other ok looking things... (~15 lines)
Finished building test context for c:\.....\test.ts
Warning: 0 : Message:Chutzpah determined generated .js files are missing but the compile
mode is External so Chutzpah can't compile them. Test results may be wrong.
Then it starts Phantom js and logs loading / receiving resources. My test.ts file is not one of the recources listed but the site-wide .js is (I checked the site-wide file and my tests are being appended to it).
Finished test run for c:\......\test.ts in Discovery mode
Cleaning up test context for c:\......\test.ts
Chutzpah run finished with 0 passed, 0 failed and 0 errors
Chutzpah.json file cache cleared
End Test Adapter Discover Tests
chutzpah.json
{
"Framework": "jasmine",
"EnableTestFileBatching": true,
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ],
"Paths": [
{
"OutputPath": "../SiteWide.js",
"SourcePath": "Views"
}
]
},
"References": [
{
"Path": "../knockout-3.4.2.js",
"IsTestFrameworkFile": true
}
],
"Tests": [
{
"Includes": [ "*.ts" ],
"Path": "../Tests/Views"
}
],
"EnableTracing": true,
"TraceFilePath": "./trace.log"
}
tests.ts
describe('configuring unit tests for typescript!', () => {
it('this should pass', () => {
expect(1).toBe(1);
});
it('this should fail', () => {
expect(1).toBe(0);
});
});
There are a few things I'm suspicious of (the missing .js files line from the trace - but that might just be caused by my single js file compilation step?)
Maybe I'm missing references to jasmine in my chutzpah.json?
I'm at a loss for why the Jasmine tests work, but Chutzpah doesn't report back.
Maybe late...
But something like this in chutzpah.json would help.
{
"Framework": "jasmine",
"Compile": {
"Mode": "External",
"Extensions": [ "*.ts" ],
"ExtensionsWithNoOutput": [ "*.d.ts" ]
},
"References": [
{ "Path": "node_modules/promise-polyfill/dist", "Include": "*.js", "Exclude": "*.d.ts" },
{ "Path": "node_modules/systemjs/dist", "Include": "*.js", "Exclude": "*.d.ts" }
],
"Tests": [
{ "Path": "unittests", "Includes": [ "*.spec.ts" ], "Excludes": [ "*.d.ts" ], "ExpandReferenceComments": "true" }
]
}
Having your system related files is important in the references. Also you can try "*.spec.js" in the Tests section

Debugging Django in VSCode fails on imp.py

I am unable to debug my Django app. I am using virtualenv and have configured my VSCode workspace to point to the absolute path within my virtual environment for python.
"python.pythonPath": "/Users/Me/PyProjs/proj_env/bin/python"
When trying to debug, however, the editor jumps to the imp.py file (which is located at ~/proj_env/lib/python3.4) and fails at the new_module() method.
def new_module(name):
"""**DEPRECATED**
Create a new module.
The module is not entered into sys.modules.
"""
return types.ModuleType(name) #Editor breaks here.
Inspecting the name variable, I see it is set to "__main__". When stepping through, the editor exits debug mode and no errors or exceptions are logged in the Debug Console.
Anybody know what my issue could possibly be? I just want to debug my application!
You probably have stopOnEntry set to true in your launch.json. Try setting it to false:
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/manage.py",
"cwd": "${workspaceFolder}",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput",
"DjangoDebugging"
]
},
Looks like a possible defect, check the VS Code Python GitHub repo: https://github.com/DonJayamanne/pythonVSCode/issues/1092