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(testing): ensure baseUrl is not passed to playwright cli #21943

Merged
merged 1 commit into from
Feb 22, 2024
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
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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import {
addProjectConfiguration,
readNxJson,
readProjectConfiguration,
updateNxJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import removeBaseUrlFromProjectJson from './remove-baseUrl-from-project-json';

describe('removeBaseUrlFromProjectJson', () => {
describe('--projects', () => {
it('should not update if the project is correct', async () => {
// 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);

// ACT
await 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', async () => {
// 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);

// ACT
await removeBaseUrlFromProjectJson(tree);

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

it('should remove baseUrl from target options and configurations', async () => {
// 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);

// ACT
await removeBaseUrlFromProjectJson(tree);

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

describe('--nx.json', () => {
it('should remove baseUrl from targetDefaults in nx.json', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace();
const nxJson = readNxJson(tree);
nxJson.targetDefaults['@nx/playwright:playwright'] = {
options: {
baseUrl: 'http://localhost:4200',
},
configurations: {
ci: {
baseUrl: 'http://localhost:4201',
},
},
};
nxJson.targetDefaults['e2e'] = {
executor: '@nx/playwright:playwright',
options: {
baseUrl: 'http://localhost:4200',
},
configurations: {
ci: {
baseUrl: 'http://localhost:4201',
},
},
};
updateNxJson(tree, nxJson);

// ACT
await removeBaseUrlFromProjectJson(tree);

// ASSERT
const maybeUpdatedNxJson = readNxJson(tree);
expect(maybeUpdatedNxJson.targetDefaults).toMatchInlineSnapshot(`
{
"@nx/playwright:playwright": {
"configurations": {
"ci": {},
},
"options": {},
},
"build": {
"cache": true,
},
"e2e": {
"configurations": {
"ci": {},
},
"executor": "@nx/playwright:playwright",
"options": {},
},
"lint": {
"cache": true,
},
}
`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
formatFiles,
readNxJson,
readProjectConfiguration,
type Tree,
updateNxJson,
updateProjectConfiguration,
} from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';

export default async function (tree: Tree) {
forEachExecutorOptions(
tree,
'@nx/playwright:playwright',
(options, projectName, targetName, configurationName) => {
if (options?.['baseUrl']) {
const project = readProjectConfiguration(tree, projectName);
if (configurationName) {
delete project.targets[targetName].configurations[configurationName][
'baseUrl'
];
} else {
delete project.targets[targetName].options['baseUrl'];
}

updateProjectConfiguration(tree, projectName, project);
}
}
);

const nxJson = readNxJson(tree);
for (const [targetNameOrExecutor, target] of Object.entries(
nxJson.targetDefaults
)) {
if (
targetNameOrExecutor === '@nx/playwright:playwright' ||
(target.executor && target.executor === '@nx/playwright:playwright')
) {
let updated = false;
if (target.options?.['baseUrl']) {
delete nxJson.targetDefaults[targetNameOrExecutor].options['baseUrl'];
updated = true;
}

if (target.configurations) {
for (const [configurationName, configuration] of Object.entries(
target.configurations
)) {
if (configuration['baseUrl']) {
delete nxJson.targetDefaults[targetNameOrExecutor].configurations[
configurationName
]['baseUrl'];
updated = true;
}
}
}

if (updated) {
updateNxJson(tree, nxJson);
}
}
}

await formatFiles(tree);
}