nx/packages/devkit/src/generators/e2e-web-server-info-utils.spec.ts
Colum Ferry 320d9f223f
fix(testing): application generators should accurately configure e2e projects (#27453)
- feat(devkit): add util for determining the e2e web server info
- feat(vite): add util for determining the e2e web server info
- feat(webpack): add util for determining the e2e web server info
- fix(webpack): allow port override
- fix(devkit): e2e web server info util should handle target defaults
- feat(webpack): export the e2e web server info utils
- fix(vite): rename util
- fix(devkit): util should determine the devTarget for cypress
- fix(react): improve accuracy of e2e project generation

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
The logic for finding the correct targets and web addresses to use when
setting up e2e projects is flawed and missing some key considerations.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
The logic is accurate and usage is simplified across plugins 

Projects:
- [x] Angular
- [x] Expo
- [x] Next
- [x] Nuxt
- [x] Vue
- [x] Web
- [x] Remix
- [x] React
- [x] React Native


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-08-27 10:00:43 -04:00

249 lines
7.1 KiB
TypeScript

import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports';
import { type Tree, readNxJson, updateNxJson } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
import { getE2EWebServerInfo } from './e2e-web-server-info-utils';
describe('getE2EWebServerInfo', () => {
let tree: Tree;
let tempFs: TempFs;
beforeEach(() => {
tempFs = new TempFs('e2e-webserver-info');
tree = createTreeWithEmptyWorkspace();
tree.root = tempFs.tempDir;
tree.write(`app/vite.config.ts`, ``);
tempFs.createFileSync(`app/vite.config.ts`, ``);
});
afterEach(() => {
tempFs.cleanup();
jest.resetModules();
});
it('should use the default values when no plugin is registered and plugins are not being used', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
updateNxJson(tree, nxJson);
// ACT
const e2eWebServerInfo = await getE2EWebServerInfo(
tree,
'app',
{
plugin: '@nx/vite/plugin',
configFilePath: 'app/vite.config.ts',
serveTargetName: 'serveTargetName',
serveStaticTargetName: 'previewTargetName',
},
{
defaultServeTargetName: 'serve',
defaultServeStaticTargetName: 'preview',
defaultE2EWebServerAddress: 'http://localhost:4200',
defaultE2ECiBaseUrl: 'http://localhost:4300',
defaultE2EPort: 4200,
},
false
);
// ASSERT
expect(e2eWebServerInfo).toMatchInlineSnapshot(`
{
"e2eCiBaseUrl": "http://localhost:4300",
"e2eCiWebServerCommand": "npx nx run app:preview",
"e2eDevServerTarget": "app:serve",
"e2eWebServerAddress": "http://localhost:4200",
"e2eWebServerCommand": "npx nx run app:serve",
}
`);
});
it('should use the default values of the plugin when the plugin is just a string', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.plugins = ['@nx/vite/plugin'];
updateNxJson(tree, nxJson);
// ACT
const e2eWebServerInfo = await getE2EWebServerInfo(
tree,
'app',
{
plugin: '@nx/vite/plugin',
configFilePath: 'app/vite.config.ts',
serveTargetName: 'serveTargetName',
serveStaticTargetName: 'previewTargetName',
},
{
defaultServeTargetName: 'serve',
defaultServeStaticTargetName: 'preview',
defaultE2EWebServerAddress: 'http://localhost:4200',
defaultE2ECiBaseUrl: 'http://localhost:4300',
defaultE2EPort: 4200,
},
true
);
// ASSERT
expect(e2eWebServerInfo).toMatchInlineSnapshot(`
{
"e2eCiBaseUrl": "http://localhost:4300",
"e2eCiWebServerCommand": "npx nx run app:preview",
"e2eDevServerTarget": "app:serve",
"e2eWebServerAddress": "http://localhost:4200",
"e2eWebServerCommand": "npx nx run app:serve",
}
`);
});
it('should use the values of the registered plugin when there is no includes or excludes defined', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx/vite/plugin',
options: {
serveTargetName: 'vite:serve',
previewTargetName: 'vite:preview',
},
});
updateNxJson(tree, nxJson);
// ACT
const e2eWebServerInfo = await getE2EWebServerInfo(
tree,
'app',
{
plugin: '@nx/vite/plugin',
configFilePath: 'app/vite.config.ts',
serveTargetName: 'serveTargetName',
serveStaticTargetName: 'previewTargetName',
},
{
defaultServeTargetName: 'serve',
defaultServeStaticTargetName: 'preview',
defaultE2EWebServerAddress: 'http://localhost:4200',
defaultE2ECiBaseUrl: 'http://localhost:4300',
defaultE2EPort: 4200,
},
true
);
// ASSERT
expect(e2eWebServerInfo).toMatchInlineSnapshot(`
{
"e2eCiBaseUrl": "http://localhost:4300",
"e2eCiWebServerCommand": "npx nx run app:vite:preview",
"e2eDevServerTarget": "app:vite:serve",
"e2eWebServerAddress": "http://localhost:4200",
"e2eWebServerCommand": "npx nx run app:vite:serve",
}
`);
});
it('should handle targetDefaults', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx/vite/plugin',
options: {
serveTargetName: 'vite:serve',
previewTargetName: 'vite:preview',
},
});
nxJson.targetDefaults ??= {};
nxJson.targetDefaults['vite:serve'] = {
options: {
port: 4400,
},
};
updateNxJson(tree, nxJson);
// ACT
const e2eWebServerInfo = await getE2EWebServerInfo(
tree,
'app',
{
plugin: '@nx/vite/plugin',
configFilePath: 'app/vite.config.ts',
serveTargetName: 'serveTargetName',
serveStaticTargetName: 'previewTargetName',
},
{
defaultServeTargetName: 'serve',
defaultServeStaticTargetName: 'preview',
defaultE2EWebServerAddress: 'http://localhost:4200',
defaultE2ECiBaseUrl: 'http://localhost:4300',
defaultE2EPort: 4200,
},
true
);
// ASSERT
expect(e2eWebServerInfo).toMatchInlineSnapshot(`
{
"e2eCiBaseUrl": "http://localhost:4300",
"e2eCiWebServerCommand": "npx nx run app:vite:preview",
"e2eDevServerTarget": "app:vite:serve",
"e2eWebServerAddress": "http://localhost:4400",
"e2eWebServerCommand": "npx nx run app:vite:serve",
}
`);
});
it('should use the values of the correct registered plugin when there are includes or excludes defined', async () => {
// ARRANGE
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx/vite/plugin',
options: {
serveTargetName: 'vite:serve',
previewTargetName: 'vite:preview',
},
include: ['libs/**'],
});
nxJson.plugins.push({
plugin: '@nx/vite/plugin',
options: {
serveTargetName: 'vite-serve',
previewTargetName: 'vite-preview',
},
include: ['app/**'],
});
updateNxJson(tree, nxJson);
// ACT
const e2eWebServerInfo = await getE2EWebServerInfo(
tree,
'app',
{
plugin: '@nx/vite/plugin',
configFilePath: 'app/vite.config.ts',
serveTargetName: 'serveTargetName',
serveStaticTargetName: 'previewTargetName',
},
{
defaultServeTargetName: 'serve',
defaultServeStaticTargetName: 'preview',
defaultE2EWebServerAddress: 'http://localhost:4200',
defaultE2ECiBaseUrl: 'http://localhost:4300',
defaultE2EPort: 4400,
},
true
);
// ASSERT
expect(e2eWebServerInfo).toMatchInlineSnapshot(`
{
"e2eCiBaseUrl": "http://localhost:4300",
"e2eCiWebServerCommand": "npx nx run app:vite-preview",
"e2eDevServerTarget": "app:vite-serve",
"e2eWebServerAddress": "http://localhost:4400",
"e2eWebServerCommand": "npx nx run app:vite-serve",
}
`);
});
});