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: UNIFY-676 browsers should be configurable in setupNodeEvents #20367

Merged
merged 10 commits into from
Mar 1, 2022
22 changes: 13 additions & 9 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ declare namespace Cypress {
/**
* The configuration for Cypress.
*/
type Config = ResolvedConfigOptions & RuntimeConfigOptions
type Config = ResolvedConfigOptions & RuntimeConfigOptions & RuntimeServerConfigOptions

/**
* Several libraries are bundled with Cypress by default.
Expand Down Expand Up @@ -2898,7 +2898,7 @@ declare namespace Cypress {
*/
clientCertificates: ClientCertificate[]

/**
/**
* Handle Cypress plugins
*/
setupNodeEvents: (on: PluginEvents, config: PluginConfigOptions) => Promise<PluginConfigOptions> | PluginConfigOptions
Expand All @@ -2907,17 +2907,13 @@ declare namespace Cypress {
/**
* Options appended to config object on runtime.
*/
interface RuntimeConfigOptions {
interface RuntimeConfigOptions extends Partial<RuntimeServerConfigOptions> {
/**
* CPU architecture, from Node `os.arch()`
*
* @see https://nodejs.org/api/os.html#os_os_arch
*/
arch: string
/**
* The browser Cypress is running on.
*/
browser: Browser
/**
* Available browsers found on your system.
*/
Expand Down Expand Up @@ -2945,12 +2941,20 @@ declare namespace Cypress {
* The Cypress version being used.
*/
version: string
}

/**
* Options appended before the server starts
*/
interface RuntimeServerConfigOptions {
/**
* The browser Cypress is running on.
*/
browser: Browser
// Internal or Unlisted at server/lib/config_options
autoOpen: boolean
browserUrl: string
clientRoute: string
configFile: string
cypressEnv: string
devServerPublicPathRoute: string
isNewProject: boolean
Expand Down Expand Up @@ -2997,7 +3001,7 @@ declare namespace Cypress {
*/
type ConfigOptions<ComponentDevServerOpts = any> = Partial<ResolvedConfigOptions<ComponentDevServerOpts>>

interface PluginConfigOptions extends ResolvedConfigOptions {
interface PluginConfigOptions extends ResolvedConfigOptions, RuntimeConfigOptions {
/**
* Absolute path to the config file (default: <projectRoot>/cypress.config.{ts|js}) or false
*/
Expand Down
2 changes: 2 additions & 0 deletions cli/types/tests/plugins-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const pluginConfig2: Cypress.PluginConfig = (on, config) => {
config.version // $ExpectType: string
config.testingType // $ExpectType: TestingType

config.browsers // $ExpectType: false

on('before:browser:launch', (browser, options) => {
browser.displayName // $ExpectType string
options.extensions // $ExpectType string[]
Expand Down
6 changes: 5 additions & 1 deletion npm/vite-dev-server/src/resolveServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export interface StartDevServerOptions {
export const resolveServerConfig = async ({ viteConfig, options, indexHtml }: StartDevServerOptions): Promise<InlineConfig> => {
const { projectRoot, supportFile, namespace } = options.config

if (!projectRoot) {
throw Error('Expected projectRoot to be defined on options.config. Did you forget to pass `config`?')
}

const requiredOptions: InlineConfig = {
base: `/${namespace}/src/`,
root: projectRoot,
}

const finalConfig: InlineConfig = { ...viteConfig, ...requiredOptions }

finalConfig.plugins = [...(finalConfig.plugins || []), makeCypressPlugin(projectRoot, supportFile, options.devServerEvents, options.specs, options.config.namespace, indexHtml)]
finalConfig.plugins = [...(finalConfig.plugins || []), makeCypressPlugin(projectRoot, supportFile, options.devServerEvents, options.specs, options.config.namespace || '', indexHtml)]

// This alias is necessary to avoid a "prefixIdentifiers" issue from slots mounting
// only cjs compiler-core accepts using prefixIdentifiers in slots which vue test utils use.
Expand Down
6 changes: 5 additions & 1 deletion npm/webpack-dev-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export async function start ({ webpackConfig: userWebpackConfig, indexHtml, opti
debug('User did not pass in any webpack configuration')
}

const { projectRoot, devServerPublicPathRoute, isTextTerminal } = options.config
const { projectRoot, devServerPublicPathRoute = '', isTextTerminal } = options.config

if (!projectRoot) {
throw Error('Expected projectRoot to be defined on options.config. Did you forget to pass `config`?')
}

const webpackConfig = await makeWebpackConfig(userWebpackConfig || {}, {
files: options.specs,
Expand Down
14 changes: 14 additions & 0 deletions packages/launchpad/cypress/e2e/system-tests/config-spec.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('config-spec', () => {
it('can filter browsers from config', () => {
cy.scaffoldProject('plugin-filter-browsers')
cy.findBrowsers()
cy.openProject('plugin-filter-browsers', ['--e2e'])
cy.withCtx(async (ctx) => {
expect(await ctx.browser.machineBrowsers()).to.have.length(12) // stubbed list of all browsers
})

cy.visitLaunchpad()
// Filtered down to the electron browser in the plugin
cy.get('[data-cy="open-browser-list"]').children().should('have.length', 1)
})
})
2 changes: 1 addition & 1 deletion packages/server/lib/project-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export class ProjectBase<TServer extends Server> extends EE {
await this.saveState(stateToSave)

await Promise.all([
checkSupportFile({ configFile: cfg.configFile, supportFile: cfg.supportFile }),
checkSupportFile(cfg.supportFile),
])

if (cfg.isTextTerminal) {
Expand Down
8 changes: 1 addition & 7 deletions packages/server/lib/project_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ export const getSpecUrl = ({
return specUrl
}

export const checkSupportFile = async ({
supportFile,
configFile,
}: {
supportFile?: string | boolean
configFile?: string | false
}) => {
export const checkSupportFile = async (supportFile?: string | boolean) => {
if (supportFile && typeof supportFile === 'string') {
const found = await fs.pathExists(supportFile)

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface FullConfig extends Partial<Cypress.RuntimeConfigOptions & Cypre
// Instead, this is an interface of values that have been manually validated to exist
// and are required when creating a project.
export type ReceivedCypressOptions =
Pick<Cypress.RuntimeConfigOptions, 'hosts' | 'projectName' | 'clientRoute' | 'devServerPublicPathRoute' | 'namespace' | 'report' | 'socketIoCookie' | 'configFile' | 'isTextTerminal' | 'isNewProject' | 'proxyUrl' | 'browsers' | 'browserUrl' | 'socketIoRoute' | 'arch' | 'platform' | 'spec' | 'specs' | 'browser' | 'version' | 'remote'>
Pick<Cypress.RuntimeConfigOptions, 'hosts' | 'projectName' | 'clientRoute' | 'devServerPublicPathRoute' | 'namespace' | 'report' | 'socketIoCookie' | 'isTextTerminal' | 'isNewProject' | 'proxyUrl' | 'browsers' | 'browserUrl' | 'socketIoRoute' | 'arch' | 'platform' | 'spec' | 'specs' | 'browser' | 'version' | 'remote'>
& Pick<Cypress.ResolvedConfigOptions, 'chromeWebSecurity' | 'supportFolder' | 'experimentalSourceRewriting' | 'fixturesFolder' | 'reporter' | 'reporterOptions' | 'screenshotsFolder' | 'pluginsFile' | 'supportFile' | 'baseUrl' | 'viewportHeight' | 'viewportWidth' | 'port' | 'experimentalInteractiveRunEvents' | 'userAgent' | 'downloadsFolder' | 'env' | 'testFiles' | 'excludeSpecPattern' | 'specPattern'> // TODO: Figure out how to type this better.

export interface SampleConfigFile{
Expand Down