Files
cypress/system-tests/lib/fixtures.ts
Lachlan Miller f0d3a48679 feat: React 18 support (#22876)
* update cli exports

* add additional react adapters

* update system test infra to better cover react versions

* use idiomatic cy.mount and cy.unmount

* add additional test projects

* update tests

* add new modules

* remove dead code, organize code more

* add react 16 project

* update webpack to resolve react correctly

* add test for react 16

* update snaps

* add react adapters

* ignore cli/react files

* use official rollup plugin to bundle npm/react

* update yarn lock for webpack dev server tests

* update vite dev server projects

* update config

* remove console.log

* update tsconfig

* fix tests

* fix another test

* update snaps

* update snaps

* fix build

* remove react{16,17}, update tests

* update build

* add missing export

* update test

* fixing tests

* fixing tests

* update snaps

* update snaps again

* build artifacts on circle

* dont try to update rollup plugin

* update circle

* update

* add missing build step

* update deps, remove old code

* revert circle changes

* do not hoist deps from react18

* remove deps
2022-07-22 11:35:09 +10:00

147 lines
4.0 KiB
TypeScript

import fs from 'fs-extra'
import _path from 'path'
import chokidar from 'chokidar'
import tempDir from 'temp-dir'
import type { ProjectFixtureDir } from './fixtureDirs'
export const root = _path.join(__dirname, '..')
const serverRoot = _path.join(__dirname, '../../packages/server/')
export { fixtureDirs, ProjectFixtureDir } from './fixtureDirs'
export const projects = _path.join(root, 'projects')
export const projectFixtures = _path.join(root, 'project-fixtures')
export const cyTmpDir = _path.join(tempDir, 'cy-projects')
const projectFixtureDirs = fs.readdirSync(projectFixtures, { withFileTypes: true }).filter((f) => f.isDirectory()).map((f) => f.name)
const safeRemove = (path) => {
try {
fs.removeSync(path)
} catch (_err) {
const err = _err as NodeJS.ErrnoException
// Windows does not like the en masse deleting of files, since the AV will hold
// a lock on files when they are written. This skips deleting if the lock is
// encountered.
if (err.code === 'EBUSY' && process.platform === 'win32') {
return console.error(`Remove failed for ${ path } due to EBUSY. Skipping on Windows.`)
}
throw err
}
}
// copy contents instead of deleting+creating new file, which can cause
// filewatchers to lose track of toFile.
const copyContents = (fromFile, toFile) => {
return Promise.all([
fs.open(toFile, 'w'),
fs.readFile(fromFile),
])
.then(([toFd, fromFileBuf]) => {
return fs.write(toFd, fromFileBuf)
.finally(() => {
return fs.close(toFd)
})
})
}
// copies all of the project fixtures
// to the cyTmpDir .projects in the root
export function scaffold () {
fs.copySync(projects, cyTmpDir)
}
/**
* Given a project name, copy the project's test files to the temp dir.
* Returns the scaffolded directory
*/
export async function scaffoldProject (project: ProjectFixtureDir): Promise<string> {
const to = _path.join(cyTmpDir, project)
const from = projectFixturePath(project)
await fs.copy(from, to)
try {
const packageJson = require(`${to}/package.json`)
const fixtureDir = packageJson.projectFixtureDirectory
if (fixtureDir) {
if (!projectFixtureDirs.includes(fixtureDir)) {
throw new Error(`Invalid project fixture directory: ${fixtureDir}, expected one of ${projectFixtureDirs}`)
}
await fs.copy(_path.join(projectFixtures, fixtureDir), to, { overwrite: false })
}
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e
}
}
return to
}
export function scaffoldWatch () {
const watchdir = _path.resolve(__dirname, '../projects')
console.log('watching files due to --no-exit', watchdir)
chokidar.watch(watchdir, {
})
.on('change', (srcFilepath, stats) => {
const tmpFilepath = _path.join(cyTmpDir, _path.relative(watchdir, srcFilepath))
return copyContents(srcFilepath, tmpFilepath)
})
.on('error', console.error)
}
// removes all of the project fixtures
// from the cyTmpDir .projects in the root
export function remove () {
safeRemove(cyTmpDir)
}
export function removeProject (name: ProjectFixtureDir) {
safeRemove(projectPath(name))
}
// Removes node_modules that might have been leftover from an initial "yarn"
// in the fixture dir
export function clearFixtureNodeModules (name: ProjectFixtureDir) {
try {
safeRemove(_path.join(projects, name, 'node_modules'))
} catch {
//
}
}
// returns the path to project fixture
// in the cyTmpDir
export function project (name: ProjectFixtureDir) {
return projectPath(name)
}
export function projectPath (name: ProjectFixtureDir) {
return _path.join(cyTmpDir, name)
}
export function get (fixture, encoding: BufferEncoding = 'utf8') {
return fs.readFileSync(_path.join(serverRoot, 'test', 'support', 'fixtures', fixture), { encoding })
}
export function path (fixture: ProjectFixtureDir) {
return _path.join(serverRoot, 'test', 'support', 'fixtures', fixture)
}
export function projectFixturePath (name: ProjectFixtureDir) {
return _path.join(projects, name)
}
export default module.exports