Skip to content

Commit

Permalink
Merge branch '10.0-release' into fix-generated-spec-link-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachJW34 authored Mar 2, 2022
2 parents a2c5c12 + e36f053 commit 2da6006
Show file tree
Hide file tree
Showing 51 changed files with 5,332 additions and 173 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ system-tests/projects/vueclivue2-configured/**/*
system-tests/projects/vueclivue3-unconfigured/**/*
system-tests/projects/vueclivue3-configured/**/*

system-tests/projects/vuecli5vue3-unconfigured/**/*
system-tests/projects/vuecli5vue3-configured/**/*

system-tests/projects/vue3-vite-ts-unconfigured/**/*
system-tests/projects/vue3-vite-ts-configured/**/*

Expand Down
27 changes: 21 additions & 6 deletions packages/data-context/__snapshots__/codegen.spec.ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
},
})
Expand All @@ -22,7 +22,7 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
baseUrl: 'localhost:3000',
},
Expand All @@ -38,7 +38,7 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
},
})
Expand All @@ -54,7 +54,7 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
retries: 2,
baseUrl: 'localhost:300',
Expand All @@ -71,7 +71,22 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
},
})
`

exports['cypress.config.js generation should handle export default in plugins file 1'] = `
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./cypress/plugins/index.ts').default(on, config)
},
},
})
Expand All @@ -86,7 +101,7 @@ module.exports = defineConfig({
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./path/to/plugin/file')(on, config)
return require('./cypress/plugins/index.js')(on, config)
},
},
})
Expand Down
94 changes: 0 additions & 94 deletions packages/data-context/__snapshots__/migration.spec.ts.js

This file was deleted.

2 changes: 2 additions & 0 deletions packages/data-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test-unit": "mocha -r @packages/ts/register --config ./test/.mocharc.js"
},
"dependencies": {
"@babel/parser": "7.13.0",
"@storybook/csf-tools": "^6.4.0-alpha.38",
"@urql/core": "2.3.1",
"@urql/exchange-execute": "1.1.0",
Expand Down Expand Up @@ -46,6 +47,7 @@
"wonka": "^4.0.15"
},
"devDependencies": {
"@babel/types": "7.17.0",
"@packages/config": "0.0.0-development",
"@packages/errors": "0.0.0-development",
"@packages/example": "0.0.0-development",
Expand Down
84 changes: 79 additions & 5 deletions packages/data-context/src/actions/WizardActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class WizardActions {
const scaffolded = await Promise.all([
this.scaffoldConfig('e2e'),
this.scaffoldSupport('e2e', this.ctx.coreData.wizard.chosenLanguage),
this.scaffoldSupport('commands', this.ctx.coreData.wizard.chosenLanguage),
this.scaffoldFixtures(),
])

Expand All @@ -187,26 +188,28 @@ export class WizardActions {
this.scaffoldConfig('component'),
this.scaffoldFixtures(),
this.scaffoldSupport('component', chosenLanguage.type),
this.scaffoldSupport('commands', chosenLanguage.type),
this.getComponentIndexHtml({
chosenFramework,
chosenLanguage,
}),
])
}

private async scaffoldSupport (fileName: 'e2e' | 'component', language: CodeLanguageEnum): Promise<NexusGenObjects['ScaffoldedFile']> {
private async scaffoldSupport (fileName: 'e2e' | 'component' | 'commands', language: CodeLanguageEnum): Promise<NexusGenObjects['ScaffoldedFile']> {
const supportFile = path.join(this.projectRoot, `cypress/support/${fileName}.${language}`)
const supportDir = path.dirname(supportFile)

// @ts-ignore
await this.ctx.fs.mkdir(supportDir, { recursive: true })
await this.scaffoldFile(supportFile, dedent`
// TODO: source the example support file
`, 'Scaffold default support file')

const fileContent = fileName === 'commands' ? this.commandsFileBody(language) : this.supportFileBody(fileName, language)

await this.scaffoldFile(supportFile, fileContent, 'Scaffold default support file')

return {
status: 'valid',
description: 'Added a support file, for extending the Cypress api',
description: `Added a ${fileName === 'commands' ? 'commands' : 'support'} file, for extending the Cypress api`,
file: {
absolute: supportFile,
},
Expand Down Expand Up @@ -375,8 +378,79 @@ export class WizardActions {
private ensureDir (type: 'component' | 'e2e' | 'fixtures') {
return this.ctx.fs.ensureDir(path.join(this.projectRoot, 'cypress', type))
}

private supportFileBody (fileName: 'e2e' | 'component', language: CodeLanguageEnum) {
return dedent`
// ***********************************************************
// This example support/${fileName}.${language} is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
import './commands'
// Alternatively you can use CommonJS syntax:
// require('./commands')
`
}

private commandsFileBody (language: CodeLanguageEnum) {
return dedent`
${language === 'ts' ? '/// <reference types="cypress" />' : ''}
// ***********************************************
// This example commands.${language} shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
${language === 'ts' ? COMMAND_TYPES : ''}
`
}
}

const COMMAND_TYPES = dedent`
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
`

const E2E_SCAFFOLD_BODY = dedent`
e2e: {
setupNodeEvents(on, config) {
Expand Down
40 changes: 30 additions & 10 deletions packages/data-context/src/sources/migration/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { substitute } from './autoRename'
import { supportFileRegexps } from './regexps'
import type { MigrationFile } from '../MigrationDataSource'
import { toPosix } from '../../util'

import Debug from 'debug'
import dedent from 'dedent'
import { hasDefaultExport } from './parserUtils'

const debug = Debug('cypress:data-context:sources:migration:codegen')

type ConfigOptions = {
Expand Down Expand Up @@ -54,7 +56,10 @@ export interface CreateConfigOptions {
}

export async function createConfigString (cfg: OldCypressConfig, options: CreateConfigOptions) {
return createCypressConfig(reduceConfig(cfg), await getPluginRelativePath(cfg, options.projectRoot), options)
const newConfig = reduceConfig(cfg)
const relativePluginPath = await getPluginRelativePath(cfg, options.projectRoot)

return createCypressConfig(newConfig, relativePluginPath, options)
}

interface FileToBeMigratedManually {
Expand Down Expand Up @@ -172,7 +177,7 @@ function createCypressConfig (config: ConfigOptions, pluginPath: string, options
const globalString = Object.keys(config.global).length > 0 ? `${formatObjectForConfig(config.global)},` : ''
const componentString = options.hasComponentTesting ? createComponentTemplate(config.component) : ''
const e2eString = options.hasE2ESpec
? createE2eTemplate(pluginPath, options.hasPluginsFile, config.e2e)
? createE2ETemplate(pluginPath, options, config.e2e)
: ''

if (defineConfigAvailable(options.projectRoot)) {
Expand Down Expand Up @@ -202,18 +207,33 @@ function formatObjectForConfig (obj: Record<string, unknown>) {
return JSON.stringify(obj, null, 2).replace(/^[{]|[}]$/g, '') // remove opening and closing {}
}

function createE2eTemplate (pluginPath: string, hasPluginsFile: boolean, options: Record<string, unknown>) {
const requirePlugins = `return require('./${pluginPath}')(on, config)`
function createE2ETemplate (pluginPath: string, createConfigOptions: CreateConfigOptions, options: Record<string, unknown>) {
if (!createConfigOptions.hasPluginsFile) {
return dedent`
e2e: {
setupNodeEvents(on, config) {}
}
`
}

const pluginFile = fs.readFileSync(path.join(createConfigOptions.projectRoot, pluginPath), 'utf8')
const relPluginsPath = path.normalize(`'./${pluginPath}'`)

const setupNodeEvents = `// We've imported your old cypress plugins here.
const requirePlugins = hasDefaultExport(pluginFile)
? `return require(${relPluginsPath}).default(on, config)`
: `return require(${relPluginsPath})(on, config)`

const setupNodeEvents = dedent`
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
${hasPluginsFile ? requirePlugins : ''}
${requirePlugins}
}`

return `e2e: {
${setupNodeEvents},${formatObjectForConfig(options)}
},`
return dedent`
e2e: {
${setupNodeEvents},${formatObjectForConfig(options)}
},`
}

function createComponentTemplate (options: Record<string, unknown>) {
Expand Down
Loading

0 comments on commit 2da6006

Please sign in to comment.