Run another command regardless of the success of protractor test - node.js

Purpose:
I want to always run another node script no matter whether protractor tests are all passed or not. To make it easier, I used the following command:
protractor tests/protractor-config.js ; npm run uitest
Problem:
The output of the command above will be:
$ iris.web.portal#1.0.0 e2e E:\IRIS\IRIS\Iris.Cloud\Iris.Web.Portal
$ protractor tests/protractor-config.js ";" "npm" "run" "uitests"
Usage: protractor [configFile] [options]
configFile defaults to protractor.conf.js
The [options] object will override values from the config file.
See the reference config for a full list of options.
Understanding:
I think protractor CLI didn't handle semicolon properly.
When I try
protractor tests/protractor-config.js && npm run uitest
It works as expected.
My config
I was using protractor#5.3.2.
Have anyone met the problem before. Thanks!

The reason protractor tests/protractor-config.js ; npm run uitest failed was because protractor took ; npm run uitest as options to the command protractor.
The reason second one worked for you is because the tests passed; The && is the AND operator , so my conjecture is if your tests fail npm run ui tests will not run . because false && <anything else> will result in false & would not get evaulated.
Example : ls <non-existent directory && echo2 will result in ls: <non_existent directory name>: No such file or directory.
Hence if you want to run a task irrespective of whether protractor tests/protractor-config.js passes or not , you might want to have a look at https://docs.npmjs.com/misc/scripts.
Spefically : npm test, pretest & posttest

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 start node server before running end to end tests using npm run?

I'm writing end to end tests for an express site, and I want to add a "test" command into package.js
This command needs to:
run eslint
compile typescript
start node server
run unit tests against that server and show output.
once done testing, close the server.
I know how to execute all those commands individually, but not all at once.
What I have now is :
npm run compile && npm run build && node ./dist/server.js --db=test && npm run test
It works to the point of: "&& npm run test"
since node server is running, it won't continue on to the next command, and if it closes then tests wouldn't run.
Any help is appreciated.
One thing that I have found to help with reliable, maintainable end-to-end tests is to separate concerns:
Test suite assumes that the server is already running
Orchestrator calls into separate commands to bring up your test stack then run the tests
In CI, this could look like
npm start-e2e-test-stack --port=XXXX --db=test
npm test --port=XXXX --db=test
npm teardown-e2e-test-stack
In my experiences, having the end-to-end tests operate against any server helps to allow them to verify all environments, local, dev, qa, staging, production.

nightwatchjs Error occurred: "There was an error while starting the test runner:"

I install nightwatchjs globally using the 'npm install -g nightwatch' command. The version is 0.9.16.
Then I simply copy the default google.js (AppData\Roaming\npm\node_modules\nightwatch\examples\tests\google.js) to a separate folder location and executed it
Any idea why this is happening ?
Could be many things so will start with this:
1) why no --test in nightwatch --test google.js
2) assuming google.js is the test file, does it exist in the Sameera directory or is in examples/tests/?
If 2 is correct I would assume you need something like:
nightwatch --test examples/tests/google.js

Why does running `jasmine` after `jasmine init` and `jasmine examples` do nothing?

I have globally installed jasmine by running npm install jasmine -g.
Running jasmine -v gives me
jasmine v2.5.0
jasmine-core v2.5.0
I have then, as per the docs, run
jasmine init
jasmine examples
This created the expected /spec directory and the spec/support/jasmine.json file.
I am under the impression that if I now run jasmine I should see some test output in the console. Instead it simply thinks about it for a second and then does nothing.
I'm running node v4.5.0 on a Windows 7 machine in a Git Bash terminal. I've tried running it from the Windows cmd prompt as well but that doesn't work either.
well jasmine does run, but it doesn't report anything when you run jasmine alone. (you can confirm that by putting console.log inside describe functions and see that indeed it will log.)
download the latest release, it will have an html file that you can run which will do all the work for you.
https://github.com/jasmine/jasmine/releases
basically running jasmine requires a boot.js file for configurations. a jasmine-html.js file for the html reporter. you can figure out everything yourself by running the SpecRunner.html.
my personal preference is to use protractor and have the reporter configured in the protractor.config file.
if you want to run jasmine and have it run, you need to add your own boot.js and reporter, and loading them first thing before the spec in the jasmine.json file.
{
"spec_dir": "spec",
"spec_files": [
"boot.js",
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
It's a bug in jasmine
https://github.com/jasmine/jasmine-npm/issues/90
Use version 2.4
npm install -g jasmine#~2.4

Timing/performance metrics of npm scripts

Is there a way to measure the performance of an npm scripts similar to the way time-grunt works?
I am moving some of my critical build tasks to use npm instead of Grunt as writing my own build script is more flexible than using some of the Grunt plugins like grunt-browserify for example.
I have tried using console.time() but it finishes before the script is done, I assume because the methods are asynchronous. I also tried running the npm script as a Grunt task like this:
grunt.registerTask('collectify', function () {
grunt.util.spawn({
cmd: 'npm',
args: ['run', 'collectify:app']
});
});
But the output is different than if I run npm run collectify:app from the command line, perhaps because of pwd issues.
Coloured bars would be nice but at the very least I'd like to see the time in numbers.
Have you tried prepending the time command before your npm run command ?
So if your command is:
npm run collectify:app
It becomes:
time npm run collectify:app
And it'll output 3 lines e.g.
real 0m11.580s
user 0m7.400s
sys 0m1.304s
Let me know if it helps!
Your best option is likely pre[foo] and post[foo] scripts.
So if I have a NPM script called "foobar" then I can create a script called "preboofar" and "postfoobar" and they will be executed automatically before and after "foobar" is executed.
So in "pre" you can touch a file with a timestamp and in "post" you can read that file and calculate the difference.

Resources