Skip to content

Commit

Permalink
fix(testing): use @nrwl/web:webpack utils to generate a more robust w…
Browse files Browse the repository at this point in the history
…ebpack config

fixes: nrwl#11372
  • Loading branch information
barbados-clemens committed Aug 29, 2022
1 parent 4dfd0f3 commit fc72fba
Show file tree
Hide file tree
Showing 17 changed files with 1,359 additions and 284 deletions.
7 changes: 6 additions & 1 deletion docs/generated/packages/react.json
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@
"name": "cypress-component-configuration",
"factory": "./src/generators/cypress-component-configuration/cypress-component-configuration#cypressComponentConfigGenerator",
"schema": {
"$schema": "http://json-schema.org/schema",
"$schema": "https://json-schema.org/schema",
"cli": "nx",
"$id": "NxReactCypressComponentTestConfiguration",
"title": "Add Cypress component testing",
Expand All @@ -1230,6 +1230,11 @@
"$default": { "$source": "projectName" },
"x-prompt": "What project should we add Cypress component testing to?"
},
"buildTarget": {
"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.",
"pattern": "^[^:\\s]+:[^:\\s]+(:\\S+)?$"
},
"generateTests": {
"type": "boolean",
"description": "Generate default component tests for existing components in the project",
Expand Down
27 changes: 24 additions & 3 deletions e2e/react/src/cypress-component-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { newProject, runCLI, uniq } from '../../utils';
import { createFile, newProject, runCLI, uniq, updateFile } from '../../utils';

describe('React Cypress Component Tests', () => {
beforeAll(() => newProject());
Expand All @@ -17,14 +17,35 @@ describe('React Cypress Component Tests', () => {
);
}, 1000000);

it('should successfully test react app', () => {
it('should successfully test react lib', () => {
const libName = uniq('cy-react-lib');
const appName = uniq('cy-react-app-target');
runCLI(`generate @nrwl/react:app ${appName} --no-interactive`);
runCLI(`generate @nrwl/react:lib ${libName} --component --no-interactive`);
runCLI(
`generate @nrwl/react:setup-tailwind --project=${libName} --no-interactive`
);
runCLI(
`generate @nrwl/react:component fancy-component --project=${libName} --no-interactive`
);
createFile(
`libs/${libName}/src/styles.css`,
`
@tailwind components;
@tailwind base;
@tailwind utilities;
`
);
updateFile(
`libs/${libName}/src/lib/fancy-component/fancy-component.tsx`,
(content) => {
return `
import '../../styles.css';
${content}`;
}
);
runCLI(
`generate @nrwl/react:cypress-component-configuration --project=${libName} --generate-tests`
`generate @nrwl/react:cypress-component-configuration --project=${libName} --build-target=${appName}:build --generate-tests`
);
expect(runCLI(`component-test ${libName} --no-watch`)).toContain(
'All specs passed!'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export class E2eMigrator extends ProjectMigrator<SupportedTargets> {
}

private updateCypress10ConfigFile(configFilePath: string): void {
this.cypressPreset = nxE2EPreset(this.project.newRoot);
this.cypressPreset = nxE2EPreset(configFilePath);

const fileContent = this.tree.read(configFilePath, 'utf-8');
let sourceFile = tsquery.ast(fileContent);
Expand Down
6 changes: 6 additions & 0 deletions packages/cypress/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"version": "12.8.0-beta.0",
"description": "Remove Typescript Preprocessor Plugin",
"factory": "./src/migrations/update-12-8-0/remove-typescript-plugin"
},
"update-cypress-configs-preset": {
"cli": "nx",
"version": "14.6.1-beta.0",
"description": "Change Cypress e2e and component testing presets to use __filename instead of __dirname and include a devServerTarget for component testing.",
"factory": "./src/migrations/update-14-6-1/update-cypress-configs-presets"
}
},
"packageJsonUpdates": {
Expand Down
11 changes: 8 additions & 3 deletions packages/cypress/plugins/cypress-preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { workspaceRoot } from '@nrwl/devkit';
import { join, relative } from 'path';
import { dirname, extname, join, relative } from 'path';
import { lstatSync } from 'fs';

interface BaseCypressPreset {
videosFolder: string;
Expand All @@ -9,8 +10,12 @@ interface BaseCypressPreset {
}

export function nxBaseCypressPreset(pathToConfig: string): BaseCypressPreset {
const projectPath = relative(workspaceRoot, pathToConfig);
const offset = relative(pathToConfig, workspaceRoot);
// prevent from placing path outside the root of the workspace
// if they pass in a file or directory
const normalizedPath =
extname(pathToConfig) === '' ? pathToConfig : dirname(pathToConfig);
const projectPath = relative(workspaceRoot, normalizedPath);
const offset = relative(normalizedPath, workspaceRoot);
const videosFolder = join(offset, 'dist', 'cypress', projectPath, 'videos');
const screenshotsFolder = join(
offset,
Expand Down
Loading

0 comments on commit fc72fba

Please sign in to comment.