-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(testing): ensure baseUrl is not passed to playwright cli (#21943)
- Loading branch information
Showing
3 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
packages/playwright/src/migrations/update-18-1-0/remove-baseUrl-from-project-json.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
packages/playwright/src/migrations/update-18-1-0/remove-baseUrl-from-project-json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |