Skip to content

Commit

Permalink
fix(network-shim): only incrementally update missing request stub sta…
Browse files Browse the repository at this point in the history
…te (#209)

* fix(network-shim): only incrementally update missing request stub state

* chore: fix typo

Co-authored-by: Médi-Rémi Hashim <[email protected]>

Co-authored-by: Médi-Rémi Hashim <[email protected]>
  • Loading branch information
HendrikThePendric and mediremi authored May 27, 2021
1 parent 5e7e025 commit e2ccea8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/cypress-commands/src/setups/enableNetworkShim/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDisabledMode, isCaptureMode } from './utils.js'
import { isDisabledMode, isStubMode, isCaptureMode } from './utils.js'
import captureRequests from './captureRequests.js'
import stubRequests from './stubRequests.js'
import validateVersionMinor from './validateVersionMinor.js'
Expand All @@ -24,6 +24,7 @@ export function enableNetworkShim() {
// This will mutate the state
captureRequests(networkShimState)
} else {
// This also mutates the state
stubRequests(networkShimState)
}
})
Expand All @@ -33,8 +34,26 @@ export function enableNetworkShim() {
if (!isDisabledMode()) {
// First get the updated local state from the alias
cy.get('@networkShimState').then(networkShimState => {
// Then update the plugin state
cy.task('setNetworkShimState', networkShimState)
/*
* In capture mode the state needs to be incrementally updated
* across tests, so after every feature the entire plugin state
* gets overwritten.
*/
if (isCaptureMode()) {
cy.task('setNetworkShimState', networkShimState)
}
/*
* In stub mode the state needs to be kept static across features
* apart from the missing request stubs which do need to be
* incrementally updated across features. So in stub mode we only
* update that state property in the plugin.
*/
if (isStubMode()) {
cy.task(
'setNetworkShimMissingRequestStubs',
networkShimState.missingRequestStubs
)
}
})
}
})
Expand Down
6 changes: 6 additions & 0 deletions packages/cypress-plugins/src/plugins/networkShim/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ module.exports = function networkShim(
state = newState
return state
},
setNetworkShimMissingRequestStubs(missingRequestStubs) {
if (Array.isArray(missingRequestStubs)) {
state.missingRequestStubs = missingRequestStubs
}
return state
},
})

on('after:run', results => {
Expand Down

0 comments on commit e2ccea8

Please sign in to comment.