Skip to content

Commit

Permalink
Merge pull request #2613 from dhis2/chore/upgrade-cypress-to-v12
Browse files Browse the repository at this point in the history
chore: upgrade cypress to v12
  • Loading branch information
HendrikThePendric authored Jul 12, 2023
2 parents 0b51fe7 + b2a9aa3 commit a7e34e6
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 132 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dhis2-preview-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
runs-on: ubuntu-latest
if: "!github.event.push.repository.fork && github.actor != 'dependabot[bot]'"
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14.x

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dhis2-verify-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

call-workflow-e2e-prod:
needs: [build, lint, test]
uses: ./.github/workflows/e2e-prod.yml
uses: dhis2/line-listing-app/.github/workflows/e2e-prod.yml@master
secrets:
baseurl: ${{ secrets.CYPRESS_DHIS2_BASE_URL_DEV }}
username: ${{ secrets.CYPRESS_DHIS2_USERNAME }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/dhis2-verify-commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
lint-pr-title:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: c-hive/gha-yarn-cache@v1
- run: yarn install --frozen-lockfile
- id: commitlint
Expand All @@ -20,7 +20,7 @@ jobs:
lint-commits:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: c-hive/gha-yarn-cache@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build
coverage
cypress.env.json
cypress/screenshots
cypress/videos

# Custom
.vscode
Expand Down
54 changes: 54 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { chromeAllowXSiteCookies } = require('@dhis2/cypress-plugins')
const { defineConfig } = require('cypress')
const {
excludeByVersionTags,
} = require('./cypress/plugins/excludeByVersionTags.js')

async function setupNodeEvents(on, config) {
chromeAllowXSiteCookies(on, config)
excludeByVersionTags(on, config)

if (!config.env.dhis2InstanceVersion) {
throw new Error(
'dhis2InstanceVersion is missing. Check the README for more information.'
)
}

return config
}

module.exports = defineConfig({
projectId: 'sojh88',
e2e: {
setupNodeEvents,
baseUrl: 'http://localhost:3000',
specPattern: 'cypress/integration/**/*.cy.js',
viewportWidth: 1280,
viewportHeight: 800,
defaultCommandTimeout: 15000,
/* Globally disable test isolation because the test suite
* contains many tests with sequential steps */
testIsolation: false,
// Record video
video: true,
/* Only compress and upload videos for failures.
* This will save execution time and reduce the risk
* out-of-memory issues on the CI machine */
videoUploadOnPasses: false,
// Enabled to reduce the risk of out-of-memory issues
experimentalMemoryManagement: true,
// Set to a low number to reduce the risk of out-of-memory issues
numTestsKeptInMemory: 5,
/* When allowing 1 retry on CI, the test suite will pass if
* it's flaky. And/but we also get to identify flaky tests on the
* Cypress Dashboard. */
retries: {
runMode: 1,
openMode: 0,
},
},
env: {
dhis2DatatestPrefix: 'dhis2-datavisualizer',
networkMode: 'live',
},
})
14 changes: 0 additions & 14 deletions cypress.json

This file was deleted.

22 changes: 12 additions & 10 deletions cypress/integration/options/icon.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
DIMENSION_TYPE_DATA_SET,
DIMENSION_TYPE_EVENT_DATA_ITEM,
DIMENSION_TYPE_INDICATOR,
DIMENSION_TYPE_PROGRAM_INDICATOR,
VIS_TYPE_SINGLE_VALUE,
visTypeDisplayNames,
} from '@dhis2/analytics'
Expand Down Expand Up @@ -56,20 +55,22 @@ const expectIconToBeHidden = () => {
cy.getBySelLike('visualization-icon').should('not.exist')
}

// TODO: Remove the commented out types below once 2.40.1 has been released, as only indicators are supported in 2.40.0

const TEST_TYPES = [
DIMENSION_TYPE_INDICATOR,
DIMENSION_TYPE_DATA_ELEMENT,
DIMENSION_TYPE_DATA_SET,
DIMENSION_TYPE_EVENT_DATA_ITEM,
DIMENSION_TYPE_PROGRAM_INDICATOR,
// DIMENSION_TYPE_DATA_ELEMENT,
// DIMENSION_TYPE_DATA_SET,
// DIMENSION_TYPE_EVENT_DATA_ITEM,
// DIMENSION_TYPE_PROGRAM_INDICATOR,
]

const TEST_ITEMS = {
[DIMENSION_TYPE_INDICATOR]: 'ANC 2 Coverage',
[DIMENSION_TYPE_DATA_ELEMENT]: 'ANC 2nd visit',
[DIMENSION_TYPE_DATA_SET]: 'Child Health',
[DIMENSION_TYPE_EVENT_DATA_ITEM]: 'MCH Weight (g)',
[DIMENSION_TYPE_PROGRAM_INDICATOR]: 'Average weight (g)',
// [DIMENSION_TYPE_DATA_ELEMENT]: 'ANC 2nd visit',
// [DIMENSION_TYPE_DATA_SET]: 'Child Health',
// [DIMENSION_TYPE_EVENT_DATA_ITEM]: 'MCH Weight (g)',
// [DIMENSION_TYPE_PROGRAM_INDICATOR]: 'Average weight (g)',
}

const PAGE_SIZE = 50
Expand Down Expand Up @@ -125,7 +126,8 @@ describe('Icon', () => {
expectIconToBeVisible()
})
})
it('icon gets correct color when a legend is in use', () => {
// TODO: Skipped because of the same reason as the commented out tests above
it.skip('icon gets correct color when a legend is in use', () => {
// enable the icon
clickMenuBarOptionsButton()
clickOptionsTab(OPTIONS_TAB_STYLE)
Expand Down
102 changes: 102 additions & 0 deletions cypress/plugins/excludeByVersionTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { tagify } = require('cypress-tags')
const d2config = require('../../d2.config.js')
/*
The list of excluded tags returned by getExcludedTags are the tags that cypress will ignore when running the test suite. So if a test is tagged with one of the tags in the excluded list, then that test will not run.
Using excluded tags (instead of included tags) allows for most of the tests to remain untagged and be run against all supported versions of DHIS2.
DHIS2 officially supports the latest 3 released versions of DHIS2. For example: 2.38, 2.39 and 2.40. Dev would then have version 2.41-SNAPSHOT. Therefore, the getExcludedTags calculates the range of tags based on minimum supported version + 3 (2.38, 2.39, 2.40, 2.41-SNAPSHOT)
With the minimum supported version of 2.38, the tags will always contain "38", "39", "40" and "41", but the comparison symbols will depend on the current instance version.
Allowed tag comparisons are ">", ">=", "<", "<="
*/

const getString = (v) => (typeof v === 'number' ? v.toString() : v)

const extractMinorVersion = (v) =>
v.indexOf('2.') === 0 ? parseInt(v.slice(2, 4)) : parseInt(v.slice(0, 2))

const MIN_DHIS2_VERSION = extractMinorVersion(
getString(d2config.minDHIS2Version)
)

const getInstanceMinorVersion = (dhis2InstanceVersion) => {
if (dhis2InstanceVersion.toLowerCase() === 'dev') {
return MIN_DHIS2_VERSION + 3
}

return extractMinorVersion(dhis2InstanceVersion)
}

const getExcludedTags = (v) => {
const currentInstanceVersion = getInstanceMinorVersion(getString(v))

if (currentInstanceVersion < MIN_DHIS2_VERSION) {
throw new Error(
'Instance version is lower than the minimum supported version'
)
}

let excludeTags = []
if (currentInstanceVersion === MIN_DHIS2_VERSION) {
// For example instance = 2.38, MIN = 2.38
excludeTags = [
`<${currentInstanceVersion}`,
`>${currentInstanceVersion}`,
`>=${currentInstanceVersion + 1}`,
`>${currentInstanceVersion + 1}`,
`>=${currentInstanceVersion + 2}`,
`>${currentInstanceVersion + 2}`,
`>=${currentInstanceVersion + 3}`,
]
} else if (currentInstanceVersion === MIN_DHIS2_VERSION + 1) {
// For example instance = 2.39, MIN = 2.38
excludeTags = [
`<=${currentInstanceVersion - 1}`,
`<${currentInstanceVersion - 1}`,
`<${currentInstanceVersion}`,
`>${currentInstanceVersion}`,
`>=${currentInstanceVersion + 1}`,
`>${currentInstanceVersion + 1}`,
`>=${currentInstanceVersion + 2}`,
]
} else if (currentInstanceVersion === MIN_DHIS2_VERSION + 2) {
// For example instance = 2.40, MIN = 2.38
excludeTags = [
`<=${currentInstanceVersion - 2}`,
`<${currentInstanceVersion - 2}`,
`<=${currentInstanceVersion - 1}`,
`<${currentInstanceVersion - 1}`,
`<${currentInstanceVersion}`,
`>${currentInstanceVersion}`,
`>=${currentInstanceVersion + 1}`,
]
} else {
// For example instance = 2.41, MIN = 2.38
excludeTags = [
`<=${currentInstanceVersion - 3}`,
`<${currentInstanceVersion - 3}`,
`<=${currentInstanceVersion - 2}`,
`<${currentInstanceVersion - 2}`,
`<${currentInstanceVersion - 1}`,
`<=${currentInstanceVersion - 1}`,
`<${currentInstanceVersion}`,
]
}

return excludeTags
}

const excludeByVersionTags = (on, config) => {
const excludedTags = getExcludedTags(config.env.dhis2InstanceVersion)

console.log('instanceVersion', config.env.dhis2InstanceVersion)
console.log('tags to exclude from testing', excludedTags)

config.env.CYPRESS_EXCLUDE_TAGS = excludedTags.join(',')

on('file:preprocessor', tagify(config))
}

module.exports = { excludeByVersionTags, getExcludedTags }
26 changes: 0 additions & 26 deletions cypress/plugins/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@dhis2/cypress-commands'

Cypress.Commands.add('getReduxState', (prop) =>
cy.window().its('store').invoke('getState').its(prop)
)
Expand Down
68 changes: 68 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import './commands.js'

const LOGIN_ENDPOINT = 'dhis-web-commons-security/login.action'
const SESSION_COOKIE_NAME = 'JSESSIONID'
const LOCAL_STORAGE_KEY = 'DHIS2_BASE_URL'

// '2.39' or 39?
const computeEnvVariableName = (instanceVersion) =>
typeof instanceVersion === 'number'
? `${SESSION_COOKIE_NAME}_${instanceVersion}`
: `${SESSION_COOKIE_NAME}_${instanceVersion.split('.').pop()}`

const findSessionCookieForBaseUrl = (baseUrl, cookies) =>
cookies.find(
(cookie) =>
cookie.name === SESSION_COOKIE_NAME && baseUrl.includes(cookie.path)
)

before(() => {
const username = Cypress.env('dhis2Username')
const password = Cypress.env('dhis2Password')
const baseUrl = Cypress.env('dhis2BaseUrl')
const instanceVersion = Cypress.env('dhis2InstanceVersion')

cy.request({
url: `${baseUrl}/${LOGIN_ENDPOINT}`,
method: 'POST',
form: true,
followRedirect: true,
body: {
j_username: username,
j_password: password,
'2fa_code': '',
},
}).should((response) => {
expect(response.status).to.eq(200)
})

cy.getAllCookies()
.should((cookies) => {
expect(cookies.length).to.be.at.least(1)
})
.then((cookies) => {
const sessionCookieForBaseUrl = findSessionCookieForBaseUrl(
baseUrl,
cookies
)
Cypress.env(
computeEnvVariableName(instanceVersion),
JSON.stringify(sessionCookieForBaseUrl)
)
})
})

beforeEach(() => {
const baseUrl = Cypress.env('dhis2BaseUrl')
const instanceVersion = Cypress.env('dhis2InstanceVersion')
const envVariableName = computeEnvVariableName(instanceVersion)
const { name, value, ...options } = JSON.parse(Cypress.env(envVariableName))

localStorage.setItem(LOCAL_STORAGE_KEY, baseUrl)
cy.setCookie(name, value, options)

cy.getAllCookies().should((cookies) => {
expect(findSessionCookieForBaseUrl(baseUrl, cookies)).to.exist
expect(localStorage.getItem(LOCAL_STORAGE_KEY)).to.equal(baseUrl)
})
})
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
"format": "d2-style apply",
"validate-commit": "d2-style check --staged",
"validate-push": "yarn test",
"cy:open": "start-server-and-test 'yarn start:nobrowser' http://localhost:3000 'yarn cypress open --browser chrome --env networkMode=live'",
"cy:open": "start-server-and-test 'yarn start:nobrowser' http://localhost:3000 'yarn cypress open --e2e --env networkMode=live'",
"cy:run": "start-server-and-test 'yarn start:nobrowser' http://localhost:3000 'yarn cypress run --browser chrome headless --env networkMode=live'"
},
"devDependencies": {
"@dhis2/cli-app-scripts": "^10.3.9",
"@dhis2/cli-style": "^10.5.1",
"@dhis2/cli-utils-cypress": "^9.0.2",
"@dhis2/cypress-commands": "^9.0.2",
"@dhis2/cypress-plugins": "^9.0.2",
"cypress": "^8.5.0",
"@dhis2/cypress-commands": "^10.0.3",
"@dhis2/cypress-plugins": "^10.0.2",
"cypress": "^12.16.0",
"cypress-tags": "^1.1.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.7",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/cypressGetExcludedTags.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExcludedTags } from '../../cypress/support/getExcludedTags.js'
import { getExcludedTags } from '../../cypress/plugins/excludeByVersionTags.js'
import { minDHIS2Version } from '../../d2.config.js'

const minVersion = parseInt(minDHIS2Version.slice(2))
Expand Down
Loading

0 comments on commit a7e34e6

Please sign in to comment.