Skip to content

Commit

Permalink
fix: use resolved port for vite (#21490)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachJW34 authored May 17, 2022
1 parent 8d79472 commit 630e422
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
20 changes: 20 additions & 0 deletions npm/vite-dev-server/cypress/e2e/vite-dev-server.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference types="Cypress" />
/// <reference path="../support/e2e.ts" />

describe('Config options', () => {
it('supports supportFile = false', () => {
cy.scaffoldProject('vite2.9.1-react')
Expand All @@ -8,4 +11,21 @@ describe('Config options', () => {
cy.contains('App.cy.jsx').click()
cy.get('.passed > .num').should('contain', 1)
})

it('chooses new port when specified port is in use', () => {
cy.scaffoldProject('vite2.9.1-react')
cy.openProject('vite2.9.1-react', ['--config-file', 'cypress-vite-port-in-use.config.ts'])
cy.startAppServer('component')

cy.visitApp()

cy.contains('App.cy.jsx').click()
cy.get('.passed > .num').should('contain', 1)

cy.withCtx(async (ctx) => {
const config = ctx.lifecycleManager.loadedFullConfig

expect(config.baseUrl).to.equal('http://localhost:3001')
})
})
})
1 change: 0 additions & 1 deletion npm/vite-dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"dependencies": {
"debug": "4.3.3",
"find-up": "6.3.0",
"get-port": "5.1.1",
"local-pkg": "0.4.1",
"pathe": "0.2.0"
},
Expand Down
10 changes: 6 additions & 4 deletions npm/vite-dev-server/src/devServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debugFn from 'debug'
import getPort from 'get-port'
import { getVite, Vite } from './getVite'
import { createViteDevServerConfig } from './resolveConfig'

Expand All @@ -24,10 +23,13 @@ export async function devServer (config: ViteDevServerConfig): Promise<Cypress.R
const server = await devServer.create(config, vite)

debug('Vite server created')
const port = await getPort({ port: 3000 })

debug('Starting Vite Server on port %i', port)
await server.listen(port)
await server.listen()
const { port } = server.config.server

if (!port) {
throw new Error('Missing vite dev server port.')
}

debug('Successfully launched the vite server on port', port)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineConfig } from 'cypress'
import defaultConfig from './cypress-vite.config'
import * as http from 'http'

export default defineConfig({
...defaultConfig,
component: {
...defaultConfig.component as Cypress.Config['component'],
devServer: {
framework: 'react',
bundler: 'vite',
viteConfig: {
logLevel: 'silent',
server: {
port: 3000
}
},
},
async setupNodeEvents() {
await new Promise<void>((res) => http.createServer().listen(3000, '127.0.0.1', res))
}
}
})

3 comments on commit 630e422

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 630e422 May 17, 2022

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/linux-x64/10.0-release-630e4220ca5ebbaa8c39044b86d434510b3a0f1b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 630e422 May 17, 2022

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/win32-x64/10.0-release-630e4220ca5ebbaa8c39044b86d434510b3a0f1b/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 630e422 May 17, 2022

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/darwin-x64/10.0-release-630e4220ca5ebbaa8c39044b86d434510b3a0f1b/cypress.tgz

Please sign in to comment.