cypress: pass environment variables from command line with npm run cy:open - testing

In cypress.json there is one env variable:
"env": {
"AUTH_TOKEN": "token_1"
},
I have multiple users in db and would like to test them separately without editing cypress.json. Cypress documentation provides two possible ways how to override env variables form command line:
cypress run --env AUTH_TOKEN="token_2"
and
AUTH_TOKEN="token_2" cypress run
When I run cypress interface with
npm run cy:open --env AUTH_TOKEN="token_2"
or
AUTH_TOKEN="token_2" npm run cy:open
token_1 does not get overriden with token_2. Why is cypress ignoring options provided in command line?

npm run requires a -- to pass parameters to script cy:open in package.json,
npm run cy:open -- --env AUTH_TOKEN="token_2"
or use yarn
yarn cy:open --env AUTH_TOKEN="token_2"
or bypass the script
yarn cypress open --env AUTH_TOKEN="token_2"
or
npx cypress open --env AUTH_TOKEN="token_2"

Related

Using command line arguments in npm run script

I am trying to change the {variable} part in my custom-defined npm run deploy script
"scripts":{
"deploy": "npm run build && scp -r ./public example#192.200.0.11:/home/{DIRECTORY}/index.js",
}
I want to run it like npm run deploy --DIRECTORY:project99
You can pass arguments to npm run as Environment variable. See Npm Docs
"scripts": {
"deploy": "npm run build && scp -r ./public example#192.200.0.11:/home/${NPM_CONFIG_DIRECTORY}/index.js"
},
This should work
npm run deploy --DIRECTORY=project99

WebdriverIO how to specify the test to run from npm command

I am using WebdriverIO.
I start my tests from the command line using 'npm test', which triggers the command: wdio wdio.conf.js as specified in my package.json here:
"scripts": {
"test": "wdio wdio.conf.js"
},
What I want to do is specify the actual test to run like this:
wdio wdio.conf.js --spec ./test/specs/e2e/login.js
My question is, how do I pass in the testcase from the npm command, i.e. how can I pass this --spec ./test/specs/e2e/login.js from the npm command into the wdio command?
You can do something like this.
npm run test -- --spec ./test/specs/e2e/login.js
Check this out. https://stackoverflow.com/a/14404223/3879644

When I run cypress as npm run cypress run build fails

I have installed cypress using npm as npm install cypress --save-dev .
I used the same command in .gitlab-ci.yml file
When i run the command npm run cypress run locally , IDE opens and when i double click the spec.js file , then the tests run.
But I i use the same command on the gitlab pipeline , it says
cypress open "run"
It looks like this is your first time using Cypress: 4.1.0
[07:45:16] Verifying Cypress can run /osmc/ux/framework-acceptance-tests/cache/Cypress/4.1.0/Cypress [started]
[07:45:18] Verifying Cypress can run /osmc/ux/framework-acceptance-tests/cache/Cypress/4.1.0/Cypress [completed]
Opening Cypress...
and build fails .
Am i missing anything here ?
It's because it's opening the test runner, which is used locally via npx cypress open.
From that output it looks like you're running npx cypress open run, which isn't a real command and will open the runner
In CI you need to use npx cypress run, which will run tests without user interaction. https://docs.cypress.io/guides/guides/command-line.html#How-to-run-commands
Your gitlab.yml file should accomodate for the npm installations.
One example is as below. Meanwhile also please check the test/run command for your specs under package.json file. use the same command to trigger the test in pipeline.
stages:
- test
test:
image: cypress/browsers:node12.14.1-chrome85
stage: test
script:
npm i
npm run start:ci &
npx cypress run
You should have your .gitlab-ci.yml with:
stages:
- test
cypress-test:
image: cypress/browsers:node16.14.0-slim-chrome99-ff97
stage: test
script:
- npm ci
- npx cypress run

Gitlab ci config for Cypress tests on Svelte SSR (Sapper)

New to Sapper, the SSR version of Svelte.
I try to get the tests from the basic template to run in Gitlab.
My test job in the .gitlab-ci.yml looks like this:
test-job:
stage: test
image: cypress/base:10
script:
- npm install --save-dev cypress
- npm i serve
- serve -s build -l 3000 & yarn wait-on http://localhost:3000
- $(npm bin)/cypress verify
- $(npm bin)/cypress run
When I tried - npm run cypress run after - npm install it could not find the server. Then i tried to install serve but I still get an error:
$ serve -s build -l 3000 & yarn wait-on http://localhost:3000
/bin/bash: line 96: serve: command not found
Thanks for any pointers.

Mocha command is not found after adding in environment Variables in CodeBuild

In my buildspec.yml file I have a post-build command that runs my mocha tests:
npm run mochatest
That is something I have set in package.json as follows:
"scripts": {
"mochatest": "mocha --timeout 30000 test/functional_api_crud.js"
},
CodeBuild runs and it starts mocha and then I had a test failure because an environment variable I used in my Node.js code was not set. So, I went into the advanced settings of CodeBuild and added in the needed environment variables. Now when the run happens I get an error that mocha cannot be found! The error lines are:
[Container] 2017/12/28 19:24:29 Running command npm run mochatest
newswatcher#0.0.1 mochatest /codebuild/output/src251232826/src
mocha --timeout 30000 test/functional_api_crud.js
sh: 1: mocha: not found
npm ERR! Please include the following file with any support request:
npm ERR! /codebuild/output/src251232826/src/npm-debug.log
This started happening after I added in my own environment variables! Did some other environment variable get upset because I did this?
It turns out that I had set the NODE_ENV environment variable to production and thus, an npm install does not bring in my devDependencies modules!