Debug hardhat solidity tests In WebStorm - debugging

After running Hardhat tests in the console with npx hardhat test I decided that being able to set break points would help me iterate faster.
How can I get Webstorm to run the underlying functions started by npx hardhat test so that I can use the built in Debugger?

I've since discovered that hardhat runs mocha under the hood.
To debug in WebStorm you can:
delete your existing configurations
create a new mocha configuration
set any configurations in 'Node options'. Note: since I'm forking the main net it takes a while for tests to start so I added the --timeout 10000 because mocha's default timeout is only 2000ms
select the mocha package, WebStorm doesn't select it by default
set your test file pattern
add const {ethers} = require('hardhat'); to your test file because it is no longer injected by hardhat during run time.
If the green debug icon does not appear I had success in closing and reopening WebStorm.
At this point I could successfully set break points in my test file but not in the MyContract.sol file. This is not surprising given that the contract is compiled before its run.

Create or open the package.json file for your Hardhat project.
Add a test NPM run script and save the file. Your package.json should look something like this.
{
"name": "hardhat-project",
"scripts": {
"test": "hardhat test"
},
"devDependencies": {
"#nomiclabs/hardhat-ethers": "2.0.2",
"#nomiclabs/hardhat-waffle": "2.0.1",
"chai": "4.3.4",
"ethereum-waffle": "3.4.0",
"ethers": "5.4.4",
"hardhat": "2.6.0"
}
}
In the left gutter of the editor pane, a little play icon should appear, click it and then click Debug "test".
I go through the instructions in a little more detail here, but this is the general idea. https://allendefibank.medium.com/how-to-debug-solidity-contracts-in-webstorm-hardhat-2ea0d3c4d582

If you're use typescript you need to import ts-mocha instead of mocha

Related

Cypress cucumber with custom directory structure - Unable to find the step defs

I need to support a custom directory structure on my project that uses Cypress for testing. As I am using cucumber preprocessor plugin to handle feature files (trying to work with ATDD approach but also I am using other plugins to manage accessibility and performance testing) I though why not try to split the directory structure this way
package.json
src/
testing/
---> acceptance/
---> accessibility/
---> performance/
---> cypress.acceptance.json
---> cypress.accessibility.json
---> cypress.performance.json
Where the cypress.X.json files are the Cypress configuration files that will act as the cypress.json for each testing folder.
To manage this I am adding some scripts to my package.json to make things easier
"scripts": {
...
"test:acceptance": "npx cypress run --config-file test/cypress.acceptance.json",
"test:performance": "npx cypress run --config-file test/cypress.performance.json",
"test:accessibility": "npx cypress run --config-file test/cypress.accessibility.json",
...
}
If we focus on the acceptance testing for a moment, the Cypress configuration (test/cypress.acceptance.json) looks something like this (yes I had to override all the defaults to make Cypress happy!)
{
"baseUrl": "http://localhost:3000/",
"video": true,
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"nonGlobalStepBaseDir": "test/acceptance/integration"
},
"testFiles": "**/*.feature",
"integrationFolder": "test/acceptance/integration",
"fixturesFolder": "test/acceptance/fixture",
"screenshotsFolder": "test/acceptance/screenshots",
"videoFolder": "test/acceptance/videos",
"pluginsFile": "test/acceptance/plugins/index.js"
}
From what I can tell this seems to be doable and in the docs https://github.com/TheBrainFamily/cypress-cucumber-preprocessor#configuration they give the options above in the "cypress-cucumber-preprocessor" where they also suggest the nonGlobalStepBaseDir should point at the same path as the integration folder (which I have done... except that all of this doesn't work. The error I am getting is the step definitions are not found (will post only one of the failed test for shortness, the others are the same)
Running: features/product-catalogue/product-catalog.feature (4 of 8)
Product Catalog
1) A customer is able to see the product catalog
2) The product catalog displays the navigation bar
3) The catalogue page view defaults to grid on mobile
0 passing (2s)
0 pending
3 failing
1) Product Catalog
A customer is able to see the product catalog:
Error: Step implementation missing for: products are available on the system
at Context.resolveAndRunStepDefinition (http://localhost:3000/__cypress/tests?p=test/acceptance/integration/features/product-catalogue/product-catalog.feature:12789:11)
at Context.eval (http://localhost:3000/__cypress/tests?p=test/acceptance/integration/features/product-catalogue/product-catalog.feature:12104:35)
Note that the internal directory structure used to work before when it was inside the traditional cypress/integration/features folder. At the end of the day I have just renamed things around and pushed one level down to test/acceptance/integration/features.
From what I can tell it's the plugin not able to get the base folder for the non-global step definitions, but I can't see why.
Any help would be highly appreciated, thank you

Cypress browser refreshes browser on changing test file

I am using Cypress for end to end testing, and I would like to be able to see all run test suites, in the browser, even after they are run. Currently, after each test suite is completed (test which are stored in separate files), the browser reloads and I cannot see previously run tests, and after the final test suite, the browser closes. Is there an option to change this behavior so that I can run all test files, have all the results visible in the browser and that the browser doesn't close at the end?
I am currently running tests using this command: ./node_modules/.bin/cypress run --headed --spec 'cypress/integration/tests/*'
where /tests is the folder where I currently have my files.
I have added --no-exit but in this case cypress doesn't move to the next test file and only the first one runs.
A workaround solution could be to generate reports with Mochawsome, for each Test Spec, and then merge and view those rendered reports. The reports will contain the results from the tests, test bodies, any errors that occurred and some other bits of information.
If you read through the page in the link it shows you how to generate individual reports then combine them together, and then render them as HTML. These can then be viewed in the browser.
This command can be used to install what's needed npm install --save-dev mochawesome mochawesome-merge mochawesome-report-generator
and then add the Reporter configuration to the cypress.json:
{
"reporter": "mochawesome",
"reporterOptions": {
"reportDir": "cypress/results",
"overwrite": false,
"html": false,
"json": true
}
}
Keep in mind that it may not give you the level of detail that is contained in the Cypress Dashboard in the browser, for example, what was yielded from a request.
Cypress has a lot of possible command with a lot of possible config too.
Read this.
And if you use npm just run like this :
npm run cypress:open
and in your package.json :
"scripts": {
"cypress:open": "cypress open"
}

How to debug a resolvejs application using VSCode

I'm using Visual Studio Code.
I just created a resolvejs application.
How to run the application step by step from vscode ?
Here is my debugging setup (in .vscode\launch.json):
{
"type": "node",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/run.js",
"args": [
"dev"
],
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
"runtimeArgs": [
"--nolazy",
"--inspect"
]
}
My breakpoints in my command handlers do not work.
Although file run.js is some kind of entry point in resolve framework-based application, it is not actual entry point for runtime phase. When resolve application is being launched by yarn dev / npm run dev command, in fact two actions are performed: building bundle using run.js (which by default uses config.[MODE].js files for appropriate mode) and launching target bundle in separate process with watch mode, which causes automatic rebuild on every file change in project (aka hot reload).
Bundle is stored at <APP_DIR>/dist/common/local-entry/local-entry.js. To allow debugging from IDE, spawned debug nodejs should invoke target bundle directly. It can be reached by separating application compile- and runtime phases.
Resolve includes command for application building without run it - yarn build / npm run build. This command builds application in production mode, but this behavior can be easily modified to development mode by editing run.js entry point - just find line case 'build': and replace following line from await build(merge(baseConfig, prodConfig)) to await build(merge(baseConfig, devConfig)). Or additional script for building app in dev mode can be appended, like yarn build-dev - amount of possible scripts is unlimited.
Then just run command yarn build and start debugger with pointing entry point as ${workspaceFolder}/dist/common/local-entry/local-entry.js. If source maps had built right, breakpoints should work fine.

Coverage in WebStorm with Mocha doesn't work

I followed this guide to get coverage report integrated in WebStorm.
https://www.jetbrains.com/help/webstorm/mocha.html
When I click "Run Mocha test with coverage" the coverage window shows up after running the test. But I don't see any coverage reported. It is not even showing 0%, it is not showing any numbers at all in the Statistics column.
Any help will be appreciated.
Some more background:
WebStorm 2017.2.3
I'm referring Mocha v3.2.0 in package.json
The code is written in ES2015.
The mocha Run configuration uses these environment variables
BABEL_ENV=test and these extra mocha options: --compilers
js:babel-core/register
Thanks in advance
Further information: when I run with coverage in WebStorm I see this command being output to the console:
/Users/nikolaschou/.nvm/versions/node/v8.1.2/bin/node /Users/nikolaschou/Dev/dsg/bifrostshop/node_modules/nyc/bin/nyc.js --reporter=lcovonly --extension=.ts --extension=.js --exclude=test/unitmocha//.spec. --exclude=test/unitmocha//.test. --report-dir=/private/var/folders/q7/kn0zjzks5dz0q2bx0kpg2yhw0000gn/T/mocha-intellij-coverage- /Users/nikolaschou/Dev/dsg/bifrostshop/node_modules/mocha/bin/_mocha --compilers js:babel-core/register --ui bdd --reporter "/Applications/WebStorm 2.app/Contents/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js" --recursive /Users/nikolaschou/Dev/dsg/bifrostshop/test/unitmocha
Add a file called .nycrc in the project root with these contents:
{ "exclude": [] }
This overrides nyc default config and win is restored.
In my case I had this problem because mocha did not exit properly. As the coverage reports seem to be generated on program exit, they are never generated.
Mocha 4 especially has changed behaviour from Mocha 3 as it does "no longer automatically kill itself via process.exit() when it thinks it should be done running".
However you can force that old behaviour with the --exit option.
The better option is probably to examine what keeps your program from terminating and perform a clean shut down. (Maybe with some help of wtfnode for example)
Readings:
Mocha Won't Force Exit
mocha 4 doesn't exit unlike mocha 3

Have npm watch files in background to enable integration with Visual Studio Task Runner

I am trying to have npm watch for file changes in my client side code. I can have it work from the command line ok but I'd really like it to work from Visual Studio task runner.
my package.json looks something like this:
...
"watch": {
"compile": {
"patterns": [
"wwwroot"
],
"extensions": "ts"
}
},
"scripts": {
"prebuild": "copyfiles -f ./node_modules/d3/build/*.js ./wwwroot/lib/d3",
"compile": "tsc && browserify ./wwwroot/app/app.js -o ./wwwroot/bundle.js",
"build": "npm run compile",
"watch": "npm-watch"
},
...
So running npm run build then npm run watch from the command line has everything running as expected.
When I use the Task Runner however to bind my watch script to the After Build event like this:
it never 'exits' and, as such, the build never completes* and Visual Studio waits for me to kill the task before the application runs. Is there a way to have the script run in the background or some kind of 'detached' mode to enable Visual Studio to complete the build and run the application?
I tried using different watch tools but can't find any such option in any of them.
*Actually, I guess the build itself probably DOES complete but VisualStudio never runs the app and just hangs waiting on the script exiting.
Change the watch Bindings to Project Open. Then it will start watching after the project is opened.

Resources