-
Notifications
You must be signed in to change notification settings - Fork 3.4k
chore(launchpad): work on infra for scaffold tests #20818
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3035b1e
chore: work on infra for scaffold tests
lmiller1990 5046ce0
revert
lmiller1990 7420dc8
wip
lmiller1990 5d05d81
ignore scaffolded files in linting
lmiller1990 2a6360e
Merge remote-tracking branch 'origin/10.0-release' into lmiller1990/U…
lmiller1990 94878d8
add snapshot for plain TS project
lmiller1990 f3b0a73
add more tests
lmiller1990 e7702b5
linting
lmiller1990 605558f
add temp-dir dep to data-context
lmiller1990 8d08812
exclude scaffolded assets from linting
lmiller1990 b8c5453
bump server tsconfig lib to es2019
lmiller1990 957ab51
bump graphql tsconfig lib
lmiller1990 699c6dd
add missing dependency
lmiller1990 d2b8708
Merge remote-tracking branch 'origin/10.0-release' into lmiller1990/U…
lmiller1990 3810fd6
use task to avoid test code leaking into data-context prod
lmiller1990 0c9a243
remove unused code
lmiller1990 5f16689
remove deps
lmiller1990 5b3a06b
make currentProject cross platform with path.posix and path.sep
lmiller1990 8e64cf3
posix.join
lmiller1990 ae5931c
use path.posix everywhere
lmiller1990 3c5de3b
update types
lmiller1990 c32483a
Merge branch '10.0-release' into lmiller1990/UNIFY-1305
lmiller1990 1af1e6c
correct paths and add ct framework to expected dir
lmiller1990 283f534
Merge branch '10.0-release' into lmiller1990/UNIFY-1305
ZachJW34 044bd4d
fix scaffolded files from indexHtmlFile merge
ZachJW34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import type { FRONTEND_FRAMEWORKS } from '@packages/scaffold-config' | ||
| import type { SnapshotScaffoldTestResult } from '@packages/launchpad/cypress/tasks/snapshotsScaffold' | ||
|
|
||
| // The tests in this file take an existing project without Cypress Configured | ||
| // and add Cypress using the launchpad setup wizard. | ||
| // | ||
| // See `system-tests/projects/pristine` for an example. | ||
| // | ||
| // After adding a test for a project for the first time and running | ||
| // this spec, it will see what files are scaffolded and save them | ||
| // in a directory named `expected-cypress-{lang}-{testingType}`. | ||
| // For example, when configuring the `pristine` project using | ||
| // plain JS for E2E testing, it will make | ||
| // a new directory in `pristine` named `expected-cypress-js-e2e` | ||
| // containing the scaffolded files. | ||
| // | ||
| // Each subsequent run will compare the scaffolded files in the | ||
| // `expected-cypress-js-e2e` directory to the newly created ones. | ||
| // | ||
| // If there is a discrepancy, the test will fail and show the diff in the command log. | ||
| // | ||
| // To update your expected files, just delete the `expected-cypress` and re-run the test, | ||
| // or modify them by hand. | ||
| function scaffoldAndOpenE2EProject ( | ||
| name: Parameters<typeof cy.scaffoldProject>[0], | ||
| language: 'js' | 'ts', | ||
| args?: Parameters<typeof cy.openProject>[1], | ||
| ) { | ||
| cy.scaffoldProject(name) | ||
| cy.openProject(name, args) | ||
|
|
||
| cy.visitLaunchpad() | ||
|
|
||
| cy.contains('Welcome to Cypress!').should('be.visible') | ||
| cy.contains('[data-cy-testingtype="e2e"]', 'Not Configured') | ||
| cy.contains('[data-cy-testingtype="component"]', 'Not Configured') | ||
| cy.contains('E2E Testing').click() | ||
| cy.contains(language === 'js' ? 'JavaScript' : 'TypeScript').click() | ||
| cy.contains('Next').click() | ||
| cy.contains('We added the following files to your project.') | ||
| cy.contains('Continue').click() | ||
| cy.contains('Choose a Browser') | ||
| } | ||
|
|
||
| function scaffoldAndOpenCTProject ( | ||
| name: Parameters<typeof cy.scaffoldProject>[0], | ||
| language: 'js' | 'ts', | ||
| framework: typeof FRONTEND_FRAMEWORKS[number]['name'], | ||
| bundler?: typeof FRONTEND_FRAMEWORKS[number]['supportedBundlers'][number]['name'], | ||
| args?: Parameters<typeof cy.openProject>[1], | ||
| ) { | ||
| cy.scaffoldProject(name) | ||
| cy.openProject(name, args) | ||
|
|
||
| cy.visitLaunchpad() | ||
|
|
||
| cy.contains('Welcome to Cypress!').should('be.visible') | ||
| cy.contains('[data-cy-testingtype="e2e"]', 'Not Configured') | ||
| cy.contains('[data-cy-testingtype="component"]', 'Not Configured') | ||
| cy.contains('Component Testing').click() | ||
| cy.contains(language === 'js' ? 'JavaScript' : 'TypeScript').click() | ||
|
|
||
| cy.contains('Pick a framework').click() | ||
| cy.contains(framework).click() | ||
| if (bundler) { | ||
| cy.contains(bundler).click() | ||
| } | ||
|
|
||
| cy.contains('Next Step').click() | ||
| cy.contains('Skip').click() | ||
| cy.contains('We added the following files to your project.') | ||
| cy.contains('Continue').click() | ||
| cy.contains('Choose a Browser') | ||
| } | ||
|
|
||
| function assertScaffoldedFilesAreCorrect (language: 'js' | 'ts', testingType: Cypress.TestingType, ctFramework?: string) { | ||
| cy.withCtx((ctx) => ctx.currentProject).then((currentProject) => { | ||
| cy.task<SnapshotScaffoldTestResult>('snapshotCypressDirectory', { | ||
| currentProject, | ||
| testingType, | ||
| language, | ||
| ctFramework, | ||
| }) | ||
| .then((result) => { | ||
| if (result.status === 'ok') { | ||
| return result | ||
| } | ||
|
|
||
| throw new Error(result.message) | ||
| }) | ||
| .then((res) => { | ||
| cy.log(`✅ ${res.message}`) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| describe('scaffolding new projects', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need more to express the scaffolded CT |
||
| it('scaffolds E2E for a JS project', () => { | ||
| scaffoldAndOpenE2EProject('pristine', 'js') | ||
| assertScaffoldedFilesAreCorrect('js', 'e2e') | ||
| }) | ||
|
|
||
| it('scaffolds E2E for a TS project', () => { | ||
| scaffoldAndOpenE2EProject('pristine', 'ts') | ||
| assertScaffoldedFilesAreCorrect('ts', 'e2e') | ||
| }) | ||
|
|
||
| it('scaffolds CT for a JS project', () => { | ||
| scaffoldAndOpenCTProject('pristine', 'js', 'Create React App (v5)') | ||
| assertScaffoldedFilesAreCorrect('js', 'component', 'Create React App (v5)') | ||
| }) | ||
|
|
||
| it('scaffolds CT for a TS project', () => { | ||
| scaffoldAndOpenCTProject('pristine', 'ts', 'Create React App (v5)') | ||
| assertScaffoldedFilesAreCorrect('ts', 'component', 'Create React App (v5)') | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import path from 'path' | ||
| import globby from 'globby' | ||
| import fs from 'fs-extra' | ||
| import disparity from 'disparity' | ||
| import dedent from 'dedent' | ||
| import { cyTmpDir } from '@tooling/system-tests/lib/fixtures' | ||
|
|
||
| const systemTestsDir = path.join(__dirname, '..', '..', '..', '..', 'system-tests', 'projects') | ||
|
|
||
| export interface SnapshotScaffoldTestResult { | ||
| status: 'ok' | 'fail' | ||
| message: string | ||
| } | ||
|
|
||
| interface FileToDiff { | ||
| actual: string | ||
| expected: string | ||
| } | ||
|
|
||
| async function snapshotScaffoldedFiles (expectedScaffoldDir: string, filesToDiff: FileToDiff[]): Promise<SnapshotScaffoldTestResult> { | ||
| // see if existing snapshots exist compared to source | ||
| // project in `system-tests/projects` | ||
| // expected-cypress-{'js' | 'ts'} | ||
| fs.mkdirSync(expectedScaffoldDir) | ||
|
|
||
| await Promise.all(filesToDiff.map(async (files) => { | ||
| const { expected, actual } = files | ||
|
|
||
| await fs.ensureFile(expected) | ||
| await fs.copy(actual, expected) | ||
| await fs.copy(actual, expected) | ||
| })) | ||
|
|
||
| return { | ||
| status: 'ok', | ||
| message: `Created expected files based on test`, | ||
| } | ||
| } | ||
|
|
||
| function compareScaffoldedFiles (filesToDiff: FileToDiff[]): SnapshotScaffoldTestResult { | ||
| for (const { actual, expected } of filesToDiff) { | ||
| try { | ||
| const read = (f: string) => fs.readFileSync(f, 'utf-8') | ||
| const actualContent = read(actual).trim() | ||
| const expectedContent = read(expected).trim() | ||
| const diff = disparity.unifiedNoColor(actualContent, expectedContent, {}) | ||
|
|
||
| if (diff !== '') { | ||
| return { | ||
| status: 'fail', | ||
| message: `Expected contents of ${actual} and ${expected} to match. Diff: ${diff}`, | ||
| } | ||
| } | ||
| } catch (err) { | ||
| const e = err as NodeJS.ErrnoException | ||
|
|
||
| if (e.code === 'ENOENT') { | ||
| return { | ||
| status: 'fail', | ||
| message: `Expected ${e.path} to exist, but it was not found`, | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| status: 'fail', | ||
| message: `Unexpected error: ${e.message}`, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| status: 'ok', | ||
| message: 'Scaffolded files matched expected files', | ||
| } | ||
| } | ||
|
|
||
| interface SnapshotCypressDirectoryOptions { | ||
| currentProject: string | ||
| language: 'js' | 'ts' | ||
| testingType: Cypress.TestingType | ||
| ctFramework?: string | ||
| } | ||
|
|
||
| function removeHyphensAndBrackets (str: string) { | ||
| return str.toLowerCase().replaceAll(' ', '-').replaceAll('(', '').replaceAll(')', '') | ||
| } | ||
|
|
||
| export async function snapshotCypressDirectory ({ currentProject, language, testingType, ctFramework }: SnapshotCypressDirectoryOptions): Promise<SnapshotScaffoldTestResult> { | ||
| if (!currentProject.startsWith(cyTmpDir)) { | ||
| throw Error(dedent` | ||
| snapshotCypressDirectory is designed to be used with system-tests infrastructure. | ||
| It should not be used with projects that are not created in a temporary directory | ||
| by the system-tests infrastructure. | ||
|
|
||
| Expected currentProject to be in /tmp. currentProject found at path ${currentProject}`) | ||
| } | ||
|
|
||
| const projectDir = currentProject.replace(cyTmpDir, systemTestsDir) | ||
| const projectName = projectDir.replace(systemTestsDir, '').slice(1) | ||
|
|
||
| let expectedScaffoldDir = path.join(projectDir, `expected-cypress-${language}-${testingType}`) | ||
|
|
||
| if (ctFramework) { | ||
| expectedScaffoldDir += `-${removeHyphensAndBrackets(ctFramework)}` | ||
| } | ||
|
|
||
| const joinPosix = (...s: string[]) => path.join(...s).split(path.sep).join(path.posix.sep) | ||
|
|
||
| const files = ( | ||
| await Promise.all([ | ||
| globby(joinPosix(currentProject, 'cypress'), { onlyFiles: true }), | ||
| globby(joinPosix(currentProject, 'cypress.config.*'), { onlyFiles: true }), | ||
| ]) | ||
| ).reduce((acc, curr) => { | ||
| return [acc, curr].flat(2) | ||
| }, []) | ||
|
|
||
| const actualRelativeFiles = files.map((file) => { | ||
| return file.replace(cyTmpDir, '').slice(projectName.length + 2) | ||
| }) | ||
|
|
||
| const filesToDiff = actualRelativeFiles.map<FileToDiff>((file) => { | ||
| return { | ||
| actual: path.join(currentProject, file), | ||
| expected: path.join(expectedScaffoldDir, file), | ||
| } | ||
| }) | ||
|
|
||
| if (fs.existsSync(expectedScaffoldDir)) { | ||
| // compare! | ||
| return compareScaffoldedFiles(filesToDiff) | ||
| } | ||
|
|
||
| return snapshotScaffoldedFiles(expectedScaffoldDir, filesToDiff) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...sts/projects/pristine/expected-cypress-js-component-create-react-app-v5/cypress.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const { devServer } = require('@cypress/react/plugins/react-scripts') | ||
|
|
||
| module.exports = { | ||
| component: { | ||
| devServer, | ||
| }, | ||
| } |
5 changes: 5 additions & 0 deletions
5
.../pristine/expected-cypress-js-component-create-react-app-v5/cypress/fixtures/example.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "name": "Using fixtures to represent data", | ||
| "email": "hello@cypress.io", | ||
| "body": "Fixtures are a great way to mock data for responses to routes" | ||
| } |
25 changes: 25 additions & 0 deletions
25
...ts/pristine/expected-cypress-js-component-create-react-app-v5/cypress/support/commands.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // *********************************************** | ||
| // This example commands.js 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) => { ... }) |
14 changes: 14 additions & 0 deletions
14
...ne/expected-cypress-js-component-create-react-app-v5/cypress/support/component-index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
| <meta name="viewport" content="width=device-width,initial-scale=1.0"> | ||
| <title>Components App</title> | ||
|
|
||
| </head> | ||
| <body> | ||
|
|
||
| <div id="__cy_root"></div> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.