Skip to content

Commit

Permalink
Merge pull request #76 from klerick/setup-e2e-test
Browse files Browse the repository at this point in the history
ci: Setup e2e test
  • Loading branch information
klerick authored Mar 7, 2024
2 parents 73ce9d8 + a01fb8d commit 3c070d4
Show file tree
Hide file tree
Showing 23 changed files with 476 additions and 162 deletions.
1 change: 1 addition & 0 deletions .e2e.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_LOGGING=0
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DB_HOST=localhost
DB_LOGGING=1

DB_USERNAME="postgres"
DB_PASSWORD="postgres"
DB_NAME="json-api-db"
DB_PORT=5432
DB_TYPE=postgres

#DB_USERNAME="root"
#DB_PASSWORD="password"
#DB_NAME="example_new"
#DB_PORT=3306
#DB_TYPE=mysql
4 changes: 1 addition & 3 deletions .github/actions/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ runs:
with:
path: |
node_modules
.nx
.angular
~/.cache/Cypress # needed for the Cypress binary
key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package-lock.json') }}
- name: Npm install
if: steps.cache.outputs.cache-hit != 'true'
if: steps.cache-dependencies-restore.outputs.cache-hit != 'true'
run: npm ci
shell: bash
- name: Cache npm dependencies
Expand Down
67 changes: 57 additions & 10 deletions .github/workflows/bump-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,75 @@ jobs:
run: |
echo "GIST_SECRET=${{ secrets.GIST_SECRET }}" >> $GITHUB_ENV
echo "GIST_ID=${{ secrets.GIST_ID }}" >> $GITHUB_ENV
- name: Restore cached .nx
id: cache-nx-restore
uses: actions/cache/restore@v4
with:
path: |
.nx
key: ${{ runner.os }}-nx-master"
- name: Test and build
run: npx nx affected -t test build --parallel=3 --exclude='json-api-front,json-api-server,json-api-server-e2e,json-shared-type,database,@nestjs-json-api/source'
- name: Upload test coverage badge
run: npx nx affected -t upload-badge --parallel=3 --exclude='json-api-front,json-api-server,shared-utils,json-api-server-e2e,json-shared-type,database,@nestjs-json-api/source'
- name: Cache npm dependencies
- name: Save cached .nx
id: cache-dependencies-save
uses: actions/cache/save@v4
with:
path: |
.nx
.angular
key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package-lock.json') }}
# env:
# GIST_SECRET: ${{ secrets.GIST_SECRET }}
# GIST_ID: ${{ secrets.GIST_ID }}
# - run: npm nx affected -t e2e-ci --parallel=1
# - run: npm nx affected -t deploy --no-agents

key: ${{ steps.cache-nx-restore.outputs.cache-primary-key }}
run-e2e-test:
runs-on: ubuntu-latest
name: Try run e2e test
needs:
- build-and-test
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: json-api-db
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Npm install
uses: ./.github/actions
- name: Restore cached .nx
id: cache-nx-restore
uses: actions/cache/restore@v4
with:
path: |
.nx
key: ${{ runner.os }}-nx-master"
- run: git branch --track main origin/master
- run: npm run typeorm migration:run
- run: npm run seed:run
- run: npx nx affected -t e2e --parallel=1
- name: Save cached .nx
id: cache-dependencies-save
uses: actions/cache/save@v4
with:
path: |
.nx
key: ${{ steps.cache-nx-restore.outputs.cache-primary-key }}
bump-version:
name: "Bump version"
needs:
- build-and-test
- run-e2e-test
runs-on: ubuntu-latest
permissions:
contents: "write"
Expand Down
69 changes: 63 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,81 @@ on:
- synchronize
jobs:
run-test:
name: Try build and run unit test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Npm install
uses: ./.github/actions
# This line is needed for nx affected to work when CI is running on a PR
- name: Get branch names.
id: branch-names
uses: tj-actions/branch-names@v8
- name: Restore cached .nx
id: cache-nx-restore
uses: actions/cache/restore@v4
with:
path: |
.nx
key: ${{ runner.os }}-nx-${{ steps.branch-names.outputs.current_branch }}"
- run: git branch --track main origin/master
- name: Test and build
run: npx nx affected -t test build --parallel=3 --exclude='json-api-front,json-api-server,json-api-server-e2e,json-shared-type,database,@nestjs-json-api/source'
- name: Cache npm dependencies
- name: Save cached .nx
id: cache-dependencies-save
uses: actions/cache/save@v4
with:
path: |
.nx
key: ${{ steps.cache-nx-restore.outputs.cache-primary-key }}
run-e2e-test:
runs-on: ubuntu-latest
name: Try run e2e test
needs:
- run-test
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: json-api-db
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Npm install
uses: ./.github/actions
- name: Get branch names.
id: branch-names
uses: tj-actions/branch-names@v8
- name: Restore cached .nx
id: cache-nx-restore
uses: actions/cache/restore@v4
with:
path: |
.nx
key: ${{ runner.os }}-nx-${{ steps.branch-names.outputs.current_branch }}"
- run: git branch --track main origin/master
- run: npm run typeorm migration:run
- run: npm run seed:run
- run: npx nx affected -t e2e --parallel=1
- name: Save cached .nx
id: cache-dependencies-save
uses: actions/cache/save@v4
with:
path: |
.nx
.angular
key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package-lock.json') }}
# - run: npm nx affected -t e2e-ci --parallel=1
# - run: npm nx affected -t deploy --no-agents
key: ${{ steps.cache-nx-restore.outputs.cache-primary-key }}
10 changes: 7 additions & 3 deletions apps/json-api-server-e2e/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ export default {
globalTeardown: '<rootDir>/src/support/global-teardown.ts',
setupFiles: ['<rootDir>/src/support/test-setup.ts'],
testEnvironment: 'node',
maxWorkers: 1,
transform: {
'^.+\\.[tj]s$': ['ts-jest', {
tsconfig: '<rootDir>/tsconfig.spec.json',
}],
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/json-api-server-e2e',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {
adapterForAxios,
FilterOperand,
JsonApiJs,
JsonSdkPromise,
} from 'json-api-nestjs-sdk';
import axios from 'axios';
import { INestApplication } from '@nestjs/common';
import { FilterOperand, JsonSdkPromise } from 'json-api-nestjs-sdk';
import { Addresses, CommentKind, Comments, Roles, Users } from 'database';
import { faker } from '@faker-js/faker';
import { getUser } from '../utils/data-utils';
import { run, creatSdk } from '../utils/run-ppplication';

let app: INestApplication;

beforeAll(async () => {
app = await run();
});

afterAll(async () => {
await app.close();
});

describe('Atomic method:', () => {
let jsonSdk: JsonSdkPromise;
Expand All @@ -16,18 +22,7 @@ describe('Atomic method:', () => {
let commentsArray: Comments[];
let usersId: number[];
beforeEach(async () => {
const axiosAdapter = adapterForAxios(axios);

jsonSdk = JsonApiJs(
{
adapter: axiosAdapter,
apiHost: 'http://localhost:3000',
apiPrefix: 'api',
dateFields: ['createdAt', 'updatedAt'],
operationUrl: 'operation',
},
true
);
jsonSdk = creatSdk();

const addressesPromise = Array.from(new Array(2)).map(() => {
const address = new Addresses();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import {
adapterForAxios,
FilterOperand,
JsonApiJs,
JsonSdkPromise,
} from 'json-api-nestjs-sdk';
import axios, { AxiosError } from 'axios';
import { INestApplication } from '@nestjs/common';
import { FilterOperand, JsonSdkPromise } from 'json-api-nestjs-sdk';
import { AxiosError } from 'axios';
import { Users } from 'database';

import { run, creatSdk } from '../utils/run-ppplication';

let app: INestApplication;

beforeAll(async () => {
app = await run();
});

afterAll(async () => {
await app.close();
});

describe('Check common decorator', () => {
let jsonSdk: JsonSdkPromise;
const axiosAdapter = adapterForAxios(axios);
beforeEach(async () => {
jsonSdk = JsonApiJs(
{
adapter: axiosAdapter,
apiHost: 'http://localhost:3000',
apiPrefix: 'api',
dateFields: ['createdAt', 'updatedAt'],
operationUrl: 'operation',
idIsNumber: false,
},
true
);
jsonSdk = creatSdk();
});

afterEach(async () => {});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import {
adapterForAxios,
FilterOperand,
JsonApiJs,
JsonSdkPromise,
} from 'json-api-nestjs-sdk';
import { INestApplication } from '@nestjs/common';
import { FilterOperand, JsonSdkPromise } from 'json-api-nestjs-sdk';
import { BookList, Users } from 'database';
import axios, { AxiosError } from 'axios';
import { AxiosError } from 'axios';
import { faker } from '@faker-js/faker';
import { lastValueFrom } from 'rxjs';
import { creatSdk, run, axiosAdapter } from '../utils/run-ppplication';

let app: INestApplication;

beforeAll(async () => {
app = await run();
});

afterAll(async () => {
await app.close();
});

describe('Other call type:', () => {
let jsonSdk: JsonSdkPromise;
const axiosAdapter = adapterForAxios(axios);

beforeEach(async () => {
jsonSdk = JsonApiJs(
{
adapter: axiosAdapter,
apiHost: 'http://localhost:3000',
apiPrefix: 'api',
dateFields: ['createdAt', 'updatedAt'],
operationUrl: 'operation',
idIsNumber: false,
},
true
);
jsonSdk = creatSdk({
idIsNumber: false,
});
});

afterEach(async () => {});
Expand Down Expand Up @@ -52,7 +51,6 @@ describe('Other call type:', () => {
jsonSdk.jsonApiUtilsService.convertResponseData(newBookSource);

expect(newBook.id).toBeDefined();

const bookResultSource = await lastValueFrom(
axiosAdapter.get<BookList>(`${url}/${newBook.id}`)
);
Expand Down
Loading

0 comments on commit 3c070d4

Please sign in to comment.