Skip to content

Commit

Permalink
Merge pull request #227 from IKatsuba/local-development
Browse files Browse the repository at this point in the history
local-development
  • Loading branch information
IKatsuba authored Sep 18, 2023
2 parents 5e0fda7 + 3b6bcf7 commit 1d63cf7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 40 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ jobs:
npx nx affected --target=lint --parallel=3
npx nx affected --target=test --parallel=3 --ci --code-coverage
npx nx affected --target=build --parallel=3
e2e:
name: Nx Cloud - E2E Job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: |
npm ci
npx nx build api -c=production
- name: Docker Compose
run: |
docker-compose -f compose.test.yml up -d
npx -y wait-on http://localhost:3000/ping
- name: Run E2E tests
run: |
npx nx e2e api-e2e
- name: Docker Compose down
run: |
docker-compose -f compose.test.yml down
agents:
name: Nx Cloud - Agents
uses: nrwl/ci/.github/workflows/[email protected]
Expand Down
37 changes: 17 additions & 20 deletions apps/api-e2e/tests/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
import { readJson, runNxCommandAsync } from '@nx/plugin/testing';
import { addPackageToPackageJson, createWorkspace } from './utils';
import { readJson, runNxCommandAsync, tmpProjPath } from '@nx/plugin/testing';
import { addPackageToPackageJson, createTestProject } from './utils';
import { rmSync } from 'fs';
import { getPackageManagerCommand } from 'nx/src/utils/package-manager';
import { execSync } from 'child_process';

describe('api e2e', () => {
// Setting up individual workspaces per
// test can cause e2e runs to take a long time.
// For this reason, we recommend each suite only
// consumes 1 workspace. The tests should each operate
// on a unique project in the workspace, such that they
// are not dependant on one another.
beforeAll(async () => {
createWorkspace();
addPackageToPackageJson('@nrwl/nx-cloud');
// const project = uniq('app');
// await runNxCommandAsync(`generate @nx/node:application ${project}`);
createTestProject();

addPackageToPackageJson('nx-cloud');
}, 120000);

afterAll(() => {
// `nx reset` kills the daemon, and performs
// some work which can help clean up e2e leftovers
runNxCommandAsync('reset');
rmSync(tmpProjPath(), {
recursive: true,
force: true,
});
});

it('should configure Nx Cloud', async () => {
await runNxCommandAsync(`generate @nrwl/nx-cloud:init`, {
execSync(`npx nx generate nx-cloud:init`, {
env: {
...process.env,
NX_CLOUD_API: 'http://localhost:3333/',
NX_CLOUD_API: 'http://localhost:3000/',
},
silenceError: false,
cwd: tmpProjPath(),
});

const nxJson = readJson('nx.json');

expect(nxJson.tasksRunnerOptions).toEqual({
default: {
runner: '@nrwl/nx-cloud',
runner: 'nx-cloud',
options: {
cacheableOperations: ['build', 'lint', 'test', 'e2e'],
accessToken: expect.any(String),
url: 'http://localhost:3333/',
url: 'http://localhost:3000/',
},
},
});
Expand Down
40 changes: 21 additions & 19 deletions apps/api-e2e/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import {
cleanup,
runPackageManagerInstall,
tmpProjPath,
} from '@nx/plugin/testing';
import * as fsExtra from 'fs-extra';
import * as path from 'path';
import { tmpProjPath } from '@nx/plugin/testing';
import { dirname, join, basename } from 'path';
import * as ch from 'child_process';
import { execSync } from 'child_process';
import { getPackageManagerCommand } from 'nx/src/utils/package-manager';
import { mkdirSync, rmSync } from 'fs';

export function createWorkspace() {
fsExtra.ensureDirSync(tmpProjPath());
export function createTestProject() {
const projectName = basename(tmpProjPath());
const projectDirectory = tmpProjPath();

cleanup();
const localTmpDir = path.dirname(tmpProjPath());
// Ensure projectDirectory is empty
rmSync(projectDirectory, {
recursive: true,
force: true,
});
mkdirSync(dirname(projectDirectory), {
recursive: true,
});

ch.execSync(
`node ${require.resolve(
'nx'
)} new proj --nx-workspace-root=${localTmpDir} --no-interactive --skip-install --collection=@nx/workspace --npmScope=proj --preset=empty`,
execSync(
`npx --yes create-nx-workspace@latest ${projectName} --preset apps --no-nxCloud --no-interactive`,
{
cwd: localTmpDir,
cwd: dirname(projectDirectory),
stdio: 'inherit',
env: process.env,
}
);

runPackageManagerInstall();
console.log(`Created test project in "${projectDirectory}"`);
}

export function addPackageToPackageJson(packageName: string) {
const packageManagerCommand = getPackageManagerCommand();

ch.execSync(`${packageManagerCommand.addDev} ${packageName}`, {
cwd: tmpProjPath(),
});
Expand Down
46 changes: 46 additions & 0 deletions compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
services:
api:
build:
context: .
dockerfile: apps/api/Dockerfile
ports:
- 3000:3000
environment:
- PORT=3000
- DB_HOST=postgres
- DB_PORT=5432
- DB_USER=postgres
- DB_PASSWORD=postgres
- DB_NAME=postgres
- JWT_SECRET=secret
- AWS_S3_ENDPOINT_URL=http://s3:9000
- AWS_ACCESS_KEY_ID=minio
- AWS_SECRET_ACCESS_KEY=minio123
- AWS_S3_BUCKET_NAME=nx-cloud
- AWS_REGION=us-east-1
postgres:
image: postgres:12.1
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
s3:
image: minio/minio
entrypoint: sh -c "minio server /data --console-address ':9001'"
environment:
MINIO_ROOT_USER: minio
MINIO_ROOT_PASSWORD: minio123
MINIO_DOMAIN: localhost
ports:
- 9000:9000
- 9001:9001
create-bucket-and-user:
image: minio/mc
entrypoint: sh -c "mc config host add minio http://s3:9000 minio minio123 && mc mb minio/nx-cloud && mc anonymous set public minio/nx-cloud"
depends_on:
- s3
environment:
MINIO_ACCESS_KEY: minio
MINIO_SECRET_KEY: minio123
File renamed without changes.

0 comments on commit 1d63cf7

Please sign in to comment.