Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let running = false
let errFn
let queueId = 0
let sessionId = null
let sessionStack = [] // Stack to support nested sessions
let asyncErr = null
let ignoredErrs = []

Expand Down Expand Up @@ -89,6 +90,7 @@ module.exports = {
if (promise && running) this.catch()
queueId++
sessionId = null
sessionStack = [] // Clear the session stack
asyncErr = null
log(`${currentQueue()} Starting recording promises`)
promise = Promise.resolve()
Expand Down Expand Up @@ -123,8 +125,13 @@ module.exports = {
*/
start(name) {
if (sessionId) {
debug(`${currentQueue()}Session already started as ${sessionId}`)
this.restore(sessionId)
debug(`${currentQueue()}Session already started as ${sessionId}, nesting session ${name}`)
// Push current session to stack instead of restoring it
sessionStack.push({
id: sessionId,
promise: promise,
running: this.running,
})
}
debug(`${currentQueue()}Starting <${name}> session`)
tasks.push('--->')
Expand All @@ -142,9 +149,18 @@ module.exports = {
tasks.push('<---')
debug(`${currentQueue()}Finalize <${name}> session`)
this.running = false
sessionId = null
this.catch(errFn)
promise = promise.then(() => oldPromises.pop())

// Restore parent session from stack if available
if (sessionStack.length > 0) {
const parentSession = sessionStack.pop()
sessionId = parentSession.id
this.running = parentSession.running
debug(`${currentQueue()}Restored parent session <${sessionId}>`)
} else {
sessionId = null
}
},

/**
Expand Down
82 changes: 54 additions & 28 deletions test/data/sandbox/base_test_session.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
Feature('Session');
Feature('Session')

Scenario('basic session @1', ({ I }) => {
I.do('writing');
I.do('writing')
session('davert', () => {
I.do('reading');
});
I.do('playing');
I.do('reading')
})
I.do('playing')
session('john', () => {
I.do('crying');
});
I.do('crying')
})
session('davert', () => {
I.do('smiling');
});
I.do('laughing');
I.do('smiling')
})
I.do('laughing')
session('mike', () => {
I.do('spying');
});
I.do('spying')
})
session('john', () => {
I.do('lying');
});
I.do('waving');
});
I.do('lying')
})
I.do('waving')
})

Scenario('session defined not used @2', ({ I }) => {
session('davert');
I.do('writing');
I.do('playing');
session('davert')
I.do('writing')
I.do('playing')
session('john', () => {
I.do('crying');
});
I.do('crying')
})
session('davert', () => {
I.do('smiling');
});
I.do('laughing');
I.do('smiling')
})
I.do('laughing')
session('davert', () => {
I.do('singing');
});
I.do('waving');
});
I.do('singing')
})
I.do('waving')
})

Scenario('tryTo inside session @3', ({ I }) => {
const { tryTo } = require('../../../lib/effects')
I.do('before session')
session('tryTo-test', async () => {
I.do('inside session')
await tryTo(() => {
I.do('inside tryTo')
})
I.do('after tryTo')
})
I.do('after session')
})

Scenario('session inside tryTo @4', ({ I }) => {
const { tryTo } = require('../../../lib/effects')
I.do('before tryTo')
tryTo(async () => {
I.do('inside tryTo')
await session('nested-session', () => {
I.do('inside nested session')
})
I.do('after session')
})
I.do('after tryTo')
})