Skip to content

Commit dc28a1b

Browse files
authored
Fix flaky cypress test (#4758)
1 parent 309dfdb commit dc28a1b

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

.circleci/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ jobs:
248248
php_version: << parameters.php_version >>
249249
- run:
250250
name: Run Cypress tests
251-
max_auto_reruns: 3
252251
command: |
253252
rm -rf cypress/tmp cypress/results
254253
mkdir cypress/tmp

.ddev/commands/host/dkan-module-test-cypress

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ else
2828
echo "No package.json present, not trying to install."
2929
fi
3030

31-
# Commenting out until we get a lower roll worked out.
32-
# {"name":"testapiuser","mail":"testapiuser@test.com","role":"api_user"},
3331
TEST_USERS_JSON='[
3432
{"name":"testadmin","mail":"testadmin@test.com","role":"administrator"}
3533
]'
36-
readarray -t TU_TEST_USERS < <(jq -c '.[]' <<<"$TEST_USERS_JSON")
37-
for TU_TEST_USER in "${TU_TEST_USERS[@]}"; do
34+
35+
jq -c '.[]' <<<"$TEST_USERS_JSON" | while read -r TU_TEST_USER; do
3836
name=$(jq -r '.name' <<<"$TU_TEST_USER")
3937
mail=$(jq -r '.mail' <<<"$TU_TEST_USER")
4038
role=$(jq -r '.role' <<<"$TU_TEST_USER")
@@ -46,8 +44,7 @@ npx cypress@"$DKAN_MODULE_CYPRESS_VERSION" info
4644
CYPRESS_baseUrl="$DDEV_PRIMARY_URL" npx cypress@"$DKAN_MODULE_CYPRESS_VERSION" run "$@"
4745
DKAN_MODULE_RESULT=$?
4846

49-
for TU_TEST_USER in "${TU_TEST_USERS[@]}"; do
50-
name=$(jq -r '.name' <<<"$TU_TEST_USER")
47+
jq -r '.[] | .name' <<<"$TEST_USERS_JSON" | while read -r name; do
5148
ddev drush user:cancel --delete-content $name -y
5249
done
5350

cypress/e2e/10_workflow_transitions.cy.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ context('DKAN Workflow', () => {
144144
cy.get('#edit-submit').click()
145145
cy.get('.messages--status').should('contain', 'has been updated')
146146

147-
// Ensure dataset is now hidden from search
148-
dkan.searchMetastore({fulltext: dataset_title, facets: ''}).then((response) => {
147+
// Ensure dataset is now hidden from search (poll until index reflects the
148+
// state change, since search-index updates can be asynchronous in CI)
149+
dkan.searchMetastoreUntil(
150+
{fulltext: dataset_title, facets: ''},
151+
(body) => !body.results || body.results.length === 0
152+
).then((response) => {
149153
expect(response.status).to.eq(200)
150154
expect(response.body.results).to.be.empty
151155
})

cypress/support/helpers/dkan.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ export function searchMetastore (params = {}) {
100100
return cy.request('GET', url)
101101
}
102102

103+
// Poll the metastore search endpoint until the predicate returns true or the
104+
// timeout elapses. Useful when search-index updates are asynchronous (e.g.
105+
// after a moderation-state transition in CI).
106+
export function searchMetastoreUntil (params = {}, predicate, timeout = 15000) {
107+
const interval = 2000
108+
const param_string = (new URLSearchParams(params)).toString()
109+
const url = getMetastoreSearchEndpoint() + '?' + param_string
110+
const deadline = Date.now() + timeout
111+
112+
function attempt () {
113+
return cy.request('GET', url).then((response) => {
114+
if (predicate(response.body) || Date.now() >= deadline) {
115+
return cy.wrap(response)
116+
}
117+
return cy.wait(interval).then(() => attempt())
118+
})
119+
}
120+
121+
return attempt()
122+
}
123+
103124
export function generateMetastore (schema_id, identifier = null) {
104125
// Generate a unique metastore identifier if one was not supplied.
105126
identifier = identifier || generateMetastoreIdentifier()

0 commit comments

Comments
 (0)