Skip to content

Commit

Permalink
fix(testing): fix misc issues in migrations (#27471)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez authored Aug 16, 2024
1 parent 69c989e commit e368cd4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import updateCiWebserverForVite from './update-ci-webserver-for-static-serve';
import updateCiWebserverForStaticServe from './update-ci-webserver-for-static-serve';
import {
type Tree,
type ProjectGraph,
Expand All @@ -16,7 +16,7 @@ jest.mock('@nx/devkit', () => ({
}),
}));

describe('updateCiWebserverForVite', () => {
describe('update-ci-webserver-for-static-serve migration', () => {
let tree: Tree;
let tempFs: TempFs;

Expand All @@ -35,6 +35,21 @@ describe('updateCiWebserverForVite', () => {
tempFs.reset();
});

it('should not error when there are no plugin registrations', async () => {
const nxJson = readNxJson(tree);
// ensure there are no plugins
delete nxJson.plugins;
updateNxJson(tree, nxJson);
addProject(tree, tempFs, {
buildTargetName: 'build',
ciTargetName: 'e2e-ci',
appName: 'app',
noVite: true,
});

await expect(updateCiWebserverForStaticServe(tree)).resolves.not.toThrow();
});

it('should update to serve-static target for webpack', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
Expand Down Expand Up @@ -63,7 +78,7 @@ describe('updateCiWebserverForVite', () => {
});

// ACT
await updateCiWebserverForVite(tree);
await updateCiWebserverForStaticServe(tree);

// ASSERT
expect(tree.read('app-e2e/cypress.config.ts', 'utf-8'))
Expand Down Expand Up @@ -137,7 +152,7 @@ export default defineConfig({
);

// ACT
await updateCiWebserverForVite(tree);
await updateCiWebserverForStaticServe(tree);

// ASSERT
expect(tree.read('app-e2e/cypress.config.ts', 'utf-8'))
Expand Down Expand Up @@ -186,7 +201,7 @@ export default defineConfig({
addProject(tree, tempFs);

// ACT
await updateCiWebserverForVite(tree);
await updateCiWebserverForStaticServe(tree);

// ASSERT
expect(tree.read('app-e2e/cypress.config.ts', 'utf-8'))
Expand Down Expand Up @@ -234,7 +249,7 @@ export default defineConfig({
});

// ACT
await updateCiWebserverForVite(tree);
await updateCiWebserverForStaticServe(tree);

// ASSERT
expect(tree.read('app-e2e/cypress.config.ts', 'utf-8'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default async function (tree: Tree) {
typeof p === 'string' ? p === pluginName : p.plugin === pluginName
);

if (!matchingPluginRegistrations?.length) {
return;
}

const {
createNodesV2,
}: { createNodesV2: CreateNodesV2<CypressPluginOptions> } = await import(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,74 @@ describe('add-e2e-ci-target-defaults', () => {
`);
});

it.each`
webServerCommand
${`'npx nx run app:serve-static'`}
${`"npx nx run app:serve-static"`}
${`'npx nx serve-static app'`}
${`"npx nx serve-static app"`}
`(
'should handle the webServerCommand $webServerCommand',
async ({ webServerCommand }) => {
const nxJson = readNxJson(tree);
nxJson.plugins = [
{
plugin: '@nx/playwright/plugin',
options: { targetName: 'e2e', ciTargetName: 'e2e-ci' },
},
];
updateNxJson(tree, nxJson);
addProject(tree, tempFs);
tree.write(
`app-e2e/playwright.config.ts`,
`import { defineConfig, devices } from '@playwright/test';
import { nxE2EPreset } from '@nx/playwright/preset';
import { workspaceRoot } from '@nx/devkit';
const baseURL = process.env['BASE_URL'] || 'http://localhost:4200';
export default defineConfig({
...nxE2EPreset(__filename, { testDir: './src' }),
use: {
baseURL,
trace: 'on-first-retry',
},
webServer: {
command: ${webServerCommand},
url: 'http://localhost:4200',
reuseExistingServer: !process.env.CI,
cwd: workspaceRoot,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
});`
);

await addE2eCiTargetDefaults(tree);

expect(readNxJson(tree).targetDefaults).toMatchInlineSnapshot(`
{
"build": {
"cache": true,
},
"e2e-ci--**/*": {
"dependsOn": [
"^build",
],
},
"lint": {
"cache": true,
},
}
`);
}
);

it('should add the targetDefaults with the correct ciTargetNames and buildTargets when there is more than one plugin', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default async function addE2eCiTargetDefaults(tree: Tree) {
if (!nodes.length) {
continue;
}
const ciWebServerCommand = nodes[0].getText();
const ciWebServerCommand = nodes[0].getText().replace(/['"]/g, '');
let serveStaticProject: string;
let serveStaticTarget: string;
let serveStaticConfiguration: string;
Expand Down Expand Up @@ -95,7 +95,7 @@ export default async function addE2eCiTargetDefaults(tree: Tree) {
}

const resolvedServeStaticTarget =
graph.nodes[serveStaticProject].data.targets[serveStaticTarget];
graph.nodes[serveStaticProject]?.data?.targets?.[serveStaticTarget];
if (!resolvedServeStaticTarget) {
continue;
}
Expand Down

0 comments on commit e368cd4

Please sign in to comment.