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

fix(node): pass projectNameAndRootFormat to js lib generator from nest lib generator #18891

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions packages/express/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ describe('app', () => {
let appTree: Tree;

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
appTree = createTreeWithEmptyWorkspace();
});

it('should generate files', async () => {
await applicationGenerator(appTree, {
name: 'myNodeApp',
projectNameAndRootFormat: 'as-provided',
} as Schema);

const mainFile = appTree.read('apps/my-node-app/src/main.ts').toString();
const mainFile = appTree.read('my-node-app/src/main.ts').toString();
expect(mainFile).toContain(`import express from 'express';`);

const tsconfig = readJson(appTree, 'apps/my-node-app/tsconfig.json');
const tsconfig = readJson(appTree, 'my-node-app/tsconfig.json');
expect(tsconfig).toMatchInlineSnapshot(`
{
"compilerOptions": {
"esModuleInterop": true,
},
"extends": "../../tsconfig.base.json",
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
Expand All @@ -38,11 +39,11 @@ describe('app', () => {
}
`);

const eslintrcJson = readJson(appTree, 'apps/my-node-app/.eslintrc.json');
const eslintrcJson = readJson(appTree, 'my-node-app/.eslintrc.json');
expect(eslintrcJson).toMatchInlineSnapshot(`
{
"extends": [
"../../.eslintrc.json",
"../.eslintrc.json",
],
"ignorePatterns": [
"!**/*",
Expand Down Expand Up @@ -79,14 +80,15 @@ describe('app', () => {
it('should add types to the tsconfig.app.json', async () => {
await applicationGenerator(appTree, {
name: 'myNodeApp',
projectNameAndRootFormat: 'as-provided',
} as Schema);
const tsconfig = readJson(appTree, 'apps/my-node-app/tsconfig.app.json');
const tsconfig = readJson(appTree, 'my-node-app/tsconfig.app.json');
expect(tsconfig.compilerOptions.types).toContain('express');
expect(tsconfig).toMatchInlineSnapshot(`
{
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"outDir": "../dist/out-tsc",
"types": [
"node",
"express",
Expand All @@ -110,23 +112,21 @@ describe('app', () => {
await applicationGenerator(appTree, {
name: 'myNodeApp',
js: true,
projectNameAndRootFormat: 'as-provided',
} as Schema);

expect(appTree.exists('apps/my-node-app/src/main.js')).toBeTruthy();
expect(appTree.read('apps/my-node-app/src/main.js').toString()).toContain(
expect(appTree.exists('my-node-app/src/main.js')).toBeTruthy();
expect(appTree.read('my-node-app/src/main.js').toString()).toContain(
`import express from 'express';`
);

const tsConfig = readJson(appTree, 'apps/my-node-app/tsconfig.json');
const tsConfig = readJson(appTree, 'my-node-app/tsconfig.json');
expect(tsConfig.compilerOptions).toEqual({
allowJs: true,
esModuleInterop: true,
});

const tsConfigApp = readJson(
appTree,
'apps/my-node-app/tsconfig.app.json'
);
const tsConfigApp = readJson(appTree, 'my-node-app/tsconfig.app.json');
expect(tsConfigApp.include).toEqual(['src/**/*.ts', 'src/**/*.js']);
expect(tsConfigApp.exclude).toEqual([
'jest.config.ts',
Expand Down
9 changes: 2 additions & 7 deletions packages/express/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
addDependenciesToPackageJson,
readJson,
NxJsonConfiguration,
Tree,
} from '@nx/devkit';
import { addDependenciesToPackageJson, readJson, Tree } from '@nx/devkit';
import { expressVersion } from '../../utils/versions';
import initGenerator from './init';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
Expand All @@ -12,7 +7,7 @@ describe('init', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
tree = createTreeWithEmptyWorkspace();
});

it('should add dependencies', async () => {
Expand Down
61 changes: 36 additions & 25 deletions packages/nest/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ describe('application generator', () => {
const appDirectory = 'my-node-app';

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
tree = createTreeWithEmptyWorkspace();
jest.clearAllMocks();
});

it('should generate project configurations', async () => {
await applicationGenerator(tree, { name: appName });
await applicationGenerator(tree, {
name: appName,
projectNameAndRootFormat: 'as-provided',
});

const projectConfigurations = devkit.getProjects(tree);

Expand All @@ -23,33 +26,32 @@ describe('application generator', () => {
});

it('should generate files', async () => {
await applicationGenerator(tree, { name: appName });
await applicationGenerator(tree, {
name: appName,
projectNameAndRootFormat: 'as-provided',
});

expect(tree.exists(`apps/${appDirectory}/src/main.ts`)).toBeTruthy();
expect(
tree.exists(`apps/${appDirectory}/src/app/app.controller.spec.ts`)
).toBeTruthy();
expect(tree.exists(`${appDirectory}/src/main.ts`)).toBeTruthy();
expect(
tree.exists(`apps/${appDirectory}/src/app/app.controller.ts`)
tree.exists(`${appDirectory}/src/app/app.controller.spec.ts`)
).toBeTruthy();
expect(
tree.exists(`apps/${appDirectory}/src/app/app.module.ts`)
tree.exists(`${appDirectory}/src/app/app.controller.ts`)
).toBeTruthy();
expect(tree.exists(`${appDirectory}/src/app/app.module.ts`)).toBeTruthy();
expect(
tree.exists(`apps/${appDirectory}/src/app/app.service.spec.ts`)
).toBeTruthy();
expect(
tree.exists(`apps/${appDirectory}/src/app/app.service.ts`)
tree.exists(`${appDirectory}/src/app/app.service.spec.ts`)
).toBeTruthy();
expect(tree.exists(`${appDirectory}/src/app/app.service.ts`)).toBeTruthy();
});

it('should configure tsconfig correctly', async () => {
await applicationGenerator(tree, { name: appName });
await applicationGenerator(tree, {
name: appName,
projectNameAndRootFormat: 'as-provided',
});

const tsConfig = devkit.readJson(
tree,
`apps/${appDirectory}/tsconfig.app.json`
);
const tsConfig = devkit.readJson(tree, `${appDirectory}/tsconfig.app.json`);
expect(tsConfig.compilerOptions.emitDecoratorMetadata).toBe(true);
expect(tsConfig.compilerOptions.target).toBe('es2021');
expect(tsConfig.exclude).toEqual([
Expand All @@ -60,11 +62,12 @@ describe('application generator', () => {
});

it('should add strict checks with --strict', async () => {
await applicationGenerator(tree, { name: appName, strict: true });
const tsConfig = devkit.readJson(
tree,
`apps/${appDirectory}/tsconfig.app.json`
);
await applicationGenerator(tree, {
name: appName,
strict: true,
projectNameAndRootFormat: 'as-provided',
});
const tsConfig = devkit.readJson(tree, `${appDirectory}/tsconfig.app.json`);

expect(tsConfig.compilerOptions.strictNullChecks).toBeTruthy();
expect(tsConfig.compilerOptions.noImplicitAny).toBeTruthy();
Expand All @@ -79,15 +82,22 @@ describe('application generator', () => {
it('should format files', async () => {
jest.spyOn(devkit, 'formatFiles');

await applicationGenerator(tree, { name: appName });
await applicationGenerator(tree, {
name: appName,
projectNameAndRootFormat: 'as-provided',
});

expect(devkit.formatFiles).toHaveBeenCalled();
});

it('should not format files when --skipFormat=true', async () => {
jest.spyOn(devkit, 'formatFiles');

await applicationGenerator(tree, { name: appName, skipFormat: true });
await applicationGenerator(tree, {
name: appName,
skipFormat: true,
projectNameAndRootFormat: 'as-provided',
});

expect(devkit.formatFiles).not.toHaveBeenCalled();
});
Expand All @@ -98,6 +108,7 @@ describe('application generator', () => {
await applicationGenerator(tree, {
name: appName,
e2eTestRunner: 'none',
projectNameAndRootFormat: 'as-provided',
});

const projectConfigurations = devkit.getProjects(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ exports[`convert-tslint-to-eslint should work for NestJS applications 1`] = `

exports[`convert-tslint-to-eslint should work for NestJS applications 2`] = `
{
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"$schema": "../node_modules/nx/schemas/project-schema.json",
"name": "nest-app-1",
"projectType": "application",
"root": "apps/nest-app-1",
"root": "nest-app-1",
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"options": {
"lintFilePatterns": [
"apps/nest-app-1/**/*.ts",
"nest-app-1/**/*.ts",
],
},
"outputs": [
Expand Down Expand Up @@ -246,7 +246,7 @@ exports[`convert-tslint-to-eslint should work for NestJS applications 3`] = `
exports[`convert-tslint-to-eslint should work for NestJS applications 4`] = `
{
"extends": [
"../../.eslintrc.json",
"../.eslintrc.json",
],
"ignorePatterns": [
"!**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { exampleRootTslintJson } from '@nx/linter';
import { conversionGenerator } from './convert-tslint-to-eslint';

const appProjectName = 'nest-app-1';
const appProjectRoot = `apps/${appProjectName}`;
const appProjectRoot = `${appProjectName}`;
const appProjectTSLintJsonPath = joinPathFragments(
appProjectRoot,
'tslint.json'
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('convert-tslint-to-eslint', () => {

beforeEach(async () => {
jest.spyOn(devkit, 'installPackagesTask');
host = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
host = createTreeWithEmptyWorkspace();

writeJson(host, 'tslint.json', exampleRootTslintJson.raw);

Expand All @@ -114,8 +114,8 @@ describe('convert-tslint-to-eslint', () => {
lint: {
executor: '@angular-devkit/build-angular:tslint',
options: {
exclude: ['**/node_modules/**', '!apps/nest-app-1/**/*'],
tsConfig: ['apps/nest-app-1/tsconfig.app.json'],
exclude: ['**/node_modules/**', '!nest-app-1/**/*'],
tsConfig: ['nest-app-1/tsconfig.app.json'],
},
},
},
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('convert-tslint-to-eslint', () => {
/**
* Existing tslint.json file for the app project
*/
writeJson(host, 'apps/nest-app-1/tslint.json', projectTslintJsonData.raw);
writeJson(host, 'nest-app-1/tslint.json', projectTslintJsonData.raw);
/**
* Existing tslint.json file for the lib project
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/nest/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('init generator', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
tree = createTreeWithEmptyWorkspace();
jest.clearAllMocks();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ exports[`lib --testEnvironment should set target jest testEnvironment to jsdom 1
"/* eslint-disable */
export default {
displayName: 'my-lib',
preset: '../../jest.preset.js',
preset: '../jest.preset.js',
transform: {
'^.+\\\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/my-lib',
coverageDirectory: '../coverage/my-lib',
};
"
`;
Expand All @@ -18,13 +18,13 @@ exports[`lib --testEnvironment should set target jest testEnvironment to node by
"/* eslint-disable */
export default {
displayName: 'my-lib',
preset: '../../jest.preset.js',
preset: '../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/my-lib',
coverageDirectory: '../coverage/my-lib',
};
"
`;
Expand All @@ -40,7 +40,7 @@ exports[`lib --unit-test-runner none should not generate test configuration 1`]
"noPropertyAccessFromIndexSignature": true,
"strict": true,
},
"extends": "../../tsconfig.base.json",
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
Expand All @@ -56,7 +56,7 @@ exports[`lib --unit-test-runner none should not generate test configuration 2`]
"executor": "@nx/linter:eslint",
"options": {
"lintFilePatterns": [
"libs/my-lib/**/*.ts",
"my-lib/**/*.ts",
],
},
"outputs": [
Expand All @@ -76,7 +76,7 @@ exports[`lib nested should create a local tsconfig.json 1`] = `
"noPropertyAccessFromIndexSignature": true,
"strict": true,
},
"extends": "../../../tsconfig.base.json",
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
Expand Down Expand Up @@ -114,7 +114,7 @@ exports[`lib not nested should create a local tsconfig.json 1`] = `
"noPropertyAccessFromIndexSignature": true,
"strict": true,
},
"extends": "../../tsconfig.base.json",
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
Expand All @@ -131,7 +131,7 @@ exports[`lib not nested should create a local tsconfig.json 1`] = `
exports[`lib not nested should generate files 1`] = `
{
"extends": [
"../../.eslintrc.json",
"../.eslintrc.json",
],
"ignorePatterns": [
"!**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export function toJsLibraryGeneratorOptions(
unitTestRunner: options.unitTestRunner,
config: options.standaloneConfig ? 'project' : 'workspace',
setParserOptionsProject: options.setParserOptionsProject,
projectNameAndRootFormat: options.projectNameAndRootFormat,
};
}
Loading