Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update test setup of e2e and integration #1651

Merged
merged 9 commits into from
Oct 15, 2024
Merged
14 changes: 7 additions & 7 deletions .github/actions/e2e/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ runs:
- name: Set up Node.js version
uses: actions/setup-node@v4
with:
node-version-file: './e2e/.node-version'
node-version-file: './tests/e2e/.node-version'
cache: 'npm'
cache-dependency-path: './e2e/package-lock.json'
cache-dependency-path: './tests/e2e/package-lock.json'

- name: Install e2e code-dependencies
shell: bash
working-directory: ./e2e
working-directory: ./tests/e2e
run: npm ci --omit=optional --no-fund --no-audit

- name: Lint e2e code
shell: bash
working-directory: ./e2e
working-directory: ./tests/e2e
run: npm run lint

- name: Setup environment variables
Expand Down Expand Up @@ -54,7 +54,7 @@ runs:

- name: Install runtime dependencies
shell: bash
working-directory: ./e2e
working-directory: ./tests/e2e
run: npx playwright install chromium --with-deps

- name: Wait for IBF API-service to be running
Expand All @@ -67,7 +67,7 @@ runs:

- name: Run end-to-end tests
shell: bash
working-directory: ./e2e
working-directory: ./tests/e2e
env:
DASHBOARD_URL: http://localhost:4200
run: npm test
Expand All @@ -77,7 +77,7 @@ runs:
with:
name: test-result-artifacts
path: |
./e2e/test-results/
./tests/e2e/test-results/
./interfaces/IBF-dashboard/dashboard-logs.txt
retention-days: 30

Expand Down
83 changes: 39 additions & 44 deletions .github/actions/integration/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,42 @@ inputs:
required: true

runs:
using: composite

steps:
- name: Set up Node.js version
uses: actions/setup-node@v4
with:
node-version-file: './services/API-service/.node-version'
cache: 'npm'
cache-dependency-path: './services/API-service/package-lock.json'

- name: Install code-dependencies
shell: bash
working-directory: ./services/API-service
run: npm ci --omit=optional --no-fund --no-audit

- name: Setup environment variables
shell: bash
run: cp example.env .env

- name: Install IBF API-service
shell: bash
working-directory: ./services/API-service
run: npm ci --omit=optional --no-fund --no-audit

- name: Run IBF API-service
shell: bash
env:
MC_API: ${{ inputs.mailchimp-api-key }}
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d ibf-api-service

- name: Wait for IBF API-service to be running
shell: bash
run: timeout 60s sh -c 'until curl http://localhost:3000/api -I; do echo "Waiting for the IBF API-service to be running..."; sleep 1; done'

- name: Run integration tests
shell: bash
env:
DASHBOARD_URL: http://localhost:4200
run: docker compose exec ibf-api-service npm run test:api:all

- name: Docker logs
if: always()
uses: jwalton/gh-docker-logs@v2

using: composite

steps:
- name: Set up Node.js version
uses: actions/setup-node@v4
with:
node-version-file: './tests/integration/.node-version'
cache: 'npm'
cache-dependency-path: './tests/integration/package-lock.json'

- name: Install code-dependencies
shell: bash
working-directory: ./tests/integration
run: npm ci --omit=optional --no-fund --no-audit

- name: Setup environment variables
shell: bash
run: cp example.env .env

- name: Run IBF API-service
shell: bash
env:
MC_API: ${{ inputs.mailchimp-api-key }}
run: docker compose -f docker-compose.yml -f docker-compose.ci.yml up -d ibf-api-service

- name: Wait for IBF API-service to be running
shell: bash
run: timeout 60s sh -c 'until curl http://localhost:3000/api -I; do echo "Waiting for the IBF API-service to be running..."; sleep 1; done'

- name: Run integration tests
shell: bash
working-directory: ./tests/integration
env:
DASHBOARD_URL: http://localhost:4200
run: npm run test

- name: Docker logs
if: always()
uses: jwalton/gh-docker-logs@v2
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"open:ibf-api-service": "open http://localhost:3000/docs",
"test:interface": "npm test --prefix interfaces/IBF-dashboard/",
"test:performance": "npm run lighthouse:run",
"test:e2e": "npm test --prefix tests/e2e",
"test:integration": "npm test --prefix tests/integration",
"lint": "npx npm-run-all --print-label --parallel lint:interface lint:services",
"lint:services": "npm run lint --prefix services/API-service/",
"lint:interface": "npm run lint --prefix interfaces/IBF-dashboard/",
Expand Down
4 changes: 0 additions & 4 deletions services/API-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,3 @@ For the rest, follow the same instructions as above to receive initial and follo
- Endpoint URL: `https://ibf.510.global/api/point-data/community-notification/${countryCodeISO3}`
- To test this locally you can replace `ibf.510.global` by a local ngrok address
- To demo on other environments, replace by respective environment-url, e.g. `ibf-test.510.global`

## API tests

1. Run them by `docker exec ibf-api-service npm run test:api:all`
16 changes: 0 additions & 16 deletions services/API-service/jest.api.config.js

This file was deleted.

4 changes: 0 additions & 4 deletions services/API-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"test": "jest --config=jest.json --detectOpenHandles --forceExit --passWithNoTests",
"test:dev": "npm test -- --watchAll",
"test:coverage": "npm test -- --coverage --coverageDirectory=coverage",
"test:api:all": "node --expose-gc node_modules/.bin/jest --config=jest.api.config.js --runInBand --detectOpenHandles --logHeapUsage",
"test:api:watch": "npm run test:api:all -- --watchAll",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",
"migration:generate": "npm run typeorm migration:generate -- -d ./appdatasource.ts",
"migration:run": "npm run typeorm migration:run -- -d ./appdatasource.ts",
Expand Down Expand Up @@ -61,7 +59,6 @@
"@types/express": "^4.17.14",
"@types/jest": "^26.0.20",
"@types/node": "16.x",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"atob": ">=2.1.0",
Expand All @@ -74,7 +71,6 @@
"jest": "^24.9.0",
"nodemon": "^2.0.7",
"prettier": "^2.7.1",
"supertest": "^6.1.3",
"swagger-ui-express": "^4.0.6",
"ts-jest": "^24.3.0",
"ts-node": "^10.9.1",
Expand Down
8 changes: 0 additions & 8 deletions services/API-service/test/tsconfig.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from '@playwright/test';
import { format } from 'date-fns';
import { Locator, Page } from 'playwright';

import EnglishTranslations from '../../interfaces/IBF-dashboard/src/assets/i18n/en.json';
import EnglishTranslations from '../../../interfaces/IBF-dashboard/src/assets/i18n/en.json';
import DashboardPage from './DashboardPage';

const chatDialogueContentWelcomeNoTrigger =
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions e2e/README.md → tests/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

## Installation

Clone the repository and run local Docker environment following the general [installation-documentation](../README.md#getting-started).
Clone the repository and run local Docker environment following the general [installation-documentation](../../README.md#getting-started).

### Install E2E-test dependencies

From the repository root-folder, move into this folder: `cd ./e2e/`
From the repository root-folder, move into this folder: `cd ./tests/e2e/`

Then, in _this_ folder, run:

Expand All @@ -45,7 +45,7 @@ npx playwright install

### Set necessary Environment-variables

See the "Testing only"-section at the end of the [`./.env.example`](./.env.example)-file.
See the "Testing only"-section at the end of the [`./.env.example`](../../.env.example)-file.

Make sure to fill in all relevant variables in your local `./.env`-file.

Expand Down Expand Up @@ -121,7 +121,6 @@ class LoginPage extends BasePage {
}

export default LoginPage;

```

## Writing Tests
Expand Down
63 changes: 63 additions & 0 deletions tests/e2e/helpers/utility.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as request from 'supertest';
import TestAgent from 'supertest/lib/agent';

import { FloodsScenario } from '../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import users from '../../../services/API-service/src/scripts/json/users.json';

export async function getAccessToken(): Promise<string> {
const admin = users.find(
(user: { userRole: string }) => user.userRole === 'admin',
);
if (!admin) throw new Error('Admin not found');

const login = await loginApi(admin?.email, admin?.password);
const accessToken = login.body.user.token;
return accessToken;
}

export function loginApi(
email: string,
password?: string,
): Promise<request.Response> {
return getServer().post(`/user/login`).send({
email,
password,
});
}

export function getHostname(): string | undefined {
// Use here the port the API-service is exposed to on the host, as the e2e tests are run on the host

return process.env.API_SERVICE_URL;
}

export function getServer(): TestAgent<request.Test> {
return request.agent(getHostname());
}

export function resetDB(accessToken: string): Promise<request.Response> {
return getServer()
.post('/scripts/reset')
.set('Authorization', `Bearer ${accessToken}`)
.send({
secret: process.env.RESET_SECRET,
});
}

export function mockFloods(
scenario: FloodsScenario,
countryCodeISO3: string,
accessToken: string,
): Promise<request.Response> {
return getServer()
.post('/mock/floods')
.set('Authorization', `Bearer ${accessToken}`)
.query({ isApiTest: true })
.send({
scenario,
secret: process.env.RESET_SECRET,
removeEvents: true,
date: new Date(),
countryCodeISO3,
});
}
File renamed without changes.
4 changes: 0 additions & 4 deletions e2e/package.json → tests/e2e/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"name": "e2e",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint \"**/*.{ts,js}\"",
"fix": "npm run lint -- --fix",
"test": "playwright test"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@eslint/js": "^9.10.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

const envPath = path.resolve(__dirname, '../.env');
const envPath = path.resolve(__dirname, '../../.env');
dotenv.config({ path: envPath });

export default defineConfig({
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import DashboardPage from 'Pages/DashboardPage';
import { qase } from 'playwright-qase-reporter';
import { NoTriggerDataSet } from 'testData/testData.enum';

import { FloodsScenario } from '../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import { FloodsScenario } from '../../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import {
getAccessToken,
mockFloods,
resetDB,
} from '../../../services/API-service/test/helpers/utility.helper';
} from '../../helpers/utility.helper';
import LoginPage from '../../Pages/LoginPage';

let accessToken: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import UserStateComponent from 'Pages/UserStateComponent';
import { qase } from 'playwright-qase-reporter';
import { NoTriggerDataSet } from 'testData/testData.enum';

import { FloodsScenario } from '../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import { FloodsScenario } from '../../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import {
getAccessToken,
mockFloods,
resetDB,
} from '../../../services/API-service/test/helpers/utility.helper';
} from '../../helpers/utility.helper';
import LoginPage from '../../Pages/LoginPage';

let accessToken: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import UserStateComponent from 'Pages/UserStateComponent';
import { qase } from 'playwright-qase-reporter';
import { NoTriggerDataSet } from 'testData/testData.enum';

import { FloodsScenario } from '../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import { FloodsScenario } from '../../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import {
getAccessToken,
mockFloods,
resetDB,
} from '../../../services/API-service/test/helpers/utility.helper';
} from '../../helpers/utility.helper';
import LoginPage from '../../Pages/LoginPage';

let accessToken: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import UserStateComponent from 'Pages/UserStateComponent';
import { qase } from 'playwright-qase-reporter';
import { NoTriggerDataSet } from 'testData/testData.enum';

import { FloodsScenario } from '../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import { FloodsScenario } from '../../../../services/API-service/src/scripts/enum/mock-scenario.enum';
import {
getAccessToken,
mockFloods,
resetDB,
} from '../../../services/API-service/test/helpers/utility.helper';
} from '../../helpers/utility.helper';
import LoginPage from '../../Pages/LoginPage';

let accessToken: string;
Expand Down
Loading