Skip to content

Commit

Permalink
fix(web): playwright should use vite preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Jul 31, 2024
1 parent 2033eb5 commit c7c535e
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,151 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`app not nested should generate files if bundler is vite 1`] = `
"import { defineConfig, devices } from '@playwright/test';
import { nxE2EPreset } from '@nx/playwright/preset';
import { workspaceRoot } from '@nx/devkit';
// For CI, you may want to set BASE_URL to the deployed application.
const baseURL = process.env['BASE_URL'] || 'http://localhost:4300';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
...nxE2EPreset(__filename, { testDir: './src' }),
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
baseURL,
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Run your local dev server before starting the tests */
webServer: {
command: 'npx nx my-app:preview',
url: 'http://localhost:4300',
reuseExistingServer: !process.env.CI,
cwd: workspaceRoot,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
// Uncomment for mobile browsers support
/* {
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 12'] },
}, */
// Uncomment for branded browsers
/* {
name: 'Microsoft Edge',
use: { ...devices['Desktop Edge'], channel: 'msedge' },
},
{
name: 'Google Chrome',
use: { ...devices['Desktop Chrome'], channel: 'chrome' },
} */
],
});
"
`;

exports[`app not nested should use serve target and port if bundler=vite, e2eTestRunner=playwright, addPlugin=false 1`] = `
"import { defineConfig, devices } from '@playwright/test';
import { nxE2EPreset } from '@nx/playwright/preset';
import { workspaceRoot } from '@nx/devkit';
// For CI, you may want to set BASE_URL to the deployed application.
const baseURL = process.env['BASE_URL'] || 'http://localhost:4200';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
...nxE2EPreset(__filename, { testDir: './src' }),
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
baseURL,
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Run your local dev server before starting the tests */
webServer: {
command: 'npx nx my-app:serve',
url: 'http://localhost:4200',
reuseExistingServer: !process.env.CI,
cwd: workspaceRoot,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
// Uncomment for mobile browsers support
/* {
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
{
name: 'Mobile Safari',
use: { ...devices['iPhone 12'] },
}, */
// Uncomment for branded browsers
/* {
name: 'Microsoft Edge',
use: { ...devices['Desktop Edge'], channel: 'msedge' },
},
{
name: 'Google Chrome',
use: { ...devices['Desktop Chrome'], channel: 'chrome' },
} */
],
});
"
`;

exports[`app setup web app with --bundler=vite should setup vite configuration 1`] = `null`;

exports[`app should setup eslint 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ describe('web app generator (legacy)', () => {
},
},
"defaultConfiguration": "development",
"dependsOn": [
"build",
],
"executor": "@nx/vite:preview-server",
"options": {
"buildTarget": "my-vite-app:build",
Expand Down
35 changes: 33 additions & 2 deletions packages/web/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'nx/src/internal-testing-utils/mock-project-graph';

import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version';
import { readProjectConfiguration, Tree } from '@nx/devkit';
import {
readNxJson,
readProjectConfiguration,
Tree,
updateNxJson,
} from '@nx/devkit';
import { getProjects, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';

Expand Down Expand Up @@ -159,10 +164,22 @@ describe('app', () => {
});

it('should generate files if bundler is vite', async () => {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];
nxJson.plugins.push({
plugin: '@nx/vite/plugin',
options: {
buildTargetName: 'build',
previewTargetName: 'preview',
},
});
updateNxJson(tree, nxJson);
await applicationGenerator(tree, {
name: 'my-app',
bundler: 'vite',
projectNameAndRootFormat: 'as-provided',
e2eTestRunner: 'playwright',
addPlugin: true,
});
expect(tree.exists('my-app/src/main.ts')).toBeTruthy();
expect(tree.exists('my-app/src/app/app.element.ts')).toBeTruthy();
Expand All @@ -179,7 +196,9 @@ describe('app', () => {
path: './tsconfig.spec.json',
},
]);
expect(tree.exists('my-app-e2e/playwright.config.ts')).toBeTruthy();
expect(
tree.read('my-app-e2e/playwright.config.ts', 'utf-8')
).toMatchSnapshot();
expect(tree.exists('my-app/index.html')).toBeTruthy();
expect(tree.exists('my-app/vite.config.ts')).toBeTruthy();
expect(tree.exists(`my-app/environments/environment.ts`)).toBeFalsy();
Expand All @@ -188,6 +207,18 @@ describe('app', () => {
).toBeFalsy();
});

it('should use serve target and port if bundler=vite, e2eTestRunner=playwright, addPlugin=false', async () => {
await applicationGenerator(tree, {
name: 'my-app',
bundler: 'vite',
projectNameAndRootFormat: 'as-provided',
e2eTestRunner: 'playwright',
});
expect(
tree.read('my-app-e2e/playwright.config.ts', 'utf-8')
).toMatchSnapshot();
});

it('should extend from root tsconfig.json when no tsconfig.base.json', async () => {
tree.rename('tsconfig.base.json', 'tsconfig.json');

Expand Down
23 changes: 16 additions & 7 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
linter: options.linter,
setParserOptionsProject: options.setParserOptionsProject,
webServerCommand: `${getPackageManagerCommand().exec} nx ${
options.e2eWebServerTarget
} ${options.name}`,
options.projectName
}:${options.e2eWebServerTarget}`,
webServerAddress: options.e2eWebServerAddress,
addPlugin: options.addPlugin,
});
Expand Down Expand Up @@ -493,18 +493,28 @@ async function normalizeOptions(
nxJson.useInferencePlugins !== false;
options.addPlugin ??= addPluginDefault;

let e2ePort = 4200;
let e2eWebServerTarget = 'serve';
if (options.addPlugin) {
if (nxJson.plugins) {
for (const plugin of nxJson.plugins) {
if (
options.bundler === 'vite' &&
typeof plugin === 'object' &&
plugin.plugin === '@nx/vite/plugin' &&
(plugin.options as VitePluginOptions).serveTargetName
plugin.plugin === '@nx/vite/plugin'
) {
e2eWebServerTarget = (plugin.options as VitePluginOptions)
.serveTargetName;
e2eWebServerTarget =
options.e2eTestRunner === 'playwright'
? (plugin.options as VitePluginOptions).previewTargetName ??
'preview'
: (plugin.options as VitePluginOptions).serveTargetName ??
'serve';
e2ePort =
e2eWebServerTarget ===
(plugin.options as VitePluginOptions)?.previewTargetName ||
e2eWebServerTarget === 'preview'
? 4300
: e2ePort;
} else if (
options.bundler === 'webpack' &&
typeof plugin === 'object' &&
Expand All @@ -518,7 +528,6 @@ async function normalizeOptions(
}
}

let e2ePort = 4200;
if (
nxJson.targetDefaults?.[e2eWebServerTarget] &&
nxJson.targetDefaults?.[e2eWebServerTarget].options?.port
Expand Down

0 comments on commit c7c535e

Please sign in to comment.