-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2613 from dhis2/chore/upgrade-cypress-to-v12
chore: upgrade cypress to v12
- Loading branch information
Showing
14 changed files
with
298 additions
and
132 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ build | |
coverage | ||
cypress.env.json | ||
cypress/screenshots | ||
cypress/videos | ||
|
||
# Custom | ||
.vscode | ||
|
This file contains 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,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', | ||
}, | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,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 } |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,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) | ||
}) | ||
}) |
This file contains 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 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
Oops, something went wrong.