Cannot view code coverage on angular-cli new project - unit-testing

I'm trying to figure out how to get code coverage working with #angular/cli but so far i'm not having much luck.
I started a new project using angular CLI. Basically all i did was ng new test-coverage and once everything was installed in my new project folder, I did a ng test --code-coverage. The tests were run successfully but nothing resembling code coverage was displayed in the browser.
Am I missing some dependencies or something else? Any help will be appreciated.
EDIT:
R. Richards and Rachid Oussanaa were right, the file does get generated and I can access it by opening the index.html.
Now i'm wondering, is there a way I could integrate that into a node command so that the file opens right after the tests are run?

here's what you can do:
install opn-cli which is a cli for the popular opn package which is a cross-platform tool used to open files in their default apps.
npm install -D opn-cli -D to install as dev dependency.
in package.json add a script under scripts as follows
"scripts": {
...
"test-coverage": "ng test --code-coverage --single-run && opn ./coverage/index.html"
}
now run npm run test-coverage
this will run the script we defined. here is an explanation of that script:
ng test --code-coverage --single-run will run tests, with coverage, only ONCE, hence --single-run
&& basically executes the second command if the first succeeds
opn ./coverage/index.html will open the file regardless of platform.

Related

Disable generation of cypress folders when running tests

I'm trying to do a bunch of unit tests with Cypress. Here's the npm script that runs them:
cypress run --project tests/unit/ --headless
When I run them, it generates the typical plugin/support/videos folders, but I don't need them. Is there any flag that disables the generation of these 3 folders when running the tests?
Thanks!
Just add these generated reports to a .gitignore file in the project's root like so:
# Cypress generated files #
######################
cypress.env.json
cypress.meta.json
cypress/logs/
cypress/videos/*
cypress/screenshots/*
cypress/integration/_generated/*
cypress/data/migration/generated/*.csv
cypress/fixtures/example.json
cypress/build/*
Now, these files will never be version-controlled.
You can also disable video recording with proper configuration in your cypress.json file like so: "video": false.
You can also do it with CLI by overriding your cypress.json.
Currently, there's no way to disable the generation of those files. However, you could remove them by when launching Cypress with an npm script like so:
"clean:launch:test": "rm -rf /cypress/movies && rm -rf /cypress/screenshots && cypress run --project tests/unit/ --headless"
Then you can run it like so: npm run clean:launch:test. It should remove those folders & launch Cypress's unit tests.
I suggest just adding them to .gitignore or configuring Cypress to trash them before each run. You can read about it here.
cypress.json file:
trashAssetsBeforeRuns: true
To disable the creation of video and screenshots folder you can do like in the following command.
cypress run --config video=false,screenshotOnRunFailure=false
To remove plugin/support folders I think they are not generated with current Cypress version so you can just remove them and add to .gitignore.
Video recording can be turned off entirely by setting video to false from within your configuration.
"video": false
https://docs.cypress.io/guides/guides/screenshots-and-videos#Videos

Testing - How to split up pre-commit unit tests and CI end-to-end tests

Scenario
I'm working on an app that has fast unit/functional jest tests along with slower end-to-end jest-puppeteer tests. I would like to split those up so that I can run the faster tests as part of a git pre-commit hook and leave the end-to-end tests to be run on CI after the code is eventually pushed to origin.
Question
How can I define specific tests to run at pre-commit? Specifically via regex similar to jest moduleNameMapper eg <rootDir>/__tests__/[a-z]+\.unit\.test\.js
Best idea so far:
in package.json add test:pre which uses bash find . -regex with bash for do to run desired "pre commit" tests
I've added
"test:pre": "PRE=1 npm test -- test/pre-*test.js"
# everything after -- is a kind of regex filter for matching file names
to my package.json scripts and in my jest-puppeteer global-setup.js I'm using
if(+process.env.PRE) return;
before all the puppeteer extras are started. So now I can
$ npm run test:pre
and violá

Appveyor build failure

I am creating a build with Appveyor on Github use devtool https://github.com/atom/atom-keymap. Although Travis builds success, Appveyor builds still appear error!
I do not know real root cause, but I think I can help with a way to troubleshoot this. Basically you can connect to AppVeyor VM via RDP and debug it. Here are the steps:
Insert - ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) before - npm run ci in your appveyor.yml file.
In RDP run the following:
cd c:\projects\atom-keymap
npm run compile
npm run lint
This will bring you to the state to get a repro and debug (because npm run ci is npm run compile && npm run lint && npm run test).
To get a repro npm run test.
To debug the problem, do something like this:
devtool --console node_modules/mocha/bin/_mocha --colors spec/helpers/setup.js spec/* --break
(this will let you debug step-by-step)
or
devtool --console node_modules/mocha/bin/_mocha --colors spec/helpers/setup.js spec/* --watch
(this will let you see a lot of error details)
This is the same what npm run test does, but without switch to quit on error and with debug options.
I went this route myself till this point but my limited knowledge of this npm module did not let me to dig till the root cause.

golang test for sub directories fails

In my use case I am setting up a single go test which runs all _test.go in all packages in the project folder. I tried to achieve this using $go test ./... from the src folder of the project
/project-name
/src
/mypack
/dao
/util
When I try to run the test it is asking to install the packages which are used in the imported packages. For example if I import "github.com/go-sql-driver/mysql", it might have used another package github.com/golang/protobuf/proto. I did not manually import the proto package. The application runs without manually importing the inner package. But when I run the tests it fails. But individual package test succeeded. Do I have to install all the packages in the $go test ./... error manually?
Could anyone help me on this?
You need to run go get -t ./... first to get all test deps.
From the go test -h:
The -t flag instructs get to also download the packages required to
build the tests for the specified packages.

Running jest tests directly in Intellij Idea/WebStorm?

I'm using jest to write tests in my ReactJS application.
So far, to run my test suite, I need to type 'npm test'.
Here's the snippet from package.npm:
"scripts": {
"test": "./node_modules/.bin/jest",
(other stuff)
},
"jest": {
"unmockedModulePathPatterns": ["<rootDir>/node_modules/react"],
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"testFileExtensions": [
"es6",
"js"
],
"moduleFileExtensions": [
"js",
"json",
"es6"
]
},
Is it possible to run those tests within my IDE (IDEA/WebStorm) directly, preserving the configuration? I'm not a js guy, but for example WebStrom works perfectly fine with Karma. Shouldn't this be possible with jest-cli either?
To make Jest test results shown in a tree view (like karma, etc.), a special integration is needed. WebStorm doesn't yet support Jest. Please vote for WEB-14979 to be notified on any progress.
EDIT: as of March 2017 the first version of Jest integration at WebStorm has been released.
In WebStorm 9+ You can set this up as follows:
Install Jest CLI: npm install --save-dev jest-cli
Create node run configuration with javascript file set to node_modules/.bin/jest, and application parameter to --runInBand. runInBand tells jest to run in single process, otherwise there's a port conflict when running multiple node processes in debug mode
Create some tests and run configuration in Debug mode (Ctrl-D/CMD-D). If you set breakpoints in your test or app code they should hit
It would be great though if you could click on file:line numbers in the output to go directly to the code.
app_sciences's answer is awesome, but does not work for Windows.
For windows, you can use next configuration:
Provided configuration is taken from here
For IDEA I'm using https://confluence.jetbrains.com/display/IDEADEV/Run+Configurations for that purposes. For WebStorm it seems you can add your config by yourself https://www.jetbrains.com/webstorm/help/creating-and-editing-run-debug-configurations.html . The configuration you are talking about is on the software level. If you will configure to run it via your IDE it will definitely will run within the ENV variables and paths given, you just need to add the needed global paths and the commands to run.