Add e2e tests for nest (#15522)

This commit is contained in:
Nicholas Cunningham 2023-03-08 11:53:31 -07:00 committed by GitHub
parent a69f8abdf9
commit 0383fa6017
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 56 additions and 7 deletions

View File

@ -64,6 +64,7 @@ describe('Node Applications + webpack', () => {
const expressApp = uniq('expressapp');
const fastifyApp = uniq('fastifyapp');
const koaApp = uniq('koaapp');
const nestApp = uniq('koaapp');
runCLI(`generate @nrwl/node:lib ${testLib1}`);
runCLI(`generate @nrwl/node:lib ${testLib2} --importPath=@acme/test2`);
@ -76,18 +77,27 @@ describe('Node Applications + webpack', () => {
runCLI(
`generate @nrwl/node:app ${koaApp} --framework=koa --no-interactive`
);
runCLI(
`generate @nrwl/node:app ${nestApp} --framework=nest --bundler=webpack --no-interactive`
);
// Use esbuild by default
checkFilesDoNotExist(`apps/${expressApp}/webpack.config.js`);
checkFilesDoNotExist(`apps/${fastifyApp}/webpack.config.js`);
checkFilesDoNotExist(`apps/${koaApp}/webpack.config.js`);
// Uses only webpack
checkFilesExist(`apps/${nestApp}/webpack.config.js`);
expect(() => runCLI(`lint ${expressApp}`)).not.toThrow();
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow();
expect(() => runCLI(`lint ${koaApp}`)).not.toThrow();
expect(() => runCLI(`lint ${nestApp}`)).not.toThrow();
expect(() => runCLI(`lint ${expressApp}-e2e`)).not.toThrow();
expect(() => runCLI(`lint ${fastifyApp}-e2e`)).not.toThrow();
expect(() => runCLI(`lint ${koaApp}-e2e`)).not.toThrow();
expect(() => runCLI(`lint ${nestApp}-e2e`)).not.toThrow();
// Only Fastify generates with unit tests since it supports them without additional libraries.
expect(() => runCLI(`lint ${fastifyApp}`)).not.toThrow();
@ -99,9 +109,13 @@ describe('Node Applications + webpack', () => {
addLibImport(koaApp, testLib1);
addLibImport(koaApp, testLib2, '@acme/test2');
addLibImport(nestApp, testLib1);
addLibImport(nestApp, testLib2, '@acme/test2');
await runE2eTests(expressApp);
await runE2eTests(fastifyApp);
await runE2eTests(koaApp);
await runE2eTests(nestApp);
}, 300_000);
it('should generate a Dockerfile', async () => {

View File

@ -14,9 +14,9 @@ describe('AppController', () => {
});
describe('getData', () => {
it('should return "Welcome to <%= name %>!"', () => {
it('should return "Hello API"', () => {
const appController = app.get<AppController>(AppController);
expect(appController.getData()).toEqual({message: 'Welcome to <%= name %>!'});
expect(appController.getData()).toEqual({message: 'Hello API'});
});
});
});

View File

@ -14,8 +14,8 @@ describe('AppService', () => {
});
describe('getData', () => {
it('should return "Welcome to <%= name %>!"', () => {
expect(service.getData()).toEqual({message: 'Welcome to <%= name %>!'});
it('should return "Hello API"', () => {
expect(service.getData()).toEqual({message: 'Hello API'});
});
});
});

View File

@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getData(): { message: string } {
return ({ message: 'Welcome to <%= name %>!' });
return ({ message: 'Hello API' });
}
}

View File

@ -12,7 +12,7 @@ async function bootstrap() {
const app = await NestFactory.create(AppModule);
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.PORT || 3333;
const port = process.env.PORT || 3000;
await app.listen(port);
Logger.log(`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`);
}

View File

@ -47,5 +47,6 @@ export function toNodeApplicationGeneratorOptions(
setParserOptionsProject: options.setParserOptionsProject,
rootProject: options.rootProject,
bundler: 'webpack', // Some features require webpack plugins such as TS transformers
isNest: true,
};
}

View File

@ -415,6 +415,7 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
name: options.rootProject ? 'e2e' : `${options.name}-e2e`,
project: options.name,
port: options.port,
isNest: options.isNest,
});
tasks.push(e2eTask);
}

View File

@ -20,6 +20,7 @@ export interface Schema {
port?: number;
rootProject?: boolean;
docker?: boolean;
isNest?: boolean;
}
export type NodeJsFrameWorks = 'express' | 'koa' | 'fastify' | 'nest' | 'none';

View File

@ -43,7 +43,7 @@ export async function e2eProjectGenerator(host: Tree, _options: Schema) {
if (options.projectType === 'server') {
generateFiles(
host,
path.join(__dirname, 'files/server'),
path.join(__dirname, 'files/server/common'),
options.e2eProjectRoot,
{
...options,
@ -52,6 +52,20 @@ export async function e2eProjectGenerator(host: Tree, _options: Schema) {
tmpl: '',
}
);
if (options.isNest) {
generateFiles(
host,
path.join(__dirname, 'files/server/nest'),
options.e2eProjectRoot,
{
...options,
...names(options.rootProject ? 'server' : options.project),
offsetFromRoot: offsetFromRoot(options.e2eProjectRoot),
tmpl: '',
}
);
}
} else if (options.projectType === 'cli') {
const mainFile = appProject.targets.build?.options?.outputPath;
generateFiles(

View File

@ -0,0 +1,10 @@
import axios from 'axios';
describe('GET /api', () => {
it('should return a message', async () => {
const res = await axios.get(`/api`);
expect(res.status).toBe(200);;
expect(res.data).toEqual({ message: 'Hello API' });
});
})

View File

@ -7,4 +7,5 @@ export interface Schema {
linter?: 'eslint' | 'none';
formatFile?: boolean;
rootProject?: boolean;
isNest?: boolean;
}

View File

@ -39,6 +39,13 @@
"enum": ["eslint", "none"],
"default": "eslint"
},
"isNest": {
"description": "Is the E2E server project nest",
"type": "boolean",
"default": false,
"hidden": true,
"x-priority": "internal"
},
"rootProject": {
"description": "Create node application at the root of the workspace.",
"type": "boolean",