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

feat: detect component testing env #20002

Merged
merged 96 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
007f952
fix: add isDetecetd property on wizard fw
elevatebart Feb 1, 2022
2472f94
get the isDetected flag
elevatebart Feb 1, 2022
6cdb975
feat: remove key warning
elevatebart Feb 1, 2022
05bfc64
fix: avoid using promises to keep PR to minimum
elevatebart Feb 1, 2022
4436465
Add detected keyword
elevatebart Feb 1, 2022
99b8d50
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 1, 2022
3fe5635
fix: use tsconfig and configFile
elevatebart Feb 1, 2022
0f1bc40
test: add test for select
elevatebart Feb 1, 2022
202432c
test: update mock values for wizard
elevatebart Feb 1, 2022
db0c147
fix: add ct for environment
elevatebart Feb 1, 2022
b289727
move init to mutation
elevatebart Feb 2, 2022
28689a5
fix types
elevatebart Feb 2, 2022
b31ae58
Add subtitle for Bundler
elevatebart Feb 2, 2022
3a18461
fix e2e tests
elevatebart Feb 2, 2022
42500c0
test: fix e2e
elevatebart Feb 2, 2022
3b35434
add e2e tests
elevatebart Feb 2, 2022
84d895b
fail if package json is invalid
elevatebart Feb 2, 2022
ba5e957
deal with error in package json
elevatebart Feb 2, 2022
7789e93
avoid resetting bundler if not necessary
elevatebart Feb 2, 2022
2526b21
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 2, 2022
e75eeb5
fix types
elevatebart Feb 2, 2022
e1dfede
fix the style
elevatebart Feb 2, 2022
8ae1601
fix: e2e tests
elevatebart Feb 2, 2022
6faad8c
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 2, 2022
584f0b9
remove unwanted change
elevatebart Feb 2, 2022
29db1c4
give it a bit more time to config
elevatebart Feb 2, 2022
f2c637d
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 3, 2022
53101e9
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 3, 2022
e5a9901
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 5, 2022
58558f3
resolve tsconfig from the current project
Feb 5, 2022
08c9c9b
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 7, 2022
720d825
fix types
elevatebart Feb 7, 2022
82f026d
import path instead of join
elevatebart Feb 7, 2022
a74352a
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 8, 2022
a884a81
fix: e2e select language highlight (#20098)
ZachJW34 Feb 8, 2022
4da62f7
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 8, 2022
8747a22
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 8, 2022
4a5c50c
test: fix e2e tests
elevatebart Feb 8, 2022
b18c603
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 9, 2022
d227cd1
make e2e tests for setup wizard pass
elevatebart Feb 9, 2022
6001329
use ts ignore to remove type classhes
elevatebart Feb 9, 2022
3ba334c
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 9, 2022
bce110e
fix: avoid pick up types form jest in system tests
elevatebart Feb 9, 2022
62ab71d
make the error of system tests more obvious
elevatebart Feb 9, 2022
0ef7ec5
add a white background to the shiki copy button
elevatebart Feb 9, 2022
e62cafc
make all jest tests not hoisted
elevatebart Feb 9, 2022
66486cc
fix: try and update jest
elevatebart Feb 9, 2022
02a6f36
last changes
elevatebart Feb 9, 2022
3c09954
chore: add some debug statements
elevatebart Feb 9, 2022
c1762f2
fix: make all file call async
elevatebart Feb 9, 2022
62422e1
fix >>> warning with vue
elevatebart Feb 9, 2022
540202d
add more debug statements
elevatebart Feb 9, 2022
ee358f9
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 9, 2022
874f969
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 9, 2022
c071755
add more debug statements
elevatebart Feb 10, 2022
712010b
force reload config when configured
elevatebart Feb 10, 2022
0a797ac
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 10, 2022
3fdbdfd
remove useless return in detectPackage
elevatebart Feb 10, 2022
81f2201
use project lifecycle typescript detection
elevatebart Feb 10, 2022
c7abc87
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 10, 2022
0c80d7f
remove ;
elevatebart Feb 10, 2022
a80f31f
better handling of scaffoldFile
elevatebart Feb 10, 2022
5447eec
last fixes
elevatebart Feb 10, 2022
16e5a68
a few fixes (woops)
elevatebart Feb 10, 2022
80c4c73
last set of fixes
elevatebart Feb 10, 2022
aef78c0
try a new method for testing
elevatebart Feb 10, 2022
d8b806a
update tslib because of warning
elevatebart Feb 10, 2022
52f9991
test: only scaffold necessary projects for each test
elevatebart Feb 10, 2022
c2c5f00
fix one debug statement
elevatebart Feb 10, 2022
fc84094
add debug statement when config file changes
elevatebart Feb 10, 2022
1478bbf
add error when watcher fails
elevatebart Feb 10, 2022
11d220b
surface the error in the UI
elevatebart Feb 10, 2022
8b1fa9e
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 10, 2022
8dda2b7
add await write finish to watchers
elevatebart Feb 10, 2022
79304a9
simplify watchers
lmiller1990 Feb 10, 2022
ca82817
remove wait
elevatebart Feb 10, 2022
457599b
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
elevatebart Feb 10, 2022
8efe86a
remove useless re-hooking
elevatebart Feb 10, 2022
0c0328a
clarify a little conditions for reloadConfig
elevatebart Feb 11, 2022
0cd129b
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
elevatebart Feb 11, 2022
e8fc166
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
elevatebart Feb 11, 2022
ab7c42a
optimize migration by avoiding overread
elevatebart Feb 11, 2022
2d94047
fix migration tests
elevatebart Feb 11, 2022
960a2c3
remove bad taste code
elevatebart Feb 11, 2022
9dd91b5
await initialization if not done
elevatebart Feb 11, 2022
7ae54ab
refactor migration tests
elevatebart Feb 11, 2022
c0c60cb
remove the then between commands
elevatebart Feb 11, 2022
4f0800f
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 11, 2022
e935bfd
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 11, 2022
e33c771
test: remove flake in error management
elevatebart Feb 11, 2022
edceffd
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
elevatebart Feb 11, 2022
a3b1f64
init oldConfigPromise to null
elevatebart Feb 11, 2022
16ffc5a
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 11, 2022
e9de23d
fix: initialize the wizard when reconfiguring
elevatebart Feb 11, 2022
9c62e5d
remove react-scripts dependency
elevatebart Feb 11, 2022
ea3068d
Merge branch '10.0-release' into elevatebart/feat/component-detect-fr…
Feb 11, 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
4 changes: 2 additions & 2 deletions npm/eslint-plugin-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"eslint-plugin-json-format": "^2.0.0",
"eslint-plugin-mocha": "^8.1.0",
"eslint-plugin-promise": "^4.2.1",
"jest": "^24.8.0",
"jest-cli": "^24.8.0",
"jest": "^24.9.0",
"jest-cli": "^24.9.0",
"sinon": "^7.3.2",
"sinon-chai": "^3.3.0"
},
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@
],
"nohoist": [
"**/webpack-preprocessor/babel-loader",
"**/webpack-batteries-included-preprocessor/ts-loader"
"**/webpack-batteries-included-preprocessor/ts-loader",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are all these changes in webpack dev server and package json here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is all connected to the fact that we need react-scripts to be resolvable from system tests so that the last step of the installation works.
react-scripts loads jest and its types and those types conflict with chai

"**/jest",
"**/jest*",
"**/expect"
]
},
"lint-staged": {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/settings/project/ExperimentRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ const { t } = useI18n()
</script>

<style scoped>
.description /deep/ a code {
.description :deep(a code) {
@apply text-indigo-500;
}

.description /deep/ a {
.description :deep(a) {
@apply outline-none text-indigo-500 hocus:underline hocus:underline-indigo-500;
}
</style>
204 changes: 149 additions & 55 deletions packages/data-context/src/actions/WizardActions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { CodeLanguageEnum, NexusGenEnums, NexusGenObjects } from '@packages/graphql/src/gen/nxs.gen'
import { Bundler, CodeLanguage, CODE_LANGUAGES, FrontendFramework } from '@packages/types'
import { Bundler, CodeLanguage, CODE_LANGUAGES, FrontendFramework, FRONTEND_FRAMEWORKS } from '@packages/types'
import assert from 'assert'
import dedent from 'dedent'
import fs from 'fs'
import path from 'path'
import Debug from 'debug'

const debug = Debug('cypress:data-context:wizard-actions')

import type { DataContext } from '..'

Expand All @@ -27,13 +29,24 @@ export class WizardActions {
}

setFramework (framework: NexusGenEnums['FrontendFrameworkEnum'] | null) {
const prevFramework = this.ctx.coreData.wizard.chosenFramework || ''

this.ctx.coreData.wizard.chosenFramework = framework

if (framework !== 'react' && framework !== 'vue') {
return this.setBundler('webpack')
}

return this.setBundler(null)
const { chosenBundler } = this.ctx.coreData.wizard

// if the previous bundler was incompatible with the
// new framework, we need to reset it
if ((chosenBundler && !this.ctx.wizard.chosenFramework?.supportedBundlers.includes(chosenBundler))
|| !['react', 'vue'].includes(prevFramework)) {
return this.setBundler(null)
}

return
}

setBundler (bundler: NexusGenEnums['SupportedBundlers'] | null) {
Expand All @@ -48,7 +61,13 @@ export class WizardActions {
return this.data
}

completeSetup () {
async completeSetup () {
debug('completeSetup')
// wait for the config to be initialized if it is not yet
// before returning. This should not penalize users but
// allow for tests, too fast for this last step to pass.
// NOTE: if the config is already initialized, this will be instant
await this.ctx.lifecycleManager.initializeConfig()
this.ctx.update((d) => {
d.scaffoldedFiles = null
})
Expand All @@ -63,6 +82,92 @@ export class WizardActions {
return this.data
}

async initialize () {
if (this.ctx.currentProject) {
this.data.detectedFramework = null
this.data.detectedBundler = null
this.data.detectedLanguage = null

await this.detectLanguage()
debug('detectedLanguage %s', this.data.detectedLanguage)
this.data.chosenLanguage = this.data.detectedLanguage || 'js'

let hasPackageJson = true

try {
await this.ctx.fs.access(path.join(this.ctx.currentProject, 'package.json'), this.ctx.fs.constants.R_OK)
} catch (e) {
debug('Could not read or find package.json: %O', e)
hasPackageJson = false
}
const packageJson: {
dependencies?: { [key: string]: string }
devDependencies?: { [key: string]: string }
} = hasPackageJson ? await this.ctx.fs.readJson(path.join(this.ctx.currentProject, 'package.json')) : {}

debug('packageJson %O', packageJson)
const dependencies = [
...Object.keys(packageJson.dependencies || {}),
...Object.keys(packageJson.devDependencies || {}),
]

this.detectFramework(dependencies)
debug('detectedFramework %s', this.data.detectedFramework)
this.detectBundler(dependencies)
debug('detectedBundler %s', this.data.detectedBundler)

this.data.chosenFramework = this.data.detectedFramework || null
this.data.chosenBundler = this.data.detectedBundler || null
}
}

private detectFramework (dependencies: string[]) {
// Detect full featured frameworks
if (dependencies.includes('next')) {
this.ctx.wizardData.detectedFramework = 'nextjs'
} else if (dependencies.includes('react-scripts')) {
this.ctx.wizardData.detectedFramework = 'cra'
} else if (dependencies.includes('nuxt')) {
this.ctx.wizardData.detectedFramework = 'nuxtjs'
} else if (dependencies.includes('@vue/cli-service')) {
this.ctx.wizardData.detectedFramework = 'vuecli'
} else if (dependencies.includes('react')) {
this.ctx.wizardData.detectedFramework = 'react'
} else if (dependencies.includes('vue')) {
this.ctx.wizardData.detectedFramework = 'vue'
}
}

private detectBundler (dependencies: string[]) {
const detectedFrameworkObject = FRONTEND_FRAMEWORKS.find((f) => f.type === this.ctx.wizardData.detectedFramework)

if (detectedFrameworkObject && detectedFrameworkObject.supportedBundlers.length === 1) {
this.ctx.wizardData.detectedBundler = detectedFrameworkObject.supportedBundlers[0] ?? null

return
}

if (dependencies.includes('webpack')) {
this.ctx.wizardData.detectedBundler = 'webpack'
}

if (dependencies.includes('vite')) {
this.ctx.wizardData.detectedBundler = 'vite'
}
}

private async detectLanguage () {
const { hasTypescript } = this.ctx.lifecycleManager.metaState

if (
hasTypescript ||
(this.ctx.lifecycleManager.configFile && /.ts$/.test(this.ctx.lifecycleManager.configFile))) {
this.ctx.wizardData.detectedLanguage = 'ts'
} else {
this.ctx.wizardData.detectedLanguage = 'js'
}
}

/**
* Scaffolds the testing type, by creating the necessary files & assigning to
*/
Expand Down Expand Up @@ -103,6 +208,7 @@ export class WizardActions {
}

private async scaffoldComponent () {
debug('scaffoldComponent')
const { chosenBundler, chosenFramework, chosenLanguage } = this.ctx.wizard

assert(chosenFramework && chosenLanguage && chosenBundler)
Expand Down Expand Up @@ -157,58 +263,43 @@ export class WizardActions {
}

private async scaffoldConfig (testingType: 'e2e' | 'component'): Promise<NexusGenObjects['ScaffoldedFile']> {
if (!fs.existsSync(this.ctx.lifecycleManager.configFilePath)) {
this.ctx.lifecycleManager.setConfigFilePath(this.ctx.coreData.wizard.chosenLanguage)
debug('scaffoldConfig')

const configCode = this.configCode(testingType, this.ctx.coreData.wizard.chosenLanguage)
if (this.ctx.lifecycleManager.metaState.hasValidConfigFile) {
const { ext } = path.parse(this.ctx.lifecycleManager.configFilePath)
const foundLanguage = ext === '.ts' ? 'ts' : 'js'
const configCode = this.configCode(testingType, foundLanguage)

return this.scaffoldFile(
this.ctx.lifecycleManager.configFilePath,
configCode,
'Created a new config file',
)
return {
status: 'changes',
description: 'Merge this code with your existing config file',
file: {
absolute: this.ctx.lifecycleManager.configFilePath,
contents: configCode,
},
}
}

const { ext } = path.parse(this.ctx.lifecycleManager.configFilePath)
const configCode = this.configCode(testingType, this.ctx.coreData.wizard.chosenLanguage)

const configCode = this.configCode(testingType, ext === '.ts' ? 'ts' : 'js')
// only do this if config file doesn't exist
this.ctx.lifecycleManager.setConfigFilePath(this.ctx.coreData.wizard.chosenLanguage)

return {
status: 'changes',
description: 'Merge this code with your existing config file',
file: {
absolute: this.ctx.lifecycleManager.configFilePath,
contents: configCode,
},
}
return this.scaffoldFile(
this.ctx.lifecycleManager.configFilePath,
configCode,
'Created a new config file',
)
}

private async scaffoldFixtures (): Promise<NexusGenObjects['ScaffoldedFile']> {
const exampleScaffoldPath = path.join(this.projectRoot, 'cypress/fixtures/example.json')

try {
await this.ctx.fs.stat(exampleScaffoldPath)
await this.ensureDir('fixtures')

return {
status: 'skipped',
file: {
absolute: exampleScaffoldPath,
contents: '// Skipped',
},
description: 'Fixtures directory already exists, skipping',
}
} catch (e) {
await this.ensureDir('fixtures')
await this.ctx.fs.writeFile(exampleScaffoldPath, `${JSON.stringify(FIXTURE_DATA, null, 2)}\n`)

return {
status: 'valid',
description: 'Added an example fixtures file/folder',
file: {
absolute: exampleScaffoldPath,
},
}
}
return this.scaffoldFile(exampleScaffoldPath,
`${JSON.stringify(FIXTURE_DATA, null, 2)}\n`,
'Added an example fixtures file/folder')
}

private wizardGetConfigCodeE2E (lang: CodeLanguageEnum): string {
Expand Down Expand Up @@ -303,18 +394,11 @@ export class WizardActions {
}

private async scaffoldFile (filePath: string, contents: string, description: string): Promise<NexusGenObjects['ScaffoldedFile']> {
if (fs.existsSync(filePath)) {
return {
status: 'skipped',
description: 'File already exists',
file: {
absolute: filePath,
},
}
}

try {
await this.ctx.fs.writeFile(filePath, contents)
debug('scaffoldFile: start %s', filePath)
debug('scaffoldFile: with content', contents)
await this.ctx.fs.writeFile(filePath, contents, { flag: 'wx' })
debug('scaffoldFile: done %s', filePath)

return {
status: 'valid',
Expand All @@ -324,6 +408,16 @@ export class WizardActions {
},
}
} catch (e: any) {
if (e.code === 'EEXIST') {
return {
status: 'skipped',
description: 'File already exists',
file: {
absolute: filePath,
},
}
}

return {
status: 'error',
description: e.message || 'Error writing file',
Expand Down
Loading