How to run tests with gruntJS - node.js

So, how do I run the test suite in gruntjs? I thought it should be pretty simple and straight-forward but well, for me it wasn't :)
Since gruntjs should be able to run tests I thought that I could just run "grunt test", but that for some reason requires the server to run. I tried starting it in another process with "grunt server watch" but then again the "grunt test" fails.
How is grunt supposed to work?

'grunt test' is a task that starts server too when required. It starts a test runner, for example karma and karma runs the tests in browser environment.
The trick is to run karma and provide configuration items in karma.conf.js
A sample gruntfile on gruntjs website shows how to make qUnit work with grunt.

Related

My jest tests run fine separately but fail when run all routes together

I'm making an API using TypeScript, node.
I'm using jest to make some tests
I have two different routes, "/users" and "/authentication", when I run one route at a time it runs ok, but when I run all tests together one of my tests returns me a 500 status code.
I'm running it in node v16.17.0
jest: v29.3.1
When I run $npm run test users
When I run $npm run test authentication
When I run $npm run test
Thats my test scipts:
"test:load-envs": "dotenv -e .env.test", "test": "npm run test:load-envs npx jest",
I've tested it manually throught thunderClient and the error case runs fine...
On thunderClient trying to emulate the error:
I searched for someone with a similar error but I couldn't find an answer to my problem.
I can ignore this since it's a personal project, but I really would like to know how to solve it

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

Angular cli, overwrite the command that gets run by 'ng e2e'?

I'm trying run Angular's e2e tests against an instance of the application ON A DIFFERENT SERVER than my local machine.
So to be clear, I'm not testing my local code.
I just need to run protractor without the angular build steps because it's a waste of time since the code I'm testing is on another server. Unfortunately, the angular.json file throws an error if i excessively modify/remove the following line:
"builder": "#angular-devkit/build-angular:protractor",
I already have a solution for this, but it's long winded and I'd like to be able to not change how my teammates are running tests from their shells:
node node_modules/protractor/bin/protractor e2e/protractor.conf.js
I have two thoughts:
Write npm script which runs this command (what i'll likely end up doing)
Find out how to overwrite what ng e2e does. If I can run the more complicated command here, it'll save productivity and feedback time.
I'm on Angular V7.
Is overwriting ng e2e so that it executes node node_modules/protractor/bin/protractor e2e/protractor.conf.js instead possible?
Yup. I would do #1. That makes sense to update your package.json
"scripts": {
"protractor": "protractor e2e/protractor.conf.js"
}
and then just run npm run protractor. The e2e command is also downloading chromedriver, the selenium jar file, and maybe geckodriver? with webdriver-manager. If you want that as a pre-step:
"scripts": {
"protractor": "protractor e2e/protractor.conf.js",
// just download chromedriver and the selenium jar
"preprotractor": "webdriver-manager update --gecko false"
}
It also starts your angular application. If you need to do that, I would just call ng serve and run it in a background process. I hope that helps.

How to run a 'watch' script along with a 'start' script in my node.js project

I'm writing an app that is composed of microservices (I use micro).
I really like es6, so I use Babel to make the development process easier. The problem that I have is that I need a script that would compile my es6 code and restarted the 'server'; I don't know how to achieve this.
Right now I have the following script in my package.json:
"scripts": {
"start": "yarn run build && micro",
"build": "./node_modules/.bin/babel src --out-dir lib"
},
When I run yarn start my es6 code compiles successfully and micro starts the server. However, if I make changes to my code, I'll have to manually stop the server and run yarn start again.
I've tried to change my build script
"build": "./node_modules/.bin/babel src --watch --out-dir lib"
But in this case the micro command does not get executed as the build script just watches for changes and blocks anything else from execution. My goal is to have a script that would watch for changes and restart the server if a change occurred (compiling the code beforehand) like in Meteor.
One option is using ParallelShell module to run shell commands in parallel. You can find an example of how to use it here
The simplest solution would be to yarn run build & micro (note the single & and not &&).
As mentioned by others, parallelshell is another good hack (probably more robust than &).

How to test express api on Travis

As mention in a previous post, the following lines only works on local machine for testing.
node app.js
karma start karma.conf.js --single-run
If I put it into the .travis.yml, it make Travis hanging on the "node app.js" line and fail the test after timeout. I think this is because node is supposed to start the app.js and keep listening. On the local machine, I can open 2 terminal windows and run them separately but I am not sure how to do it on Travis. Can anyone help?
node app.js & will make Travis run app.js in the background (this should also work locally). The "&" bit is a standard piece of shell syntax to make a process run in the background.

Resources