chore: convert @packages/scaffold-config tests from mocha to vitest (#32568)

* chore: start vitest conversion and convertsupportFile to vitest

* chore: convert frameworks spec to vitest

* chore: convert detect spec to vitest

* chore: convert ct-detect-third-party spec to vitest

* chore: convert component index template to vitest

* chore: clean up scaffold-config as tests are now converted to vitest

* fix issue where projectRoot was passed inncorrectly in test
This commit is contained in:
Bill Glesias
2025-09-29 19:47:59 -04:00
committed by GitHub
parent 942a71f0cd
commit d048e46bc7
14 changed files with 177 additions and 169 deletions
+1 -1
View File
@@ -1784,7 +1784,7 @@ jobs:
source ./scripts/ensure-node.sh
yarn lerna run types
- sanitize-verify-and-store-mocha-results:
expectedResultCount: 15
expectedResultCount: 14
verify-release-readiness:
<<: *defaults
+1 -1
View File
@@ -3486,7 +3486,7 @@ jobs:
yarn lerna run types
name: Test types
- sanitize-verify-and-store-mocha-results:
expectedResultCount: 15
expectedResultCount: 14
working_directory: ~/cypress
v8-integration-tests:
environment:
+1 -1
View File
@@ -102,7 +102,7 @@
- [ ] packages/packherd-require
- [ ] packages/proxy
- [ ] packages/rewriter
- [ ] packages/scaffold-config
- [x] packages/scaffold-config**COMPLETED**
- [ ] packages/server
- [ ] packages/socket
- [x] packages/stderr-filtering ✅ **COMPLETED**
+3 -4
View File
@@ -10,8 +10,8 @@
"clean": "rimraf --glob './src/*.js' './src/**/*.js' './src/**/**/*.js' './test/**/*.js' || echo 'cleaned'",
"clean-deps": "rimraf node_modules",
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
"test": "yarn test-unit",
"test-unit": "mocha -r @packages/ts/register 'test/unit/**' --config ./test/.mocharc.js --exit --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
"test": "vitest run",
"test-debug": "npx vitest --inspect-brk --no-file-parallelism --test-timeout=0",
"tslint": "tslint --config ../ts/tslint.json --project ."
},
"dependencies": {
@@ -28,8 +28,7 @@
"devDependencies": {
"@packages/ts": "0.0.0-development",
"@packages/types": "0.0.0-development",
"chai": "4.2.0",
"mocha": "7.0.1"
"vitest": "^3.2.4"
},
"files": [
"src"
+9
View File
@@ -171,6 +171,15 @@ export function detectLanguage ({ projectRoot, customConfigFile, pkgJson }: Dete
try {
const typescriptFile = require.resolve('typescript', { paths: [projectRoot] })
// sometimes require.resolve returns a path that does not exist but is cached by require.resolve()
const exists = fs.existsSync(typescriptFile)
if (!exists) {
debug('Resolved typescript from %s does not exist', typescriptFile)
return 'js'
}
debug('Resolved typescript from %s', typescriptFile)
} catch {
debug('No typescript installed - using js')
@@ -1,3 +0,0 @@
module.exports = {
watchFiles: ['test/**/*.ts', 'src/**/*.ts'],
}
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import { describe, it, expect } from 'vitest'
import dedent from 'dedent'
import componentIndexHtmlGenerator from '../../src/component-index-template'
import { CT_FRAMEWORKS } from '../../src/frameworks'
import componentIndexHtmlGenerator from '../src/component-index-template'
import { CT_FRAMEWORKS } from '../src/frameworks'
describe('componentIndexHtmlGenerator', () => {
it('strips spaces and newlines appropriately', () => {
@@ -21,7 +21,7 @@ describe('componentIndexHtmlGenerator', () => {
</body>
</html>`
expect(generator()).to.eq(expected)
expect(generator()).toEqual(expected)
})
it('handles header modifier', () => {
@@ -42,7 +42,7 @@ describe('componentIndexHtmlGenerator', () => {
</body>
</html>`
expect(generator()).to.eq(expected)
expect(generator()).toEqual(expected)
})
it('generates correct template for Next.js', () => {
@@ -66,6 +66,6 @@ describe('componentIndexHtmlGenerator', () => {
</body>
</html>`
expect(actual).to.eq(expected)
expect(actual).toEqual(expected)
})
})
@@ -1,8 +1,8 @@
import { scaffoldMigrationProject, fakeDepsInNodeModules } from './detect.spec'
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { scaffoldMigrationProject, fakeDepsInNodeModules } from './scaffolding'
import fs from 'fs-extra'
import path from 'path'
import { detectThirdPartyCTFrameworks, validateThirdPartyModule, isThirdPartyDefinition, isRepositoryRoot } from '../../src'
import { expect } from 'chai'
import { detectThirdPartyCTFrameworks, validateThirdPartyModule, isThirdPartyDefinition, isRepositoryRoot } from '../src'
import os from 'os'
import solidJs from './fixtures'
@@ -25,31 +25,31 @@ async function scaffoldQwikApp (thirdPartyModuleNames: Array<'cypress-ct-qwik' |
}
describe('isThirdPartyDefinition', () => {
context('global package', () => {
describe('global package', () => {
it('returns false for invalid prefix', () => {
const res = isThirdPartyDefinition({ ...solidJs, type: 'non-cypress-ct' })
expect(res).to.be.false
expect(res).toBe(false)
})
it('returns true for valid prefix', () => {
const res = isThirdPartyDefinition({ ...solidJs, type: 'cypress-ct-solid-js' })
expect(res).to.be.true
expect(res).toBe(true)
})
})
context('namespaced package', () => {
describe('namespaced package', () => {
it('returns false for non third party with namespace', () => {
const res = isThirdPartyDefinition({ ...solidJs, type: '@org/non-cypress-ct' })
expect(res).to.be.false
expect(res).toBe(false)
})
it('returns true for third party with namespace', () => {
const res = isThirdPartyDefinition({ ...solidJs, type: '@org/cypress-ct-solid-js' })
expect(res).to.be.true
expect(res).toBe(true)
})
})
})
@@ -68,7 +68,7 @@ describe('isRepositoryRoot', () => {
it('returns false if there is nothing in the directory', async () => {
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR)
expect(isCurrentRepositoryRoot).to.be.false
expect(isCurrentRepositoryRoot).toBe(false)
})
it('returns true if there is a Git directory', async () => {
@@ -76,7 +76,7 @@ describe('isRepositoryRoot', () => {
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR)
expect(isCurrentRepositoryRoot).to.be.true
expect(isCurrentRepositoryRoot).toBe(true)
})
it('returns false if there is a package.json without workspaces field', async () => {
@@ -91,7 +91,7 @@ describe('isRepositoryRoot', () => {
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR)
expect(isCurrentRepositoryRoot).to.be.false
expect(isCurrentRepositoryRoot).toBe(false)
})
it('returns true if there is a package.json with workspaces field', async () => {
@@ -109,7 +109,7 @@ describe('isRepositoryRoot', () => {
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR)
expect(isCurrentRepositoryRoot).to.be.true
expect(isCurrentRepositoryRoot).toBe(true)
})
})
@@ -119,7 +119,7 @@ describe('detectThirdPartyCTFrameworks', () => {
const thirdPartyFrameworks = await detectThirdPartyCTFrameworks(projectRoot)
expect(thirdPartyFrameworks.frameworks[0].type).eq('cypress-ct-qwik')
expect(thirdPartyFrameworks.frameworks[0].type).toEqual('cypress-ct-qwik')
})
it('detects third party frameworks in org namespace', async () => {
@@ -127,7 +127,7 @@ describe('detectThirdPartyCTFrameworks', () => {
const thirdPartyFrameworks = await detectThirdPartyCTFrameworks(projectRoot)
expect(thirdPartyFrameworks.frameworks[0].type).eq('@org/cypress-ct-qwik')
expect(thirdPartyFrameworks.frameworks[0].type).toEqual('@org/cypress-ct-qwik')
})
it('ignores misconfigured third party frameworks', async () => {
@@ -135,8 +135,8 @@ describe('detectThirdPartyCTFrameworks', () => {
const thirdPartyFrameworks = await detectThirdPartyCTFrameworks(projectRoot)
expect(thirdPartyFrameworks.frameworks.length).eq(1)
expect(thirdPartyFrameworks.frameworks[0].type).eq('cypress-ct-qwik')
expect(thirdPartyFrameworks.frameworks.length).toEqual(1)
expect(thirdPartyFrameworks.frameworks[0].type).toEqual('cypress-ct-qwik')
})
it('detects third party frameworks in monorepos with hoisted dependencies', async () => {
@@ -150,19 +150,19 @@ describe('detectThirdPartyCTFrameworks', () => {
// Look for third-party modules in packages/foo (where Cypress was launched from)
const thirdPartyFrameworks = await detectThirdPartyCTFrameworks(projectRoot)
expect(thirdPartyFrameworks.frameworks[0].type).eq('cypress-ct-qwik')
expect(thirdPartyFrameworks.frameworks[0].type).toEqual('cypress-ct-qwik')
})
it('validates third party module', () => {
expect(() => validateThirdPartyModule(solidJs)).to.not.throw()
expect(() => validateThirdPartyModule(solidJs)).not.toThrow()
const gen = (m: any) => m
expect(() => validateThirdPartyModule(gen({ ...solidJs, type: 'misconfigured' }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, type: 'misconfigured' }))).toThrow()
expect(() => validateThirdPartyModule(gen({ ...solidJs, name: 5 }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, supportedBundlers: ['random'] }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, detectors: {} }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, dependencies: {} }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, componentIndexHtml: {} }))).to.throw()
expect(() => validateThirdPartyModule(gen({ ...solidJs, supportedBundlers: ['random'] }))).toThrow()
expect(() => validateThirdPartyModule(gen({ ...solidJs, detectors: {} }))).toThrow()
expect(() => validateThirdPartyModule(gen({ ...solidJs, dependencies: {} }))).toThrow()
expect(() => validateThirdPartyModule(gen({ ...solidJs, componentIndexHtml: {} }))).toThrow()
})
})
@@ -1,79 +1,18 @@
import Module from 'module'
import { expect } from 'chai'
import { vi, describe, it, expect, beforeEach } from 'vitest'
import fs from 'fs-extra'
import type { ProjectFixtureDir } from '@tooling/system-tests'
import { detectFramework, detectLanguage, PkgJson, CT_FRAMEWORKS, resolveComponentFrameworkDefinition, WIZARD_DEPENDENCY_WEBPACK } from '../../src'
import Fixtures from '@tooling/system-tests'
import { detectFramework, detectLanguage, PkgJson, CT_FRAMEWORKS, resolveComponentFrameworkDefinition, WIZARD_DEPENDENCY_WEBPACK } from '../src'
import path from 'path'
import solidJs, { solidDep } from './fixtures'
beforeEach(() => {
// @ts-ignore
Module._cache = Object.create(null)
// @ts-ignore
Module._pathCache = Object.create(null)
require.cache = Object.create(null)
})
export async function scaffoldMigrationProject (project: ProjectFixtureDir) {
const projectPath = Fixtures.projectPath(project)
Fixtures.clearFixtureNodeModules(project)
Fixtures.removeProject(project)
await Fixtures.scaffoldProject(project)
return projectPath
}
interface DepToFake {
dependency: string
version: string
}
interface DevDepToFake {
devDependency: string
version: string
}
/**
* The way we detect dependencies is by using resolve-from (https://www.npmjs.com/package/resolve-from).
* In these unit tests, we don't want to actually run `npm install`, since it is slow,
* so this function fakes that the dependencies are installed by creating pretend dependency like this:
* `node_modules/<dependency>/package.json.
* Inside `package.json` we add the minimal:
*
* {
* "version": "5.0.0",
* "main": "index.js"
* }
*
* We have some real e2e tests that actually run `npm install`.
* Those are in launchpad/cypress/e2e/scaffold-component-testing.cy.ts.
*/
export function fakeDepsInNodeModules (cwd: string, deps: Array<DepToFake | DevDepToFake>) {
fs.mkdirSync(path.join(cwd, 'node_modules'))
for (const dep of deps) {
const depName = 'dependency' in dep ? dep.dependency : dep.devDependency
const nodeModules = path.join(cwd, 'node_modules', depName)
fs.mkdirpSync(nodeModules)
fs.writeJsonSync(
path.join(cwd, 'node_modules', depName, 'package.json'),
{ main: 'index.js', version: dep.version },
)
fs.writeFileSync(
path.join(cwd, 'node_modules', depName, 'index.js'),
'export STUB = true',
)
}
}
import { fakeDepsInNodeModules, scaffoldMigrationProject } from './scaffolding'
const resolvedCtFrameworks = CT_FRAMEWORKS.map((x) => resolveComponentFrameworkDefinition(x))
describe('detectFramework', () => {
beforeEach(() => {
vi.resetModules()
})
it('React App with webpack 5', async () => {
const projectPath = await scaffoldMigrationProject('react18-webpack-unconfigured')
@@ -84,8 +23,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('react')
expect(actual.bundler).to.eq('webpack')
expect(actual.framework?.type).toEqual('react')
expect(actual.bundler).toEqual('webpack')
})
it(`Webpack with Vue 3`, async () => {
@@ -98,8 +37,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('vue3')
expect(actual.bundler).to.eq('webpack')
expect(actual.framework?.type).toEqual('vue3')
expect(actual.bundler).toEqual('webpack')
})
it(`React with Vite`, async () => {
@@ -112,8 +51,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('react')
expect(actual.bundler).to.eq('vite')
expect(actual.framework?.type).toEqual('react')
expect(actual.bundler).toEqual('vite')
})
it(`Vue with Vite`, async () => {
@@ -126,8 +65,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('vue3')
expect(actual.bundler).to.eq('vite')
expect(actual.framework?.type).toEqual('vue3')
expect(actual.bundler).toEqual('vite')
})
;['14.0.0', '15.0.4'].forEach((v) => {
@@ -141,8 +80,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('nextjs')
expect(actual.bundler).to.eq('webpack')
expect(actual.framework?.type).toEqual('nextjs')
expect(actual.bundler).toEqual('webpack')
})
})
@@ -156,8 +95,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('angular')
expect(actual.bundler).to.eq('webpack')
expect(actual.framework?.type).toEqual('angular')
expect(actual.bundler).toEqual('webpack')
})
})
@@ -172,8 +111,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('svelte')
expect(actual.bundler).to.eq('vite')
expect(actual.framework?.type).toEqual('svelte')
expect(actual.bundler).toEqual('vite')
})
})
@@ -187,8 +126,8 @@ describe('detectFramework', () => {
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework?.type).to.eq('svelte')
expect(actual.bundler).to.eq('webpack')
expect(actual.framework?.type).toEqual('svelte')
expect(actual.bundler).toEqual('webpack')
})
it(`no framework or library`, async () => {
@@ -201,18 +140,18 @@ describe('detectFramework', () => {
fs.rmSync(path.join(Fixtures.cyTmpDir, 'node_modules'), { recursive: true, force: true })
const actual = await detectFramework(projectPath, resolvedCtFrameworks)
expect(actual.framework).to.be.undefined
expect(actual.bundler).to.be.undefined
expect(actual.framework).toBeUndefined()
expect(actual.bundler).toBeUndefined()
})
})
describe('detectLanguage', () => {
context('existing project', () => {
describe('existing project', () => {
it('with `cypress.config.ts` should return `ts`', async () => {
const projectRoot = await scaffoldMigrationProject('config-with-ts')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
it('with `cypress.config.mts` should return `ts`', async () => {
@@ -221,14 +160,14 @@ describe('detectLanguage', () => {
fs.moveSync(path.join(projectRoot, 'cypress.config.ts'), path.join(projectRoot, 'cypress.config.mts'))
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
it('with `cypress.config.js` should return `js`', async () => {
const projectRoot = await scaffoldMigrationProject('config-with-js')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
it('with `cypress.config.cjs` should return `js`', async () => {
@@ -237,7 +176,7 @@ describe('detectLanguage', () => {
await fs.move(path.join(projectRoot, 'cypress.config.js'), path.join(projectRoot, 'cypress.config.cjs'))
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
it('with `cypress.config.mjs` should return `js`', async () => {
@@ -246,7 +185,7 @@ describe('detectLanguage', () => {
await fs.move(path.join(projectRoot, 'cypress.config.js'), path.join(projectRoot, 'cypress.config.mjs'))
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
it('with custom TS cypress config file should return `ts`', async () => {
@@ -257,7 +196,7 @@ describe('detectLanguage', () => {
;['ts', 'mts'].forEach((extension) => {
const actual = detectLanguage({ projectRoot, customConfigFile: `custom_config/cypress.config-custom.${extension}`, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
})
@@ -269,7 +208,7 @@ describe('detectLanguage', () => {
;['js', 'cjs', 'mjs'].forEach((extension) => {
const actual = detectLanguage({ projectRoot, customConfigFile: `custom_config/cypress.config-custom.${extension}`, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
})
@@ -279,7 +218,7 @@ describe('detectLanguage', () => {
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '5.8.3' }])
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
it('with only .d.ts files', async () => {
@@ -289,11 +228,11 @@ describe('detectLanguage', () => {
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
})
context('pristine project', () => {
describe('pristine project', () => {
it('with typescript in package.json', async () => {
const projectRoot = await scaffoldMigrationProject('pristine-yarn')
@@ -301,7 +240,7 @@ describe('detectLanguage', () => {
const pkgJson = fs.readJsonSync(path.join(projectRoot, 'package.json'))
const actual = detectLanguage({ projectRoot, pkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
it('with root level tsconfig.json', async () => {
@@ -310,23 +249,23 @@ describe('detectLanguage', () => {
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('ts')
expect(actual).toEqual('ts')
})
it('detects js if typescript is not resolvable when there is a tsconfig.json', async () => {
let projectRoot = await scaffoldMigrationProject('pristine-npm')
const projectRoot = await scaffoldMigrationProject('pristine-npm')
const actual = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
projectRoot = await scaffoldMigrationProject('pristine-npm')
const projectRoot2 = await scaffoldMigrationProject('pristine-npm')
fakeDepsInNodeModules(projectRoot, [{ devDependency: 'typescript', version: '4.3.6' }])
fakeDepsInNodeModules(projectRoot2, [{ devDependency: 'typescript', version: '4.3.6' }])
const actualTypescript = detectLanguage({ projectRoot, pkgJson: {} as PkgJson })
const actualTypescript = detectLanguage({ projectRoot: projectRoot2, pkgJson: {} as PkgJson })
expect(actualTypescript).to.eq('ts')
expect(actualTypescript).toEqual('ts')
})
it('ignores node_modules when checking for tsconfig.json', async () => {
@@ -340,7 +279,7 @@ describe('detectLanguage', () => {
const actual = detectLanguage({ projectRoot, pkgJson })
expect(actual).to.eq('js')
expect(actual).toEqual('js')
})
})
})
@@ -12,15 +12,10 @@ export const solidDep: Cypress.CypressComponentDependency = {
// must be default export
export default defineComponentFramework({
type: 'cypress-ct-solid-js',
category: 'library',
name: 'Solid.js',
supportedBundlers: ['webpack', 'vite'],
detectors: [solidDep],
// Cypress will include the bundler dependency here, if they selected one.
dependencies: () => {
return [solidDep]
@@ -1,8 +1,8 @@
import { expect } from 'chai'
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import fs from 'fs-extra'
import path from 'path'
import os from 'os'
import { isDependencyInstalledByName } from '../../src/frameworks'
import { isDependencyInstalledByName } from '../src/frameworks'
describe('frameworks', () => {
describe('isDependencyInstalledByName', () => {
@@ -21,7 +21,7 @@ describe('frameworks', () => {
it('returns null version if dependency is not found', async () => {
const result = await isDependencyInstalledByName('my-dep', TEMP_DIR)
expect(result).to.eql({ dependency: 'my-dep', detectedVersion: null })
expect(result).toEqual({ dependency: 'my-dep', detectedVersion: null })
})
it('returns null version if there is no version in the package file', async () => {
@@ -36,7 +36,7 @@ describe('frameworks', () => {
const result = await isDependencyInstalledByName('my-dep', TEMP_DIR)
expect(result).to.eql({ dependency: 'my-dep', detectedVersion: null })
expect(result).toEqual({ dependency: 'my-dep', detectedVersion: null })
})
it('returns package version if it finds the dependency', async () => {
@@ -52,7 +52,7 @@ describe('frameworks', () => {
const result = await isDependencyInstalledByName('my-dep', TEMP_DIR)
expect(result).to.eql({ dependency: 'my-dep', detectedVersion: '1.2.3' })
expect(result).toEqual({ dependency: 'my-dep', detectedVersion: '1.2.3' })
})
})
})
@@ -0,0 +1,60 @@
import type { ProjectFixtureDir } from '@tooling/system-tests'
import path from 'path'
import Fixtures from '@tooling/system-tests'
import fs from 'fs-extra'
export async function scaffoldMigrationProject (project: ProjectFixtureDir) {
const projectPath = Fixtures.projectPath(project)
Fixtures.clearFixtureNodeModules(project)
Fixtures.removeProject(project)
await Fixtures.scaffoldProject(project)
return projectPath
}
interface DepToFake {
dependency: string
version: string
}
interface DevDepToFake {
devDependency: string
version: string
}
/**
* The way we detect dependencies is by using resolve-from (https://www.npmjs.com/package/resolve-from).
* In these unit tests, we don't want to actually run `npm install`, since it is slow,
* so this function fakes that the dependencies are installed by creating pretend dependency like this:
* `node_modules/<dependency>/package.json.
* Inside `package.json` we add the minimal:
*
* {
* "version": "5.0.0",
* "main": "index.js"
* }
*
* We have some real e2e tests that actually run `npm install`.
* Those are in launchpad/cypress/e2e/scaffold-component-testing.cy.ts.
*/
export function fakeDepsInNodeModules (cwd: string, deps: Array<DepToFake | DevDepToFake>) {
fs.mkdirSync(path.join(cwd, 'node_modules'))
for (const dep of deps) {
const depName = 'dependency' in dep ? dep.dependency : dep.devDependency
const nodeModules = path.join(cwd, 'node_modules', depName)
fs.mkdirpSync(nodeModules)
fs.writeJsonSync(
path.join(cwd, 'node_modules', depName, 'package.json'),
{ main: 'index.js', version: dep.version },
)
fs.writeFileSync(
path.join(cwd, 'node_modules', depName, 'index.js'),
'export STUB = true',
)
}
}
@@ -1,15 +1,15 @@
import { supportFileComponent } from '../../src/supportFile'
import { describe, it, expect } from 'vitest'
import { supportFileComponent } from '../src/supportFile'
import dedent from 'dedent'
import { expect } from 'chai'
describe('supportFileComponent', () => {
context('react', () => {
describe('react', () => {
const mountModule = 'cypress/react'
it(`handles ${mountModule} and JS`, () => {
const actual = supportFileComponent('js', mountModule)
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.js is processed and
// loaded automatically before your test files.
@@ -40,7 +40,7 @@ describe('supportFileComponent', () => {
it(`handles ${mountModule} and TS`, () => {
const actual = supportFileComponent('ts', mountModule)
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
@@ -81,13 +81,13 @@ describe('supportFileComponent', () => {
})
})
context('vue', () => {
describe('vue', () => {
const mountModule = 'cypress/vue'
it(`handles ${mountModule} and JS`, () => {
const actual = supportFileComponent('js', mountModule)
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.js is processed and
// loaded automatically before your test files.
@@ -118,7 +118,7 @@ describe('supportFileComponent', () => {
it(`handles ${mountModule} and TS`, () => {
const actual = supportFileComponent('ts', mountModule)
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
@@ -159,12 +159,12 @@ describe('supportFileComponent', () => {
})
})
context('angular', () => {
describe('angular', () => {
for (const mountModule of ['cypress/angular'] as const) {
it(`handles ${mountModule} and TS`, () => {
const actual = supportFileComponent('ts', mountModule)
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
@@ -206,11 +206,11 @@ describe('supportFileComponent', () => {
}
})
context('svelte', () => {
describe('svelte', () => {
it(`handles cypress/svelte and JS`, () => {
const actual = supportFileComponent('js', 'cypress/svelte')
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.js is processed and
// loaded automatically before your test files.
@@ -241,7 +241,7 @@ describe('supportFileComponent', () => {
it(`handles cypress/svelte and TS`, () => {
const actual = supportFileComponent('ts', 'cypress/svelte')
expect(actual).to.eq(dedent`
expect(actual).toEqual(dedent`
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['test/**/*.spec.ts'],
globals: true,
environment: 'node',
},
})