Skip to content

Commit

Permalink
feat: normalized signatures webpack & vite servers (#18379)
Browse files Browse the repository at this point in the history
  • Loading branch information
Barthélémy Ledoux authored Oct 14, 2021
1 parent 7aac450 commit 8f5308f
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
23 changes: 23 additions & 0 deletions npm/vite-dev-server/cypress/new-signature/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* This file is intended to test the new normalized signature
* of devServers. To make the test shorter we only test
* the smkoke test here
*/

const path = require('path')
const { devServer, defineDevServerConfig } = require('../../dist')

module.exports = (on, config) => {
on('dev-server:start', async (options) => {
return devServer(
options,
defineDevServerConfig({
configFile: path.resolve(__dirname, '..', '..', 'vite.config.ts'),
}),
)
})

config.testFiles = '**/smoke.spec.ts'

return config
}
3 changes: 2 additions & 1 deletion npm/vite-dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build-prod": "tsc",
"cy:open": "node ../../scripts/cypress.js open-ct --project ${PWD}",
"cy:run": "node ../../scripts/cypress.js run-ct --project ${PWD}",
"test": "yarn cy:run",
"cy:run-signature": "yarn cy:run --config=\"{\\\"pluginsFile\\\":\\\"cypress/new-signature/plugins.js\\\"}\"",
"test": "yarn cy:run && yarn cy:run-signature",
"watch": "tsc -w"
},
"dependencies": {
Expand Down
11 changes: 11 additions & 0 deletions npm/vite-dev-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { debug as debugFn } from 'debug'
import { InlineConfig } from 'vite'
import { start as createDevServer, StartDevServerOptions } from './startServer'
const debug = debugFn('cypress:vite-dev-server:vite')

Expand All @@ -14,3 +15,13 @@ export async function startDevServer (startDevServerArgs: StartDevServerOptions)

return { port, close: app.httpServer!.close }
}

export type CypressViteDevServerConfig = Omit<InlineConfig, 'base' | 'root'>

export function devServer (cypressDevServerConfig: Cypress.DevServerConfig, devServerConfig?: CypressViteDevServerConfig) {
return startDevServer({ options: cypressDevServerConfig, viteConfig: devServerConfig })
}

export function defineDevServerConfig (devServerConfig: CypressViteDevServerConfig) {
return devServerConfig
}
21 changes: 20 additions & 1 deletion npm/webpack-dev-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debug as debugFn } from 'debug'
import { AddressInfo } from 'net'
import { Server } from 'http'
import { start as createDevServer, StartDevServer } from './startServer'
import { start as createDevServer, StartDevServer, WebpackConfigurationWithDevServer } from './startServer'
import { webpackDevServerFacts } from './webpackDevServerFacts'

const debug = debugFn('cypress:webpack-dev-server:webpack')
Expand Down Expand Up @@ -56,3 +56,22 @@ export async function startDevServer (startDevServerArgs: StartDevServer, exitPr
reject(webpackDevServerFacts.unsupported())
})
}

export interface CypressWebpackDevServerConfig{
/* support passing a path to the user's webpack config */
webpackConfig?: WebpackConfigurationWithDevServer
/* base html template to render in AUT */
template?: string
}

export function devServer (cypressDevServerConfig: Cypress.DevServerConfig, devServerConfig?: CypressWebpackDevServerConfig) {
return startDevServer({
options: cypressDevServerConfig,
webpackConfig: devServerConfig?.webpackConfig,
template: devServerConfig?.template,
})
}

export function defineDevServerConfig (devServerConfig: CypressWebpackDevServerConfig) {
return devServerConfig
}
10 changes: 7 additions & 3 deletions npm/webpack-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import { webpackDevServerFacts } from './webpackDevServerFacts'
export interface StartDevServer extends UserWebpackDevServerOptions {
/* this is the Cypress dev server configuration object */
options: Cypress.DevServerConfig
/* support passing a path to the user's webpack config */
webpackConfig?: Record<string, any>
/* Base webpack config object used for loading component testing */
webpackConfig?: WebpackConfigurationWithDevServer
/* base html template to render in AUT */
template?: string
}

export interface WebpackConfigurationWithDevServer extends webpack.Configuration {
devServer?: WebpackDevServer.Configuration
}

const debug = Debug('cypress:webpack-dev-server:start')

export async function start ({ webpackConfig: userWebpackConfig, template, options, ...userOptions }: StartDevServer, exitProcess = process.exit): Promise<WebpackDevServer> {
Expand Down Expand Up @@ -49,7 +53,7 @@ export async function start ({ webpackConfig: userWebpackConfig, template, optio

debug('starting webpack dev server')
let webpackDevServerConfig: WebpackDevServer.Configuration = {
...userWebpackConfig?.devServer,
...(userWebpackConfig?.devServer || {}),
hot: false,
}

Expand Down
22 changes: 21 additions & 1 deletion npm/webpack-dev-server/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import http from 'http'
import fs from 'fs'
import { webpackDevServerFacts } from '../src/webpackDevServerFacts'

import { startDevServer } from '../'
import { defineDevServerConfig, devServer, startDevServer } from '../'

const requestSpecFile = (port: number) => {
return new Promise((res) => {
Expand Down Expand Up @@ -155,4 +155,24 @@ describe('#startDevServer', () => {
close(() => res())
})
})

it('accepts the devServer signature', async function () {
const devServerEvents = new EventEmitter()
const { port, close } = await devServer(
{
config,
specs,
devServerEvents,
},
defineDevServerConfig({ webpackConfig }),
)

const response = await requestSpecFile(port as number)

expect(response).to.eq('const foo = () => {}\n')

return new Promise((res) => {
close(() => res())
})
})
})

0 comments on commit 8f5308f

Please sign in to comment.