husky pre-commit yarn/npm test does not react to keybord input - husky

I have simple app create by create-react-app so my yarn test runs react-scripts test.
When i run it from console I see standard output
No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press q to quit watch mode.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press Enter to trigger a test run.
And it properly reacts to pressed key. However when i create husky pre-commit hook
. "$(dirname "$0")/_/husky.sh"
yarn test
i see the same output in console, but this time it doesn't react to press. When I press any button it is just printed in console and Im unable to proceed. Any idea how to force husky to properly run Jest tests? Thx in advance.

the problem is the yarn test command by default runs in interactive mode to solve this i recommand to add annother command to your package.json file like this
"scripts": {
...
"test:exit": "react-scripts test --force Exit"
...
}
and in husky instead of yarn test use yarn test -- --watchAll=false

Related

Run E2E tests in IDE or command line

I'm using Stencil.js to create a web component library and I'm heavily relying on E2E tests. As they're rather slow it becomes more and more cumbersome to run the entire test suite (using the Stencil.js CLI) while developing new components.
However, I'm not able to run single tests in my IDE (IntelliJ IDEA) or via command line. It works perfectly fine for unit tests though.
My Jest config looks like this:
module.exports = {
"roots": [
"<rootDir>/src"
],
"preset": "#stencil/core/testing"
}
When I try to run tests in a single file (jest --config jest.config.js --testPathPattern src/components/button/button.e2e.ts$)
it fails, because
newE2EPage() is only available from E2E tests, and ran with the --e2e cmd line flag.
newE2EPage() comes with Stencil.js and I don't know what Stencil.js's CLI does in the background. Furthermore, I cloned the Stencil.js repository, just to see if it is working with their E2E tests (https://github.com/ionic-team/stencil/tree/master/test/end-to-end) but it doesn't work either.
Any idea how I can configure Jest so that it's able to run Stencil.js-E2E tests from the command line?
The --e2e flag is used for the npm script in the package.json. To start e2e tests, you can add this in your package.json:
"scripts": {
"test:e2e": "stencil test --e2e"
}
And run npm run test:e2e. For a specific file, you add it at the end like this:
npm run test:e2e src/components/button/button.e2e.ts
For more info, see the StencilJS doc: https://stenciljs.com/docs/end-to-end-testing
i have the same problem. IntelliJ and 'Run' single 'it' didnt work.
newE2EPage() is only available from E2E tests, and ran with the --e2e cmd line flag.
when i run 'npm run test' everything will work fine. the difference is that npm run stencil before and only jest dont work.
here is the stencil jest dir https://github.com/ionic-team/stencil/tree/master/src/testing/jest aswell a config.
i found in here https://stenciljs.com/docs/testing-overview a VS-CODE run jest code but no Intellij setup.
im on the run to get the path of the current file to run stencil via npm and the path the e2e file. but i cant find the correct variable for the run config.
i hope we got this solved soon.
cheers
I am not a VS Code user, but in contrast to IntelliJ there is a launch.json for VSC to run single tests: https://github.com/ionic-team/stencil-site/pull/480

How to use Internjs in watch mode

I'm setting up my test environment and I want to use Internjs to peform my unit tests. I'm using typescript and when I type npx intern my tests are executed correctly.
But I can't configure Internjs in watch mode and the doc's didn't gave any help.
Someone can help me to configure Intern to run the unit tests when any change in my files are made?
There isn't currently a built-in watch mode in Intern.
You can implement a custom watcher using chokidar (install chokidar-cli for the command line tool). Run tsc --watch to rebuild your code as necessary, and use chokidar to watch for changes and re-run intern:
$ chokidar '**/*.js' -c 'npx intern' -d 1000

WebStorm debugger does not attach on first time

When debugging a Node.js script with WebStorm, this is what my script looks like (windows):
"debug": "npm run build && node %NODE_DEBUG_OPTION% ./dist/server.js"
In the console I can see that npm run build executes as usually with success. Then there is a red message that states the following
Debugger listening on ws://127.0.0.1:51311/0064d764-16b8-4b45-bdff-5a1732e9ee62
For help see https://nodejs.org/en/docs/inspector
I can wait as long as I want - WebStorm won't attach and the ./dist/server.js script is never executed. When I stop execution and re-run the script, it succeeds and the debugger attaches.

What is the test command while creating package.json?

While creating package.json from command line using npm init for creating a module in Node.js, there is a test command field that I don't know about. There's no mention of it in the docs too on executing npm help json also in the CLI.
Please explain what it is about.
The test command is the command that is run whenever you call npm test.
This is important when integrating with continuous integration/continuous deployment tools (such as jenkins, codeship, teamcity).
Example:
- say you deploy a project to AWS or some other cloud hosting provider,
- you can set up your infrastructure to automatically run npm test.
- If there are problems within those tests, your ci/cd will automatically rollback before deploying.
To execute tests
You can use karma, jest, or selenium/nightmare/phantomjs or about any other test scripting library/framework that allows you to write and execute tests and then set the required command in scripts.test and finally run it from npm test.
Assuming you mean scripts.test:
"scripts" : {
"test" : "echo \"Error: no test specified\" && exit 1"
}
This field contains the program(/command line) that should run when you call npm test. Typically, that program is a test-runner like mocha, ava, jest, ...
The default value is a placeholder that prints an error message (try running npm test in the same directory as your package.json).

Why when test fails jenkins still says success?

I have a MEAN project. Using Jenkins on an EC2 machine I build this using the following shell script:
npm install && PORT=8888 npm test
mocha returns 2 (number of failing tests) but still jenkins says:
Finished: SUCCESS.
If tests are failing I expect to see
Finished: FAILURE
Do you know why its not working fine?
You can:
Use a test runner like Karma, or
Tell Mocha to report in, for example, XUnit format, by passing Mocha the --reporter xunit flag. XUnit closely aligns with JUnit which Jenkins understands, or
Add in a custom reporter — mocha-jenkins-reporter is a decent option.
In the end I used a different solution: installed Jenkins Text Finder and if "expected - actual" is found in log (test failed), I let this plugin to mark the build as "Unstable".

Resources