|
1 | 1 | const I = actor(); |
| 2 | +const event = codeceptjs.event; |
| 3 | + |
| 4 | +const capturedScenarios = []; |
| 5 | + |
| 6 | +Before(test => { |
| 7 | + capturedScenarios.push({ |
| 8 | + phase: 'Before-hook', |
| 9 | + title: test && test.title, |
| 10 | + tags: test && Array.isArray(test.tags) ? [...test.tags] : null, |
| 11 | + }); |
| 12 | +}); |
| 13 | + |
| 14 | +After(test => { |
| 15 | + capturedScenarios.push({ |
| 16 | + phase: 'After-hook', |
| 17 | + title: test && test.title, |
| 18 | + tags: test && Array.isArray(test.tags) ? [...test.tags] : null, |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +event.dispatcher.on(event.test.before, test => { |
| 23 | + capturedScenarios.push({ |
| 24 | + phase: 'event.test.before', |
| 25 | + title: test && test.title, |
| 26 | + tags: test && Array.isArray(test.tags) ? [...test.tags] : null, |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +event.dispatcher.on(event.test.after, test => { |
| 31 | + capturedScenarios.push({ |
| 32 | + phase: 'event.test.after', |
| 33 | + title: test && test.title, |
| 34 | + tags: test && Array.isArray(test.tags) ? [...test.tags] : null, |
| 35 | + }); |
| 36 | +}); |
2 | 37 |
|
3 | 38 | Given('I opened website', () => { |
4 | 39 | // From "gherkin/basic.feature" {"line":8,"column":5} |
5 | 40 | I.amOnPage('/'); |
6 | 41 | }); |
7 | 42 |
|
| 43 | +Then('the Before hook should have captured this scenario', () => { |
| 44 | + const captureSnapshot = JSON.stringify(capturedScenarios, null, 2); |
| 45 | + |
| 46 | + const matchesScenario = entry => |
| 47 | + entry.tags && entry.tags.includes('@scenarioHook') && entry.tags.includes('@hookCapture'); |
| 48 | + |
| 49 | + const bddBefore = capturedScenarios.find( |
| 50 | + entry => entry.phase === 'Before-hook' && matchesScenario(entry), |
| 51 | + ); |
| 52 | + const eventBefore = capturedScenarios.find( |
| 53 | + entry => entry.phase === 'event.test.before' && matchesScenario(entry), |
| 54 | + ); |
| 55 | + |
| 56 | + if (!bddBefore) throw new Error(`BDD Before() did not fire for @scenarioHook. Captured:\n${captureSnapshot}`); |
| 57 | + if (!eventBefore) throw new Error(`event.test.before did not fire with real test. Captured:\n${captureSnapshot}`); |
| 58 | + |
| 59 | + for (const entry of [bddBefore, eventBefore]) { |
| 60 | + if (!entry.title || entry.title === '...') { |
| 61 | + throw new Error(`Placeholder title in ${entry.phase}: ${JSON.stringify(entry)}`); |
| 62 | + } |
| 63 | + if (!entry.title.includes('Before hook captures scenario metadata')) { |
| 64 | + throw new Error(`Unexpected title in ${entry.phase}: ${JSON.stringify(entry)}`); |
| 65 | + } |
| 66 | + } |
| 67 | +}); |
| 68 | + |
8 | 69 | When('go to {string} page', (url) => { |
9 | 70 | // From "gherkin/basic.feature" {"line":9,"column":5} |
10 | 71 | I.amOnPage(url); |
|
0 commit comments