GitHub Action [cypress CI integration ]: How to set env variables before running cypress run command - javascript

I am writing workflows for my repository, and I want to run test cases whenever there is a code push.
Here is the code snippet from .yml file:
- name: Run cypress test
with:
env: true
env:
username: ${{secrets.CYPRESS_USERNAME}}
password: ${{secrets.CYPRESS_PASSWORD}}
run: npm run cy:test --env fileConfig=production, username=$username, password=$password
continue-on-error: false
Snippet from JSON :
{
"env": {
"userId": "1",
"environment": "production",
"baseUrl": " base URL"
}
}
So, I want to pass the username and password along with the configfile in the cypress run command so that they can be set as the env variable because I am using username and password for my login test module.
With the above code i am getting error :
The workflow is not valid. .github/workflows/main.yml (Line: 43, Col: 9): Unexpected value 'run' .github/workflows/main.yml (Line: 36, Col: 9): Required property is missing: uses
Thanks :)

First, you need to add the env variables as empty strings on cypress.json:
{
"env": {
"userId": "1",
"environment": "production",
"baseUrl": "base URL",
"username": "",
"password": ""
}
}
In main.yml file, you need to add the CYPRESS_ prefix to the variables:
env:
CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}
Also, you should use the setup that can be found on cypress docs (https://docs.cypress.io/guides/continuous-integration/github-actions#Basic-Setup), example:
name: Cypress Tests
on: [push]
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
# Install NPM dependencies, cache them correctly
# and run all Cypress tests
- name: Cypress run
uses: cypress-io/github-action#v2
with:
build: npm run build
start: npm start
env:
CYPRESS_username: ${{secrets.CYPRESS_USERNAME}}
CYPRESS_password: ${{secrets.CYPRESS_PASSWORD}}
You should also read this article: https://glebbahmutov.com/blog/keep-passwords-secret-in-e2e-tests/

Related

StyleLint throws error when running in Github action

I have been trying to deep dive into the problem, but I cannot figure out where everything is going wrong. As you can see below is where the error is happening for this specific rule https://stylelint.io/user-guide/rules/declaration-block-no-redundant-longhand-properties/. Any advice and help would be greatly appreciated :).
node_modules/stylelint/lib/rules/declaration-block-no-redundant-longhand-properties/index.js:117
.map((p) => transformedDeclarationNodes.get(p)?.value.trim())
^
SyntaxError: Unexpected token '.'
my linter config file
{
"extends": "stylelint-config-recommended-scss",
"plugins":["stylelint-prettier"],
"rules": {
"prettier/prettier": true,
"scss/dollar-variable-pattern": null,
"block-no-empty": null,
"rule-empty-line-before":["always",{"ignore":["first-nested"]}],
"scss/selector-no-redundant-nesting-selector": true,
"font-family-no-missing-generic-family-keyword": [true,{"ignoreFontFamilies":["MAIN_THEME"]}],
"color-function-notation": "legacy",
"alpha-value-notation": "number",
"scss/dollar-variable-colon-space-after": "always",
"scss/dollar-variable-colon-space-before": "never",
"unit-allowed-list": ["px","vmin","em","rem","deg","%","vh","vw","s"],
"declaration-block-no-redundant-longhand-properties":[true, {
"severity": "warning"
}],
"selector-class-pattern": "^.*$",
"number-max-precision":2,
"keyframes-name-pattern": "^[a-zA-Z]+?(?:-+[a-zA-Z]{2,10}){1,3}$"
}
}
my CI action file
name: something
on: [pull_request]
jobs:
buildnode:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: '12.x'
- name: install modules
run: npm install
- name: run scss linter
run: npm run styleLinter
- name: run js linter
run: npm run linter
- name: build web app
run: npm run build
- name: test the code
run: npm test
Node version is out of date for styleLint so I updated to 18 that resolved the issue.

dataSource flag in typeorm cli

I'm starting a project with typeorm. My configuration file looks like this:
ormconfig.js
import dotenv from 'dotenv'
dotenv.config();
const {
DATABASE_HOST,
DATABASE_PORT,
DATABASE_NAME,
DATABASE_PASSWORD
} = process.env
export default {
type: 'postgres',
host: DATABASE_HOST,
port: DATABASE_PORT,
database: DATABASE_NAME,
password: DATABASE_PASSWORD,
entities: [
"./api/modules/documents/entity/*.js",
],
migrations: [
"./api/database/migrations/*.js",
],
cli: {
migrationsDir: "./api/database/migrations",
entitiesDir: "api/database/modules/**/entity"
}
}
package.json scripts
"scripts": {
"gulp": "node_modules/gulp-cli/bin/gulp.js",
"start": "nodemon ./src/app.js",
"start:api": "nodemon ./api/server.js",
"typeorm": "node ./node_modules/typeorm/cli.js --config ./ormconfig.js"
},
When i try to run the command yarn typeorm migration:run its show this message:
Runs all pending migrations.
Opções:
-h, --help Exibe ajuda [booleano]
-d, --dataSource Path to the file where your DataSource instance is defined.
[obrigatório]
-t, --transaction Indicates if transaction should be used or not for
migration run. Enabled by default. [padrão: "default"]
-v, --version Exibe a versão [booleano]
Missing required argument: dataSource
What would be datasource flag?
I tried with typeorm cli installed globally, same problem happens
Looks like you are using typeorm 0.3.x in your project while you are following 0.2.x configurations. Check your installed version on your package.json.
If you are using 0.3.x and want to keep with the current version you should follow the documentation: https://typeorm.io/data-source
Otherwise you can just downgrade your version executing:
npm install typeorm#0.2.45

How to set up configuration of Type ORM with .env file in Nest.JS

I want to set up configuration of TypeORM with .env file, but i have problem when I am trying to run migration script.
What i did:
1.Added scripts to package.json
"migration:generate": "node_modules/.bin/typeorm migration:generate -n",
"migration:run": "node_modules/.bin/typeorm migration:run",
"migration:revert": "node_modules/.bin/typeorm migration:revert",
2.Imported TypeOrmModule in app.module*
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('HOST'),
port: +configService.get<number>('PORT'),
username: configService.get('DATABASE_USERNAME'),
password: configService.get('DATABASE_PASSWORD'),
database: configService.get('DATABASE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
})
}
)],
3. Creaded .env file in root folder
HOST=localhost
PORT=5432
DATABASE_USER=dbuser
DATABASE_PASSWORD=dbpassword
DATABASE=dbname
now I am trying to run migration script like this:
npm run migration:generate -n AddUserTable
and I reciving error like this:
Error during migration generation:
Error: No connection options were found in any orm configuration files.
According to documentation it shuld be some ormconfig.json but it should also working with .env. Please tell me, what is wrong in my case?
Regarding the error message, you should add ormconfig.json in the root project. The .env file does not relate in this case.
Check import ConfigModule.forRoot(). It should be imported first
You can not use forRootAsync for TypeOrmModule if you use these variables in the env file
TYPEORM_CONNECTION = postgres
TYPEORM_HOST = localhost
TYPEORM_PORT = 5432
TYPEORM_USERNAME = postgres
TYPEORM_PASSWORD = 12345
TYPEORM_DATABASE = postgres
TYPEORM_SYNCHRONIZE = false
TYPEORM_MIGRATIONS_RUN = false
TYPEORM_ENTITIES = src/modules/**/*.entity.ts
TYPEORM_MIGRATIONS = db/migrations/*.ts
TYPEORM_LOGGING = true
https://github.com/typeorm/typeorm/blob/master/docs/using-ormconfig.md#using-environment-variables
You can also try such scripts:
"migration:run": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:run",
"migration:revert": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:revert",
"migration:create": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:create",
"migration:generate": "ts-node --project ./tsconfig.json -r tsconfig-paths/register node_modules/typeorm/cli.js migration:generate"
I was having the same problem. I solved it by following the config instructions in this article, which shows you how to generate a dynamic ormconfig.json file:
https://medium.com/#gausmann.simon/nestjs-typeorm-and-postgresql-full-example-development-and-project-setup-working-with-database-c1a2b1b11b8f
The extra setup is a bit annoying, but it works.
Just a note if you are using typescript - the article is from 2019 and per 2020 updates to nestjs, you'll want to change the src paths to allow for src or dist in the config.service.ts file, eg. change to something like this (depending on your file structure):
entities: ['**/*.entity{.ts,.js}'],
migrationsTableName: 'migration',
migrations: ['src/migration/*.ts'],
cli: {
migrationsDir: 'src/migration',
},
to
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
migrationsTableName: 'migration',
migrations: [__dirname + '/../migration/*{.ts,.js}'],
cli: {
migrationsDir: 'src/migration',
},

Cypress: Can't use --env variables from command line in test file

In my Cypress setup I have the following values (plus others) in my cypress.json file:
{
"PREPROD": {
"url": "https://preprod.com",
"username": "admin",
"password": "admin"
},
"PROD": {
"url": "https://prod.com",
"username": "admin",
"password": "admin"
}
}
I have my test file located in cypress/integration/test.js
Based on what environment I want to run my test, I'd like to use the values relevant to that environment in my test file
I've tried running like this:
npx cypress run --env environment="PREPROD" --spec "cypress/integration/test.js"
This returns an error saying that environment is not defined.
In my test file I have the following to use the variable from command line:
Cypress.config().environment.username
How do I go about using the variables that I define in command line?
I've tried reading the documentation here but its not working for me: Cypress Documentation
According to the documentation, you can access the environment variable like this:
Cypress.env('environment')
If you want to get the options depending on this you could do as follow:
const CypressConfiguration = require('cypress.json')
const environment = Cypress.env('environment') || 'PREPROD'
const options = CypressConfiguration[environment]
One other approach would be to use the config-file option:
cypress.prod.json
{
"url": "https://prod.com",
"username": "admin",
"password": "admin"
}
cypress.preprod.json
{
"url": "https://preprod.com",
"username": "admin",
"password": "admin"
}
Then:
npx cypress run --config-file cypress.prod.json --spec "cypress/integration/test.js"

Run single test of a specific test suite in Jest

I have a file called "test-file-1", within it i have a few describes (test suites) with unique names and inside them i have tests which may have similar names across test suites.
To run a single test suite or test i type the command below.
npm test -- -t "Test Suite or Test"
I'm trying to figure out how to run a single test of a specific test suite.
Give the path of the file you want to test :
npm test path/of/your/testfile.js -t "test name"
will run only the test (or group) named "test name" of this specific file.
After trying a lot of different things i found out that it was simpler than i thought.
You simply have to put the test after the test suite in the same string:
npm test -- -t "<Test Suite> <Test>"
import LoggerService from '../LoggerService ';
describe('Method called****', () => {
it('00000000', () => {
const logEvent = jest.spyOn(LoggerService , 'logEvent');
expect(logEvent).toBeDefined();
});
});
run below command
npm test -- tests/LoggerService.test.ts -t '00000000'
Point to the test suite in jest config file via "testMatch":["**/path/testSuite/*.js"]
and now execute by giving unique testname by jest testName
Here you have to update the testMatch every time which is your testSuite
If you are using a jest config file, you can use the testNamePattern key to configure this. So your config file could look something like
jest-e2e.json
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".spec.ts$",
"testNamePattern": "login",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"bail": true,
"verbose": true,
"testTimeout": 30000
}
This will only run test that has "login" in one of the test pattern
Reference:
https://jestjs.io/docs/cli#--testnamepatternregex
For me, it only works with:
npm run test -- tests/01_login.js --testcase "Should login into Dashboard"
npm run <script> -- <test suite path> --testcase "<test case>"
my script in package.json:
"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",
at nightwatch version 1.3.4

Categories