Skip to content

Commit

Permalink
feat: recompute data session if options change (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 5, 2023
1 parent 8082322 commit 7f5f1ed
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
43 changes: 43 additions & 0 deletions cypress/e2e/changes/data-options-change.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @ts-check

import '../../../src'

describe('data options change', () => {
const name = 'data-options-change-example'

beforeEach(() => {
// force starting fresh
Cypress.clearDataSession(name)
})

it('invalidates the session', () => {
const stub1 = cy.stub().as('setup').returns(42)
const setup = () => stub1()

cy.dataSession({ name, setup })
.then(function (value) {
expect(value, 'yielded').to.equal(42)
expect(stub1).to.be.calledOnce
stub1.resetHistory()
})
.then(() => {
// using exactly same parameters does not recreate anything
cy.log('**same data session options**')
cy.dataSession({ name, setup })
.then(function (value) {
expect(value, 'yielded').to.equal(42)
expect(stub1).to.not.be.called
})
.then(() => {
// adding parameter "expires" invalidates the data session
cy.log('**changed options**')
cy.dataSession({ name, setup, expires: 10_000 }).then(function (
value,
) {
expect(value, 'yielded').to.equal(42)
expect(stub1).to.be.calledOnce
})
})
})
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check

import '../../src'
import '../../../src'

describe('setup function changes', () => {
const name = 'setup-changes-example'
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ Cypress.Commands.add(
})
}

const setupSource = setup.toString()
// we want to recompute the data session if the user changes
// any of its options or the code in the setup callback function
const setupSource =
JSON.stringify({ expires, shareAcrossSpecs, dependsOn, showValue }) +
setup.toString()

const saveData = (data) => {
if (data === undefined) {
Expand Down

0 comments on commit 7f5f1ed

Please sign in to comment.