fix(testing): normalize paths for component-testing (#11963)

* fix(testing): normalize paths before accessing the project graph

* fix(testing): return object with offset input instead of string

* fix(testing): update schemas to provide better prompts
This commit is contained in:
Caleb Ukle 2022-09-13 09:17:49 -05:00 committed by GitHub
parent 5bc9a6f5eb
commit 1a9df531b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 42 deletions

View File

@ -526,17 +526,20 @@
"componentName": { "componentName": {
"type": "string", "type": "string",
"description": "Class name of the component to create a test for.", "description": "Class name of the component to create a test for.",
"examples": ["MyFancyButtonComponent"] "examples": ["MyFancyButtonComponent"],
"x-prompt": "What is the class name of the component to create a test for?"
}, },
"componentDir": { "componentDir": {
"type": "string", "type": "string",
"description": "Relative path to the folder that contains the component from the project root.", "description": "Relative path to the folder that contains the component from the project root.",
"example": ["src/lib/my-fancy-button"] "examples": ["src/lib/my-fancy-button"],
"x-prompt": "What is the path to the component directory from the project root?"
}, },
"componentFileName": { "componentFileName": {
"type": "string", "type": "string",
"description": "File name that contains the component without the `.ts` extension.", "description": "File name that contains the component without the `.ts` extension.",
"examples": ["my-fancy-button.component"] "examples": ["my-fancy-button.component"],
"x-prompt": "What is the file name that contains the component?"
}, },
"skipFormat": { "skipFormat": {
"description": "Skip formatting files.", "description": "Skip formatting files.",
@ -546,9 +549,9 @@
}, },
"additionalProperties": false, "additionalProperties": false,
"required": [ "required": [
"projectPath", "project",
"componentName", "componentName",
"componentPath", "componentDir",
"componentFileName" "componentFileName"
], ],
"presets": [] "presets": []
@ -2193,12 +2196,13 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project to add cypress component testing configuration to", "description": "The name of the project to add cypress component testing configuration to",
"$default": { "$source": "projectName" }, "x-dropdown": "projects",
"x-prompt": "What project should we add Cypress component testing to?" "x-prompt": "What project should we add Cypress component testing to?"
}, },
"generateTests": { "generateTests": {
"type": "boolean", "type": "boolean",
"description": "Generate default component tests for existing components in the project", "description": "Generate default component tests for existing components in the project",
"x-prompt": "Automatically generate tests for components declared in this project?",
"default": false "default": false
}, },
"buildTarget": { "buildTarget": {

View File

@ -1227,19 +1227,20 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project to add cypress component testing configuration to", "description": "The name of the project to add cypress component testing configuration to",
"$default": { "$source": "projectName" }, "x-dropdown": "projects",
"x-prompt": "What project should we add Cypress component testing to?" "x-prompt": "What project should we add Cypress component testing to?"
}, },
"generateTests": {
"type": "boolean",
"description": "Generate default component tests for existing components in the project",
"x-prompt": "Automatically generate tests for components declared in this project?",
"default": false
},
"buildTarget": { "buildTarget": {
"type": "string", "type": "string",
"description": "A build target used to configure Cypress component testing in the format of `project:target[:configuration]`. The build target should be from a React app. If not provided we will try to infer it from your projects usage.", "description": "A build target used to configure Cypress component testing in the format of `project:target[:configuration]`. The build target should be from a React app. If not provided we will try to infer it from your projects usage.",
"pattern": "^[^:\\s]+:[^:\\s]+(:\\S+)?$" "pattern": "^[^:\\s]+:[^:\\s]+(:\\S+)?$"
}, },
"generateTests": {
"type": "boolean",
"description": "Generate default component tests for existing components in the project",
"default": false
},
"skipFormat": { "skipFormat": {
"type": "boolean", "type": "boolean",
"description": "Skip formatting files", "description": "Skip formatting files",
@ -1275,7 +1276,7 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project the component is apart of", "description": "The name of the project the component is apart of",
"$default": { "$source": "projectName" }, "x-dropdown": "projects",
"x-prompt": "What project is this component apart of?" "x-prompt": "What project is this component apart of?"
}, },
"componentPath": { "componentPath": {

View File

@ -5,6 +5,7 @@ import {
runCLI, runCLI,
uniq, uniq,
updateFile, updateFile,
updateProjectConfig,
} from '../../utils'; } from '../../utils';
import { names } from '@nrwl/devkit'; import { names } from '@nrwl/devkit';
describe('Angular Cypress Component Tests', () => { describe('Angular Cypress Component Tests', () => {
@ -130,6 +131,17 @@ import {CommonModule} from '@angular/common';
} }
` `
); );
// make sure assets from the workspace root work.
createFile('libs/assets/data.json', JSON.stringify({ data: 'data' }));
updateProjectConfig(appName, (config) => {
config.targets['build'].options.assets.push({
glob: '**/*',
input: 'libs/assets',
output: 'assets',
});
return config;
});
}); });
it('should test app', () => { it('should test app', () => {

View File

@ -196,17 +196,17 @@ function normalizeBuildTargetOptions(
buildOptions.assets = buildOptions.assets.map((asset) => { buildOptions.assets = buildOptions.assets.map((asset) => {
return typeof asset === 'string' return typeof asset === 'string'
? joinPathFragments(offset, asset) ? joinPathFragments(offset, asset)
: (asset.input = joinPathFragments(offset, asset.input)); : { ...asset, input: joinPathFragments(offset, asset.input) };
}); });
buildOptions.styles = buildOptions.styles.map((style) => { buildOptions.styles = buildOptions.styles.map((style) => {
return typeof style === 'string' return typeof style === 'string'
? joinPathFragments(offset, style) ? joinPathFragments(offset, style)
: (style.input = joinPathFragments(offset, style.input)); : { ...style, input: joinPathFragments(offset, style.input) };
}); });
buildOptions.scripts = buildOptions.scripts.map((script) => { buildOptions.scripts = buildOptions.scripts.map((script) => {
return typeof script === 'string' return typeof script === 'string'
? joinPathFragments(offset, script) ? joinPathFragments(offset, script)
: (script.input = joinPathFragments(offset, script.input)); : { ...script, input: joinPathFragments(offset, script.input) };
}); });
} else { } else {
const stylePath = getTempStylesForTailwind(ctContext); const stylePath = getTempStylesForTailwind(ctContext);

View File

@ -14,17 +14,20 @@
"componentName": { "componentName": {
"type": "string", "type": "string",
"description": "Class name of the component to create a test for.", "description": "Class name of the component to create a test for.",
"examples": ["MyFancyButtonComponent"] "examples": ["MyFancyButtonComponent"],
"x-prompt": "What is the class name of the component to create a test for?"
}, },
"componentDir": { "componentDir": {
"type": "string", "type": "string",
"description": "Relative path to the folder that contains the component from the project root.", "description": "Relative path to the folder that contains the component from the project root.",
"example": ["src/lib/my-fancy-button"] "examples": ["src/lib/my-fancy-button"],
"x-prompt": "What is the path to the component directory from the project root?"
}, },
"componentFileName": { "componentFileName": {
"type": "string", "type": "string",
"description": "File name that contains the component without the `.ts` extension.", "description": "File name that contains the component without the `.ts` extension.",
"examples": ["my-fancy-button.component"] "examples": ["my-fancy-button.component"],
"x-prompt": "What is the file name that contains the component?"
}, },
"skipFormat": { "skipFormat": {
"description": "Skip formatting files.", "description": "Skip formatting files.",
@ -33,10 +36,5 @@
} }
}, },
"additionalProperties": false, "additionalProperties": false,
"required": [ "required": ["project", "componentName", "componentDir", "componentFileName"]
"projectPath",
"componentName",
"componentPath",
"componentFileName"
]
} }

View File

@ -9,14 +9,13 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project to add cypress component testing configuration to", "description": "The name of the project to add cypress component testing configuration to",
"$default": { "x-dropdown": "projects",
"$source": "projectName"
},
"x-prompt": "What project should we add Cypress component testing to?" "x-prompt": "What project should we add Cypress component testing to?"
}, },
"generateTests": { "generateTests": {
"type": "boolean", "type": "boolean",
"description": "Generate default component tests for existing components in the project", "description": "Generate default component tests for existing components in the project",
"x-prompt": "Automatically generate tests for components declared in this project?",
"default": false "default": false
}, },
"buildTarget": { "buildTarget": {

View File

@ -1,5 +1,6 @@
import { import {
ExecutorContext, ExecutorContext,
normalizePath,
ProjectConfiguration, ProjectConfiguration,
ProjectGraph, ProjectGraph,
readNxJson, readNxJson,
@ -83,9 +84,11 @@ export function getProjectConfigByPath(
configPath: string configPath: string
): ProjectConfiguration { ): ProjectConfiguration {
const configFileFromWorkspaceRoot = relative(workspaceRoot, configPath); const configFileFromWorkspaceRoot = relative(workspaceRoot, configPath);
const normalizedPathFromWorkspaceRoot = lstatSync(configPath).isFile() const normalizedPathFromWorkspaceRoot = normalizePath(
? configFileFromWorkspaceRoot.replace(extname(configPath), '') lstatSync(configPath).isFile()
: configFileFromWorkspaceRoot; ? configFileFromWorkspaceRoot.replace(extname(configPath), '')
: configFileFromWorkspaceRoot
);
const mappedGraph = mapProjectGraphFiles(graph); const mappedGraph = mapProjectGraphFiles(graph);
const componentTestingProjectName = const componentTestingProjectName =

View File

@ -15,9 +15,7 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project the component is apart of", "description": "The name of the project the component is apart of",
"$default": { "x-dropdown": "projects",
"$source": "projectName"
},
"x-prompt": "What project is this component apart of?" "x-prompt": "What project is this component apart of?"
}, },
"componentPath": { "componentPath": {

View File

@ -19,21 +19,20 @@
"project": { "project": {
"type": "string", "type": "string",
"description": "The name of the project to add cypress component testing configuration to", "description": "The name of the project to add cypress component testing configuration to",
"$default": { "x-dropdown": "projects",
"$source": "projectName"
},
"x-prompt": "What project should we add Cypress component testing to?" "x-prompt": "What project should we add Cypress component testing to?"
}, },
"generateTests": {
"type": "boolean",
"description": "Generate default component tests for existing components in the project",
"x-prompt": "Automatically generate tests for components declared in this project?",
"default": false
},
"buildTarget": { "buildTarget": {
"type": "string", "type": "string",
"description": "A build target used to configure Cypress component testing in the format of `project:target[:configuration]`. The build target should be from a React app. If not provided we will try to infer it from your projects usage.", "description": "A build target used to configure Cypress component testing in the format of `project:target[:configuration]`. The build target should be from a React app. If not provided we will try to infer it from your projects usage.",
"pattern": "^[^:\\s]+:[^:\\s]+(:\\S+)?$" "pattern": "^[^:\\s]+:[^:\\s]+(:\\S+)?$"
}, },
"generateTests": {
"type": "boolean",
"description": "Generate default component tests for existing components in the project",
"default": false
},
"skipFormat": { "skipFormat": {
"type": "boolean", "type": "boolean",
"description": "Skip formatting files", "description": "Skip formatting files",