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: allow cypress config.port to override devServer.port for proxying assets #27677

Merged
merged 30 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f82fdf0
fix: allow cypress config.port to override devServer.port for proxyin…
brian-mann Aug 28, 2023
4c54f20
changelog
lmiller1990 Aug 28, 2023
e90de0e
update snapshots
brian-mann Aug 28, 2023
f842e3f
Merge branch 'fix/config-port-overriding-devserver' of https://github…
brian-mann Aug 28, 2023
8e03114
chore: bump cache version
AtofStryker Aug 28, 2023
c30ce10
fix cy in cy tests
ryanthemanuel Aug 28, 2023
ee912fa
fix tests
ryanthemanuel Aug 28, 2023
ed303ba
fix issue where vite config was set properly
mschile Aug 28, 2023
fbd86e1
additional fixes
ryanthemanuel Aug 28, 2023
0bda09b
fix require
mschile Aug 28, 2023
837a19c
fix launchpad tests
ryanthemanuel Aug 28, 2023
078319e
Update system-tests/test/vite_dev_server_fresh_spec.ts
brian-mann Aug 28, 2023
37dd857
fix additional tests
ryanthemanuel Aug 28, 2023
b6f8794
fix tests
ryanthemanuel Aug 28, 2023
c697b80
always strip ansi from stdout BEFORE calling options.onStdout
brian-mann Aug 28, 2023
b141c20
Merge branch 'fix/config-port-overriding-devserver' of https://github…
brian-mann Aug 28, 2023
da36aa5
updating configFile in react system test
mschile Aug 28, 2023
5b621de
fix additional cy in cy tests
ryanthemanuel Aug 28, 2023
8322f14
fix cy in cy test
ryanthemanuel Aug 28, 2023
35523a2
fix tests
ryanthemanuel Aug 28, 2023
3bacf34
update snapshot
brian-mann Aug 28, 2023
8a60770
Merge branch 'fix/config-port-overriding-devserver' of https://github…
brian-mann Aug 28, 2023
3a441c5
fix for enabling turning off stripping ansi for that 1 special report…
brian-mann Aug 28, 2023
9a0481f
fix last flake
ryanthemanuel Aug 29, 2023
ca7bad2
fix last flake
ryanthemanuel Aug 29, 2023
0a0eebd
Merge branch 'develop' into fix/config-port-overriding-devserver
ryanthemanuel Aug 29, 2023
568e2a1
fix system tests
ryanthemanuel Aug 29, 2023
ee0fbd9
fix typo
ryanthemanuel Aug 29, 2023
181b27c
let's try this again
ryanthemanuel Aug 29, 2023
e684546
fix tests
ryanthemanuel Aug 29, 2023
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
4 changes: 4 additions & 0 deletions npm/vite-dev-server/src/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const createViteDevServerConfig = async (config: ViteDevServerConfig, vit
function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): InlineConfig {
const {
cypressConfig: {
port,
projectRoot,
devServerPublicPathRoute,
supportFile,
Expand All @@ -74,6 +75,8 @@ function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): Inline
specs,
} = config

const vitePort = port ?? undefined

// Vite caches its output in the .vite directory in the node_modules where vite lives.
// So we want to find that node_modules path and ensure it's added to the "allow" list
const vitePathNodeModules = path.dirname(path.dirname(require.resolve(`vite/package.json`, {
Expand Down Expand Up @@ -122,6 +125,7 @@ function makeCypressViteConfig (config: ViteDevServerConfig, vite: Vite): Inline
vite.searchForWorkspaceRoot?.(process.cwd()),
],
},
port: vitePort,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just be port - if port is provided, it's a number, otherwise it'll be undefined. No need for the vitePort variable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it’s needed because of types. Cypress provided types are number or null. Vite is number or undefined. Switch it and it’ll yell at you.

host: '127.0.0.1',
// Disable file watching and HMR when executing tests in `run` mode
...(isTextTerminal
Expand Down
5 changes: 4 additions & 1 deletion npm/webpack-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ export function devServer (devServerConfig: WebpackDevServerConfig): Promise<Cyp
return new Promise(async (resolve, reject) => {
const result = await devServer.create(devServerConfig) as DevServerCreateResult

// @ts-expect-error
const { port } = result.server?.options

if (result.version === 3) {
const srv = result.server.listen(0, '127.0.0.1', () => {
const srv = result.server.listen(port || 0, '127.0.0.1', () => {
const port = (srv.address() as AddressInfo).port

debug('Component testing webpack server 3 started on port %s', port)
Expand Down
5 changes: 5 additions & 0 deletions npm/webpack-dev-server/src/makeDefaultWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function makeCypressWebpackConfig (
const {
devServerConfig: {
cypressConfig: {
port,
projectRoot,
devServerPublicPathRoute,
supportFile,
Expand All @@ -43,6 +44,8 @@ export function makeCypressWebpackConfig (
},
} = config

const webpackDevServerPort = port ?? undefined

debug(`Using HtmlWebpackPlugin version ${htmlWebpackPluginVersion} from ${htmlWebpackPluginImportPath}`)

const optimization: Record<string, any> = {
Expand Down Expand Up @@ -106,6 +109,7 @@ export function makeCypressWebpackConfig (
return {
...finalConfig,
devServer: {
port: webpackDevServerPort,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again just port is okay, but this is more explicit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types problem without coercing to undefined

client: {
overlay: false,
},
Expand All @@ -117,6 +121,7 @@ export function makeCypressWebpackConfig (
return {
...finalConfig,
devServer: {
port: webpackDevServerPort,
overlay: false,
},
}
Expand Down
5 changes: 5 additions & 0 deletions packages/data-context/src/data/ProjectLifecycleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export class ProjectLifecycleManager {

const devServerOptions = await this.ctx._apis.projectApi.getDevServer().start({ specs: this.ctx.project.specs, config: finalConfig })

// if we received a cypressConfig.port we want to null it out
// because we propagated it into the devServer.port and it is
// later set as baseUrl which cypress is launched into
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth adding a link to the issue that explain this

finalConfig.port = null

span?.end()

if (!devServerOptions?.port) {
Expand Down
232 changes: 232 additions & 0 deletions system-tests/__snapshots__/vite_dev_server_fresh_spec.ts.js

Large diffs are not rendered by default.

478 changes: 429 additions & 49 deletions system-tests/__snapshots__/webpack_dev_server_fresh_spec.ts.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion system-tests/lib/normalizeStdout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Fixtures from './fixtures'
import _ from 'lodash'
import stripAnsi from 'strip-ansi'

export const e2ePath = Fixtures.projectPath('e2e')

Expand Down Expand Up @@ -114,7 +115,7 @@ export const normalizeStdout = function (str: string, options: any = {}) {

// remove all of the dynamic parts of stdout
// to normalize against what we expected
str = str
str = stripAnsi(str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we need to change this to stripAnsi here

// /Users/jane/........../ -> //foo/bar/.projects/
// (Required when paths are printed outside of our own formatting)
.split(pathUpToProjectName).join('/foo/bar/.projects')
Expand Down
1 change: 0 additions & 1 deletion system-tests/lib/system-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,6 @@ const systemTests = {
return stdout
.replace(/using description file: .* \(relative/g, 'using description file: [..] (relative')
.replace(/Module build failed \(from .*\)/g, 'Module build failed (from [..])')
.replace(/Project is running at http:\/\/localhost:\d+/g, 'Project is running at http://localhost:xxxx')
.replace(/webpack.*compiled with.*in \d+ ms/g, 'webpack x.x.x compiled with x errors in xxx ms')
},

Expand Down
10 changes: 10 additions & 0 deletions system-tests/project-fixtures/react/cypress-vite-port.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import viteConfig from './cypress-vite.config'

const port = 9999

viteConfig.port = port
viteConfig.env = {
PORT_CHECK: port,
}

export default viteConfig
11 changes: 11 additions & 0 deletions system-tests/project-fixtures/react/cypress-vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from 'cypress'

import type * as vite from 'vite'

declare global {
Expand All @@ -9,12 +10,22 @@ declare global {
}
}

const port = 8888

export default defineConfig({
env: {
PORT_CHECK: port,
},
videoCompression: false, // turn off video compression for CI
component: {
devServer: {
framework: 'react',
bundler: 'vite',
viteConfig: {
server: {
port,
},
},
},
},
// These tests should run quickly / fail quickly,
Expand Down
10 changes: 10 additions & 0 deletions system-tests/project-fixtures/react/cypress-webpack-port.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import cypressWebpackConfig from './cypress-webpack.config'

const port = 9999

cypressWebpackConfig.port = port
cypressWebpackConfig.env = {
PORT_CHECK: port,
}

export default cypressWebpackConfig
13 changes: 12 additions & 1 deletion system-tests/project-fixtures/react/cypress-webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineConfig } from 'cypress'

import type * as webpack from 'webpack'
import type * as webpackDevServer from 'webpack-dev-server'

Expand All @@ -12,14 +13,24 @@ declare global {
}
}

const port = 8888

const webpackConfig = require('./webpack.config.js')

webpackConfig.devServer ??= {}
webpackConfig.devServer.port = port

export default defineConfig({
env: {
PORT_CHECK: port,
},
videoCompression: false, // turn off video compression for CI
component: {
devServer: {
framework: 'react',
bundler: 'webpack',
webpackConfig: {
...require('./webpack.config.js'),
...webpackConfig,
stats: 'minimal',
},
},
Expand Down
8 changes: 8 additions & 0 deletions system-tests/project-fixtures/react/src/port.cy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const portCheck = Cypress.env('PORT_CHECK')

it('ensures we have launched at the overriden port', () => {
expect(portCheck).to.be.a('number')
expect(portCheck).to.be.oneOf([8888, 9999])

expect(window.location.host).to.eq(`localhost:${portCheck}`)
})
14 changes: 11 additions & 3 deletions system-tests/test/vite_dev_server_fresh_spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import systemTests from '../lib/system-tests'

import type { fixtureDirs } from '@tooling/system-tests'

type ProjectDirs = typeof fixtureDirs
Expand All @@ -18,11 +19,18 @@ describe('@cypress/vite-dev-server', function () {
browser: 'chrome',
snapshot: true,
expectedExitCode: 7,
onStdout: (stdout) => {
return stdout.replace(/http:\/\/localhost:\d+/g, 'http://localhost:xxxx')
},
})
})

systemTests.it.only(`executes all of the tests for ${project} when port is statically configured`, {
project,
configFile: 'cypress-vite-port.config.ts',
spec: 'src/port.cy.jsx',
testingType: 'component',
browser: 'chrome',
snapshot: true,
expectedExitCode: 0,
})
}
})
})
17 changes: 15 additions & 2 deletions system-tests/test/webpack_dev_server_fresh_spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import systemTests from '../lib/system-tests'

import type { fixtureDirs } from '@tooling/system-tests'
import { stripAnsi } from '@packages/server/lib/errors'

type ProjectDirs = typeof fixtureDirs

Expand All @@ -20,10 +20,23 @@ describe('@cypress/webpack-dev-server', function () {
snapshot: true,
expectedExitCode: 7,
onStdout: (stdout) => {
return systemTests.normalizeWebpackErrors(stripAnsi(stdout))
return systemTests.normalizeWebpackErrors(stdout)
},
})
})

systemTests.it(`executes all of the tests for ${project} when port is statically configured`, {
project,
configFile: 'cypress-webpack-port.config.ts',
spec: 'src/port.cy.jsx',
testingType: 'component',
browser: 'chrome',
snapshot: true,
expectedExitCode: 0,
onStdout: (stdout) => {
return systemTests.normalizeWebpackErrors(stdout)
},
})
}
})
})