|
| 1 | +import { stripVTControlCharacters } from 'node:util' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { makeTestHarness } from '../utils.js' |
| 4 | + |
| 5 | +describe('World', () => { |
| 6 | + it('shares state between steps but not between test cases', async () => { |
| 7 | + const harness = await makeTestHarness() |
| 8 | + await harness.writeFile( |
| 9 | + 'features/first.feature', |
| 10 | + `Feature: |
| 11 | +Scenario: |
| 12 | + Given a step |
| 13 | + And another step |
| 14 | +Scenario: |
| 15 | + Given a step |
| 16 | + And another step |
| 17 | + ` |
| 18 | + ) |
| 19 | + await harness.writeFile( |
| 20 | + 'features/steps.js', |
| 21 | + `import assert from 'node:assert' |
| 22 | +import { Given } from '@cucumber/node' |
| 23 | +Given('a step', (t) => { |
| 24 | + assert.strictEqual(t.world.foo, undefined) |
| 25 | + t.world.foo = 'bar' |
| 26 | +}) |
| 27 | +Given('another step', (t) => { |
| 28 | + assert.strictEqual(t.world.foo, 'bar') |
| 29 | +}) |
| 30 | + ` |
| 31 | + ) |
| 32 | + const [output] = await harness.run('spec') |
| 33 | + const sanitised = stripVTControlCharacters(output.trim()) |
| 34 | + expect(sanitised).to.include('ℹ pass 6') |
| 35 | + }) |
| 36 | + |
| 37 | + it('uses the world as `this` for user code functions', async () => { |
| 38 | + const harness = await makeTestHarness() |
| 39 | + await harness.writeFile( |
| 40 | + 'features/first.feature', |
| 41 | + `Feature: |
| 42 | +Scenario: |
| 43 | + Given a step |
| 44 | + ` |
| 45 | + ) |
| 46 | + await harness.writeFile( |
| 47 | + 'features/steps.js', |
| 48 | + `import assert from 'node:assert' |
| 49 | +import { After, Before, Given, ParameterType } from '@cucumber/node' |
| 50 | +Before(function(t) { |
| 51 | + assert.strictEqual(this, t.world) |
| 52 | + this.foo = 'bar' |
| 53 | +}) |
| 54 | +ParameterType({ |
| 55 | + name: 'thing', |
| 56 | + regexp: /[a-z]+/, |
| 57 | + transformer(thing) { |
| 58 | + assert.strictEqual(this.foo, 'bar') |
| 59 | + return thing |
| 60 | + }, |
| 61 | +}) |
| 62 | +Given('a {thing}', function(thing) { |
| 63 | + assert.strictEqual(this.foo, 'bar') |
| 64 | +}) |
| 65 | +After(function() { |
| 66 | + assert.strictEqual(this.foo, 'bar') |
| 67 | +}) |
| 68 | + ` |
| 69 | + ) |
| 70 | + const [output] = await harness.run('spec') |
| 71 | + const sanitised = stripVTControlCharacters(output.trim()) |
| 72 | + expect(sanitised).to.include('ℹ pass 4') |
| 73 | + }) |
| 74 | + |
| 75 | + it('uses custom world creator and destroyer', async () => { |
| 76 | + const harness = await makeTestHarness() |
| 77 | + await harness.writeFile( |
| 78 | + 'features/first.feature', |
| 79 | + `Feature: |
| 80 | +Scenario: |
| 81 | + Given a step |
| 82 | + ` |
| 83 | + ) |
| 84 | + await harness.writeFile( |
| 85 | + 'features/steps.js', |
| 86 | + `import { Given } from '@cucumber/node' |
| 87 | +Given('a step', () => {}) |
| 88 | + ` |
| 89 | + ) |
| 90 | + await harness.writeFile( |
| 91 | + 'features/support.js', |
| 92 | + `import { WorldCreator } from '@cucumber/node' |
| 93 | +WorldCreator(async () => { |
| 94 | + console.log('Ran custom world creator!') |
| 95 | + return {} |
| 96 | +}, async (world) => { |
| 97 | + console.log('Ran custom world destroyer!') |
| 98 | +}) |
| 99 | + ` |
| 100 | + ) |
| 101 | + const [output] = await harness.run('spec') |
| 102 | + const sanitised = stripVTControlCharacters(output.trim()) |
| 103 | + expect(sanitised).to.include('Ran custom world creator!') |
| 104 | + expect(sanitised).to.include('Ran custom world destroyer!') |
| 105 | + }) |
| 106 | +}) |
0 commit comments