Skip to content

Commit

Permalink
fix(testing): ensure baseUrl is not passed to playwright cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 22, 2024
1 parent 5d6abe4 commit 415dcbd
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/playwright/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"version": "17.3.1-beta.0",
"description": "Add project property to playwright config",
"implementation": "./src/migrations/update-17-3-1/add-project-to-config"
},
"18-1-0-remove-baseUrl-from-project-json": {
"cli": "nx",
"version": "18.1.0-beta.3",
"description": "Remove invalid baseUrl option from @nx/playwright:playwright targets in project.json.",
"implementation": "./src/migrations/update-18-1-0/remove-baseUrl-from-project-json"
}
}
}
11 changes: 11 additions & 0 deletions packages/playwright/src/executors/playwright/playwright.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ function createArgs(
}

for (const key in rest) {
if (key === 'baseUrl') {
// baseUrl should not exist as dictated by the Schema
// However, it does exist in some existing workspaces
console.warn(
"'baseUrl' is not a valid option for @nx/playwright:playwright. Please remove this from your project.json."
);
console.warn(
"You can configure 'baseUrl' in the 'playwright.config.ts' file."
);
continue;
}
if (exclude.includes(key)) continue;

const value = opts[key];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { addProjectConfiguration, readProjectConfiguration } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import removeBaseUrlFromProjectJson from './remove-baseUrl-from-project-json';

describe('removeBaseUrlFromProjectJson', () => {
it('should not update if the project is correct', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
const project = {
$schema: 'node_modules/nx/schemas/project-schema.json',
name: 'test',
root: '.',
sourceRoot: '',
targets: {
e2e: {
executor: '@nx/playwright:playwright',
},
},
};

addProjectConfiguration(tree, 'test', project);
tree.write('playwright.config.ts', '');

// ACT
removeBaseUrlFromProjectJson(tree);

// ASSERT
const maybeUpdatedProject = readProjectConfiguration(tree, 'test');
expect(maybeUpdatedProject).toMatchInlineSnapshot(`
{
"$schema": "node_modules/nx/schemas/project-schema.json",
"name": "test",
"root": ".",
"sourceRoot": "",
"targets": {
"e2e": {
"executor": "@nx/playwright:playwright",
},
},
}
`);
});

it('should remove baseUrl from target options', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
const project = {
$schema: 'node_modules/nx/schemas/project-schema.json',
name: 'test',
root: '.',
sourceRoot: '',
targets: {
e2e: {
executor: '@nx/playwright:playwright',
options: {
baseUrl: 'http://localhost:4200',
},
},
},
};

addProjectConfiguration(tree, 'test', project);
tree.write('playwright.config.ts', '');

// ACT
removeBaseUrlFromProjectJson(tree);

// ASSERT
const maybeUpdatedProject = readProjectConfiguration(tree, 'test');
expect(maybeUpdatedProject.targets['e2e'].options).toMatchInlineSnapshot(
`{}`
);
});

it('should remove baseUrl from target options and configurations', () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
const project = {
$schema: 'node_modules/nx/schemas/project-schema.json',
name: 'test',
root: '.',
sourceRoot: '',
targets: {
e2e: {
executor: '@nx/playwright:playwright',
options: {
baseUrl: 'http://localhost:4200',
},
configurations: {
production: {
baseUrl: 'https://mytest.com',
},
},
},
},
};

addProjectConfiguration(tree, 'test', project);
tree.write('playwright.config.ts', '');

// ACT
removeBaseUrlFromProjectJson(tree);

// ASSERT
const maybeUpdatedProject = readProjectConfiguration(tree, 'test');
expect(maybeUpdatedProject.targets['e2e'].options).toMatchInlineSnapshot(
`{}`
);
expect(
maybeUpdatedProject.targets['e2e'].configurations['production']
).toMatchInlineSnapshot(`{}`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
getProjects,
joinPathFragments,
type ProjectConfiguration,
type Tree,
updateProjectConfiguration,
} from '@nx/devkit';

export default function (tree: Tree) {
const playwrightProjects: ProjectConfiguration[] = [];
const projects = getProjects(tree);
projects.forEach((project) => {
// Check if the project contains playwright config
const configPath = joinPathFragments(project.root, 'playwright.config.ts');
if (tree.exists(configPath)) {
playwrightProjects.push(project);
}
});

for (const project of playwrightProjects) {
for (const [targetName, target] of Object.entries(project.targets)) {
if (target.executor !== '@nx/playwright:playwright') {
continue;
}

let updatedProject = false;
if (target.options?.['baseUrl']) {
delete project.targets[targetName].options['baseUrl'];
updatedProject = true;
}

if (
project.targets[targetName].configurations &&
Object.keys(project.targets[targetName].configurations).length > 0
) {
for (const [configurationName, configuration] of Object.entries(
project.targets[targetName].configurations
)) {
if (configuration['baseUrl']) {
delete project.targets[targetName].configurations[
configurationName
]['baseUrl'];
updatedProject = true;
}
}
}

if (updatedProject) {
updateProjectConfiguration(tree, project.name, project);
}
}
}
}

0 comments on commit 415dcbd

Please sign in to comment.