Skip to content

Commit

Permalink
feat(node): add e2e project generator (#14429)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Jan 17, 2023
1 parent 965c638 commit 013bb74
Show file tree
Hide file tree
Showing 27 changed files with 451 additions and 21 deletions.
6 changes: 6 additions & 0 deletions docs/generated/packages/node/generators/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
"default": false,
"hidden": true,
"x-priority": "internal"
},
"e2eTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end to end (e2e) tests",
"default": "jest"
}
},
"required": [],
Expand Down
63 changes: 63 additions & 0 deletions e2e/node/src/node-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
checkFilesDoNotExist,
cleanupProject,
killPort,
newProject,
promisifiedTreeKill,
runCLI,
runCommandUntil,
uniq,
} from '@nrwl/e2e/utils';

describe('Node Applications + webpack', () => {
beforeEach(() => newProject());

afterEach(() => cleanupProject());

async function runE2eTests(appName: string) {
process.env.PORT = '5000';
const childProcess = await runCommandUntil(`serve ${appName}`, (output) => {
return output.includes('http://localhost:5000');
});
const result = runCLI(`e2e ${appName}-e2e`);
expect(result).toContain('Setting up...');
expect(result).toContain('Tearing down..');
expect(result).toContain('Successfully ran target e2e');

await promisifiedTreeKill(childProcess.pid, 'SIGKILL');
await killPort(5000);
process.env.PORT = '';
}

it('should generate an app using webpack', async () => {
const expressApp = uniq('expressapp');
const fastifyApp = uniq('fastifyapp');
const koaApp = uniq('koaapp');

runCLI(
`generate @nrwl/node:app ${expressApp} --framework=express --no-interactive`
);
runCLI(
`generate @nrwl/node:app ${fastifyApp} --framework=fastify --no-interactive`
);
runCLI(
`generate @nrwl/node:app ${koaApp} --framework=koa --no-interactive`
);

// Use esbuild by default
checkFilesDoNotExist(`apps/${expressApp}/webpack.config.js`);
checkFilesDoNotExist(`apps/${fastifyApp}/webpack.config.js`);
checkFilesDoNotExist(`apps/${koaApp}/webpack.config.js`);

expect(() => runCLI(`lint ${expressApp}`)).not.toThrow();
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow();
expect(() => runCLI(`lint ${koaApp}`)).not.toThrow();
expect(() => runCLI(`lint ${expressApp}-e2e`)).not.toThrow();
expect(() => runCLI(`lint ${fastifyApp}-e2e`)).not.toThrow();
expect(() => runCLI(`lint ${koaApp}-e2e`)).not.toThrow();

await runE2eTests(expressApp);
await runE2eTests(fastifyApp);
await runE2eTests(koaApp);
}, 300_000);
});
1 change: 0 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@nrwl/linter": "file:../linter",
"@nrwl/webpack": "file:../webpack",
"@nrwl/workspace": "file:../workspace",
"enquirer": "~2.3.6",
"tslib": "^2.3.0"
},
"publishConfig": {
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ describe('app', () => {
lintFilePatterns: ['my-node-app/**/*.ts'],
},
});
expect(() => readProjectConfiguration(tree, 'my-node-app-e2e')).toThrow(
/Cannot find/
);
expect(() =>
readProjectConfiguration(tree, 'my-node-app-e2e')
).not.toThrow();
});

it('should update tags', async () => {
Expand Down
25 changes: 20 additions & 5 deletions packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { Linter, lintProjectGenerator } from '@nrwl/linter';
import { jestProjectGenerator } from '@nrwl/jest';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';

import { NodeJsFrameWorks, Schema } from './schema';
import { Schema } from './schema';
import { initGenerator } from '../init/init';
import { getRelativePathToRootTsConfig } from '@nrwl/workspace/src/utilities/typescript';
import {
Expand All @@ -41,9 +41,9 @@ import {
koaVersion,
nxVersion,
} from '../../utils/versions';
import { prompt } from 'enquirer';

import * as shared from '@nrwl/workspace/src/utils/create-ts-config';
import { e2eProjectGenerator } from '../e2e-project/e2e-project';

export interface NormalizedSchema extends Schema {
appProjectRoot: string;
Expand Down Expand Up @@ -318,15 +318,16 @@ function updateTsConfigOptions(tree: Tree, options: NormalizedSchema) {

export async function applicationGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(tree, schema);

const tasks: GeneratorCallback[] = [];

const initTask = await initGenerator(tree, {
...options,
skipFormat: true,
});
tasks.push(initTask);

addProjectDependencies(tree, options);
const installTask = addProjectDependencies(tree, options);
tasks.push(installTask);
addAppFiles(tree, options);
addProject(tree, options);

Expand All @@ -347,12 +348,24 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
setupFile: 'none',
skipSerializers: true,
supportTsx: options.js,
babelJest: options.babelJest,
testEnvironment: 'node',
compiler: 'tsc',
skipFormat: true,
});
tasks.push(jestTask);
}

if (options.e2eTestRunner === 'jest') {
const e2eTask = await e2eProjectGenerator(tree, {
...options,
projectType: options.framework === 'none' ? 'cli' : 'server',
name: options.rootProject ? 'e2e' : `${options.name}-e2e`,
project: options.name,
port: options.port,
});
tasks.push(e2eTask);
}

if (options.js) {
updateTsConfigsToJs(tree, { projectRoot: options.appProjectRoot });
}
Expand Down Expand Up @@ -385,6 +398,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
: joinPathFragments(appsDir, appDirectory);

options.bundler = options.bundler ?? 'esbuild';
options.e2eTestRunner = options.e2eTestRunner ?? 'jest';

const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
Expand All @@ -400,6 +414,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
parsedTags,
linter: options.linter ?? Linter.EsLint,
unitTestRunner: options.unitTestRunner ?? 'jest',
rootProject: options.rootProject ?? false,
port: options.port ?? 3000,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import connect from 'connect';

const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;

const app = connect();

app.use((req, res) => {
res.end(JSON.stringify({ 'message': 'Hello API' }));
});

app.listen(<%= port %>, () => {
// Server is running
})
app.listen(port, () => {
console.log(`[ ready ] http://localhost:${port}`);
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import express from 'express';
const app = express();

const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;

const app = express();

app.get('/', (req, res) => {
res.send({ 'message': 'Hello API'});
});

app.listen(<%= port %>, () => {
// Server is running
});
app.listen(port, () => {
console.log(`[ ready ] http://localhost:${port}`);
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fastify from 'fastify';

const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;

const app = fastify();

app.get('/', async (req, res) => {
Expand All @@ -8,11 +10,12 @@ app.get('/', async (req, res) => {

const start = async() => {
try {
await app.listen({ port: <%= port %> })
await app.listen({ port });
console.log(`[ ready ] http://localhost:${port}`);
} catch (err) {
// Errors are logged here
process.exit(1);
}
}

start();
start();
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import koa from 'koa';

const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>;

const app = new koa();

app.use(async ctx =>{
ctx.body = { 'message': 'Hello API'};
});

app.listen(<%= port %>, () => {
// Server is running
});
app.listen(port, () => {
console.log(`[ ready ] http://localhost:${port}`);
});
1 change: 1 addition & 0 deletions packages/node/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Schema {
skipPackageJson?: boolean;
directory?: string;
unitTestRunner?: 'jest' | 'none';
e2eTestRunner?: 'jest' | 'none';
linter?: Linter;
tags?: string;
frontendProject?: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/node/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
"default": false,
"hidden": true,
"x-priority": "internal"
},
"e2eTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for end to end (e2e) tests",
"default": "jest"
}
},
"required": []
Expand Down
Loading

1 comment on commit 013bb74

@vercel
Copy link

@vercel vercel bot commented on 013bb74 Jan 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app

Please sign in to comment.