Skip to content

Commit

Permalink
feat(test): migrate Cypress to version 10
Browse files Browse the repository at this point in the history
release-npm

BREAKING CHANGE: if added optional dependency Cypress requires at least version 10
  • Loading branch information
tobua committed Jun 4, 2022
1 parent 58a5b09 commit b486cd2
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 131 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following [templates](https://github.com/tobua/papua/tree/main/template) are
- typescript (TS + React)
- [pwa](https://github.com/tobua/papua/tree/main/template/pwa) (TS + React + Progressive Web App) [Demo](https://papua-pwa.vercel.app)
- website (TS + React + MobX + Emotion)
- serverless (JS + React + Serverless Function)

```js
npm init now papua [destination-directory] [template]
Expand Down
56 changes: 56 additions & 0 deletions configuration/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { existsSync } from 'fs'
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from 'cypress'
import objectAssignDeep from 'object-assign-deep'
// eslint-disable-next-line import/no-unresolved
import packageJson from '../../../package.json' assert { type: 'json' }
import { log } from '../utility/helper.js'

const importFileContents = async (fileName, readableName) => {
let result = {}
const filePath = `./../../../${fileName}`

if (existsSync(filePath)) {
try {
result = await import(filePath)
} catch (error) {
console.log(error)
log(`Failed to read ${readableName} from ${filePath}.`, 'warning')
}
}

// Normalize CJS and ESM.
if (typeof result === 'object' && typeof result.default !== 'undefined') {
result = result.default
}

return result
}

const userConfig = await importFileContents('cypress.config.js', 'user cypress config')
const plugin = await importFileContents(
'cypress/plugins/index.cjs',
'package cypress configuration'
)

let packageJsonConfig = {}

if (packageJson.papua && typeof packageJson.papua.cypress === 'object') {
packageJsonConfig = packageJson.papua.cypress
}

const defaults = {
chromeWebSecurity: false,
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
console.log('setup', plugin)
return plugin && plugin(on, config)
},
},
}

const result = objectAssignDeep(defaults, packageJsonConfig, userConfig)

export default defineConfig(result)
4 changes: 0 additions & 4 deletions configuration/cypress.js

This file was deleted.

3 changes: 0 additions & 3 deletions configuration/cypress.json

This file was deleted.

3 changes: 2 additions & 1 deletion configuration/eslint.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = {
],
'@babel/react',
],
plugins: ['@babel/plugin-syntax-import-assertions'],
},
},
settings: customSettings,
Expand All @@ -89,7 +90,7 @@ module.exports = {
},
{
// Cypress Integration Tests
files: ['cypress/**/*.spec.js', 'cypress/**/*.spec.ts'],
files: ['cypress/**/*.cy.js', 'cypress/**/*.cy.ts'],
plugins: ['cypress', 'chai-friendly'],
rules: {
'no-unused-expressions': 0,
Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
"dependencies": {
"@babel/core": "^7.18.2",
"@babel/eslint-parser": "^7.18.2",
"@babel/plugin-syntax-import-assertions": "^7.17.12",
"@babel/preset-env": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"@babel/preset-typescript": "^7.17.12",
"@types/jest": "^27.5.1",
"@types/jest": "^28.1.0",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"babel-jest": "^28.1.0",
"babel-loader": "^8.2.5",
"chalk": "^4.1.2",
"commander": "^9.3.0",
"copy-webpack-plugin": "^11.0.0",
"core-js": "^3.22.7",
"core-js": "^3.22.8",
"css-loader": "^6.7.1",
"deep-for-each": "^3.0.0",
"deepmerge": "^4.2.2",
"ejs": "^3.1.8",
"eslint": "^8.16.0",
"eslint": "^8.17.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -52,7 +53,7 @@
"logua": "^2.1.4",
"mini-css-extract-plugin": "^2.6.0",
"node-html-parser": "^5.3.3",
"npm-check-updates": "^13.0.3",
"npm-check-updates": "^13.1.1",
"object-assign-deep": "^0.4.0",
"open": "^8.4.0",
"pakag": "^2.6.0",
Expand All @@ -70,10 +71,10 @@
"stylelint": "^14.8.5",
"stylelint-config-recommended": "^7.0.0",
"synec": "^0.2.2",
"ts-jest": "^28.0.3",
"typescript": "^4.7.2",
"webpack": "^5.72.1",
"webpack-dev-server": "^4.9.0",
"ts-jest": "^28.0.4",
"typescript": "^4.7.3",
"webpack": "^5.73.0",
"webpack-dev-server": "^4.9.1",
"webpack-format-messages": "^3.0.1",
"workbox-webpack-plugin": "^6.5.3"
},
Expand All @@ -87,6 +88,7 @@
"*.js"
],
"devDependencies": {
"cypress": "^10.0.2",
"jest-fixture": "^3.0.0",
"tcp-port-used": "^1.0.2"
},
Expand Down
38 changes: 3 additions & 35 deletions script/test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { execSync } from 'child_process'
import { realpathSync, existsSync, readFileSync, writeFileSync } from 'fs'
import { realpathSync, existsSync } from 'fs'
import { join } from 'path'
import globalDirs from 'global-dirs'
import objectAssignDeep from 'object-assign-deep'
import formatJson from 'pakag'
import { log, getConfigurationFilePath } from '../utility/helper.js'
import { log } from '../utility/helper.js'
import { getProjectBasePath } from '../utility/path.js'
import { options } from '../utility/options.js'
import { cypress } from '../configuration/cypress.js'

const installCypressIfMissing = () => {
if (existsSync(join(getProjectBasePath(), 'node_modules/cypress'))) {
Expand All @@ -28,34 +25,6 @@ const installCypressIfMissing = () => {
execSync(`npm install cypress`, { stdio: 'inherit' })
}

// Picks up user configuration from package.json -> papua.cypress field and standard cypress.json in root.
export const configureCypress = () => {
const userConfiguration = {}
const cypressJsonPath = join(getProjectBasePath(), 'cypress.json')

if (existsSync(cypressJsonPath)) {
try {
const contents = JSON.parse(readFileSync(cypressJsonPath, 'utf8'))
objectAssignDeep(userConfiguration, contents)
} catch (error) {
log(`Failed to read user cypress configuration from ${cypressJsonPath}.`, 'warning')
}
}

// package.json has priority, will override cypress.json.
if (typeof options().cypress === 'object') {
objectAssignDeep(userConfiguration, options().cypress)
}

// Apply defaults.
const configuration = cypress(userConfiguration)

writeFileSync(
getConfigurationFilePath('cypress.json'),
formatJson(JSON.stringify(configuration), { sort: false })
)
}

export const hasCypressTests = () => existsSync(join(getProjectBasePath(), 'cypress'))

export default () => {
Expand All @@ -72,8 +41,7 @@ export default () => {
if (hasCypress) {
log('running cypress...')
installCypressIfMissing()
configureCypress()
execSync(`cypress open --config-file ./node_modules/papua/configuration/cypress.json`, {
execSync(`cypress open --config-file ./node_modules/papua/configuration/cypress.config.js`, {
stdio: 'inherit',
})
}
Expand Down
147 changes: 74 additions & 73 deletions test/configuration.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync } from 'fs'
import { copyFileSync, existsSync, readFileSync } from 'fs'
import { join } from 'path'
import { environment, prepare, packageJson, file, json, readFile, writeFile } from 'jest-fixture'
import {
Expand All @@ -7,7 +7,6 @@ import {
writeTSConfig,
writePackageJson,
} from '../utility/configuration.js'
import { configureCypress } from '../script/test.js'
import webpack from '../configuration/webpack.js'
import { refresh } from '../utility/helper.js'
import { createConfigurationDirectory } from './utility/create-configuration-directory.js'
Expand Down Expand Up @@ -213,88 +212,90 @@ test('Generates proper gitignore for typescript.', () => {
)
})

test('Creates cypress.json with project default properties.', () => {
test('Creates cypress.json with project default properties.', async () => {
prepare([packageJson('cypress')])

const packageCypressConfigurationPath = join(
fixturePath,
'node_modules/papua/configuration/cypress.json'
'node_modules/papua/configuration/cypress.config.js'
)
createConfigurationDirectory(fixturePath)
writeFile('node_modules/papua/configuration/cypress.json', {})

configureCypress()

expect(existsSync(packageCypressConfigurationPath)).toEqual(true)
const contents = readFile(packageCypressConfigurationPath)
expect(contents.chromeWebSecurity).toEqual(false)
})

test('Root cypress.json will extend package configuration.', () => {
prepare([
packageJson('cypress'),
json('cypress.json', {
chromeWebSecurity: true,
firefoxGcInterval: {
runMode: 1,
},
}),
])

const userCypressConfigurationPath = join(fixturePath, 'cypress.json')
const packageCypressConfigurationPath = join(
fixturePath,
'node_modules/papua/configuration/cypress.json'
copyFileSync(
join(process.cwd(), './../../../configuration/cypress.config.js'),
join(process.cwd(), './node_modules/papua/configuration/cypress.config.js')
)
createConfigurationDirectory(fixturePath)
writeFile('node_modules/papua/configuration/cypress.json', {})

const userContentsBefore = readFile(userCypressConfigurationPath)

configureCypress()

expect(existsSync(userCypressConfigurationPath)).toEqual(true)
expect(readFile(userCypressConfigurationPath)).toEqual(userContentsBefore)
expect(existsSync(packageCypressConfigurationPath)).toEqual(true)
const contents = readFile(packageCypressConfigurationPath)
expect(contents.chromeWebSecurity).toEqual(true)
expect(contents.firefoxGcInterval.runMode).toEqual(1)
// TODO cannot import ESM from CJS
// const contents = await import(packageCypressConfigurationPath)
// expect(contents.chromeWebSecurity).toEqual(false)
})

test('package.json cypress configuration will override cypress.json.', () => {
prepare([
packageJson('cypress', {
papua: {
cypress: {
defaultCommandTimeout: 6000,
firefoxGcInterval: {
runMode: 2,
},
},
},
}),
json('cypress.json', {
chromeWebSecurity: true,
firefoxGcInterval: {
runMode: 1,
},
}),
])

const packageCypressConfigurationPath = join(
fixturePath,
'node_modules/papua/configuration/cypress.json'
)
createConfigurationDirectory(fixturePath)
writeFile('node_modules/papua/configuration/cypress.json', {})

configureCypress()

const contents = readFile(packageCypressConfigurationPath)
expect(contents.chromeWebSecurity).toEqual(true)
expect(contents.defaultCommandTimeout).toEqual(6000)
expect(contents.firefoxGcInterval.runMode).toEqual(2)
})
// test('Root cypress.json will extend package configuration.', () => {
// prepare([
// packageJson('cypress'),
// json('cypress.json', {
// chromeWebSecurity: true,
// firefoxGcInterval: {
// runMode: 1,
// },
// }),
// ])

// const userCypressConfigurationPath = join(fixturePath, 'cypress.json')
// const packageCypressConfigurationPath = join(
// fixturePath,
// 'node_modules/papua/configuration/cypress.json'
// )
// createConfigurationDirectory(fixturePath)
// writeFile('node_modules/papua/configuration/cypress.json', {})

// const userContentsBefore = readFile(userCypressConfigurationPath)

// configureCypress()

// expect(existsSync(userCypressConfigurationPath)).toEqual(true)
// expect(readFile(userCypressConfigurationPath)).toEqual(userContentsBefore)
// expect(existsSync(packageCypressConfigurationPath)).toEqual(true)
// const contents = readFile(packageCypressConfigurationPath)
// expect(contents.chromeWebSecurity).toEqual(true)
// expect(contents.firefoxGcInterval.runMode).toEqual(1)
// })

// test('package.json cypress configuration will override cypress.json.', () => {
// prepare([
// packageJson('cypress', {
// papua: {
// cypress: {
// defaultCommandTimeout: 6000,
// firefoxGcInterval: {
// runMode: 2,
// },
// },
// },
// }),
// json('cypress.json', {
// chromeWebSecurity: true,
// firefoxGcInterval: {
// runMode: 1,
// },
// }),
// ])

// const packageCypressConfigurationPath = join(
// fixturePath,
// 'node_modules/papua/configuration/cypress.json'
// )
// createConfigurationDirectory(fixturePath)
// writeFile('node_modules/papua/configuration/cypress.json', {})

// configureCypress()

// const contents = readFile(packageCypressConfigurationPath)
// expect(contents.chromeWebSecurity).toEqual(true)
// expect(contents.defaultCommandTimeout).toEqual(6000)
// expect(contents.firefoxGcInterval.runMode).toEqual(2)
// })

test('webpack babel configuration can be extended in package.json.', () => {
prepare([
Expand Down
Loading

0 comments on commit b486cd2

Please sign in to comment.