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(launchpad): support default export #20383

Merged
merged 25 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a86faf6
fix requirePlugins template
davidmunechika Feb 21, 2022
2abedf4
use static import
davidmunechika Feb 21, 2022
66d664b
remove import
davidmunechika Feb 25, 2022
254b1ed
Merge branch '10.0-release' into unify-1073-config-migration
davidmunechika Feb 27, 2022
ee4d6be
Merge branch '10.0-release' into unify-1073-config-migration
lmiller1990 Feb 28, 2022
3c6aff8
chore: support default export
lmiller1990 Feb 28, 2022
0a37e49
fix tests
lmiller1990 Feb 28, 2022
89d2e6b
update test project package.json
lmiller1990 Feb 28, 2022
3ceb826
update snapshots
lmiller1990 Feb 28, 2022
735ec62
update path for requiring plugins
lmiller1990 Feb 28, 2022
49112ca
fix test
lmiller1990 Feb 28, 2022
cabfb13
Merge remote-tracking branch 'origin/10.0-release' into lmiller1990/U…
lmiller1990 Feb 28, 2022
e4475ea
revert change to path
lmiller1990 Feb 28, 2022
74b4dc0
fix expected output
lmiller1990 Feb 28, 2022
c73c73d
docs
lmiller1990 Feb 28, 2022
8c53883
update
lmiller1990 Mar 1, 2022
5db5474
fix snapshot
lmiller1990 Mar 1, 2022
98c9df1
update tests
lmiller1990 Mar 1, 2022
1c2761f
add ts plugin
lmiller1990 Mar 1, 2022
6050467
fix test for CI
lmiller1990 Mar 1, 2022
e1b019b
fix default export logic
lmiller1990 Mar 1, 2022
4f012a2
refactor: use visit to clarify the code
elevatebart Mar 1, 2022
f8a99b4
Merge branch '10.0-release' into lmiller1990/UNIFY-1073
Mar 1, 2022
7b131be
Merge remote-tracking branch 'origin/10.0-release' into lmiller1990/U…
lmiller1990 Mar 1, 2022
96f6eda
rebase
lmiller1990 Mar 1, 2022
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These were all wrong, how the heck did this get merged? This is why I don't generally like snapshots; they don't test behavior.

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
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
47 changes: 47 additions & 0 deletions packages/data-context/src/sources/migration/parserUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { parse, ParserOptions } from '@babel/parser'
import { visit } from 'recast'
import type * as bt from '@babel/types'

const babelParserOptions: ParserOptions = {
sourceType: 'module',
strictMode: false,
tokens: true,
plugins: [
'decorators-legacy',
'doExpressions',
'objectRestSpread',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportDefaultFrom',
'exportNamespaceFrom',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
'nullishCoalescingOperator',
'typescript',
],
}

export function hasDefaultExport (src: string): boolean {
const ast = parse(src, babelParserOptions) as bt.File

let hasDefault = false

visit(ast, {
visitExportDefaultDeclaration () {
hasDefault = true

return false
},
})

return hasDefault
}
5 changes: 5 additions & 0 deletions packages/data-context/test/unit/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// necessary to have mocha types working correctly
import 'mocha'
import path from 'path'
import { e2eProjectDirs } from '@packages/frontend-shared/cypress/e2e/support/e2eProjectDirs'
import Fixtures from '@tooling/system-tests/lib/fixtures'
import { DataContext, DataContextConfig } from '../../src'
Expand All @@ -8,6 +9,10 @@ import type { BrowserApiShape } from '../../src/sources/BrowserDataSource'
import type { AppApiShape, AuthApiShape, ElectronApiShape, LocalSettingsApiShape, ProjectApiShape } from '../../src/actions'
import { InjectedConfigApi } from '../../src/data'

export function getSystemTestProject (project: typeof e2eProjectDirs[number]) {
return path.join(__dirname, '..', '..', '..', '..', 'system-tests', 'projects', project)
}

export async function scaffoldMigrationProject (project: typeof e2eProjectDirs[number]) {
Fixtures.removeProject(project)

Expand Down
Loading