1- import * as fs from 'node:fs'
2- import * as path from 'node:path'
3-
4- import { AstBuilder , compile , GherkinClassicTokenMatcher , Parser } from '@cucumber/gherkin'
5- import { GherkinDocument , IdGenerator , Pickle } from '@cucumber/messages'
1+ import { IdGenerator } from '@cucumber/messages'
62import { expect , use } from 'chai'
73import sinon from 'sinon'
84import sinonChai from 'sinon-chai'
95
6+ import { parseGherkin } from '../test/parseGherkin'
107import { AmbiguousError } from './AmbiguousError'
118import { buildSupportCode } from './buildSupportCode'
12- import { DataTable } from './DataTable'
139import { makeTestPlan } from './makeTestPlan'
1410import { UndefinedError } from './UndefinedError'
1511
1612use ( sinonChai )
1713
18- function parseGherkin (
19- file : string ,
20- newId : ( ) => string
21- ) : { gherkinDocument : GherkinDocument ; pickles : ReadonlyArray < Pickle > } {
22- const data = fs . readFileSync ( path . join ( __dirname , '..' , 'testdata' , file ) , { encoding : 'utf-8' } )
23- const builder = new AstBuilder ( newId )
24- const matcher = new GherkinClassicTokenMatcher ( )
25- const parser = new Parser ( builder , matcher )
26- const uri = 'features/' + file
27- const gherkinDocument = {
28- uri,
29- ...parser . parse ( data ) ,
30- }
31- const pickles = compile ( gherkinDocument , uri , newId )
32- return {
33- gherkinDocument,
34- pickles,
35- }
36- }
37-
3814describe ( 'makeTestPlan' , ( ) => {
39- class FakeWorld { }
4015 const testRunStartedId = 'run-id'
4116 let newId : ( ) => string
4217
@@ -154,7 +129,7 @@ describe('makeTestPlan', () => {
154129 }
155130 )
156131
157- expect ( ( ) => result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( undefined ) ) . to . throw ( AmbiguousError )
132+ expect ( ( ) => result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( ) ) . to . throw ( AmbiguousError )
158133 } )
159134
160135 it ( 'throws if a step is undefined' , ( ) => {
@@ -169,18 +144,15 @@ describe('makeTestPlan', () => {
169144 )
170145
171146 try {
172- result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( undefined )
147+ result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
173148 } catch ( err : any ) {
174149 expect ( err ) . to . be . instanceOf ( UndefinedError )
175150 expect ( err . pickleStep ) . to . eq ( pickles [ 0 ] . steps [ 0 ] )
176151 }
177152 } )
178153
179154 it ( 'matches and prepares a step without parameters' , ( ) => {
180- let capturedThis : any
181- const fn = sinon . spy ( function ( this : any ) {
182- capturedThis = this
183- } )
155+ const fn = sinon . stub ( )
184156
185157 const { gherkinDocument, pickles } = parseGherkin ( 'minimal.feature' , newId )
186158 const supportCodeLibrary = buildSupportCode ( { newId } )
@@ -198,19 +170,13 @@ describe('makeTestPlan', () => {
198170 }
199171 )
200172
201- const fakeWorld = new FakeWorld ( )
202- const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( fakeWorld )
173+ const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
174+ expect ( prepared . fn ) . to . eq ( fn )
203175 expect ( prepared . args ) . to . deep . eq ( [ ] )
204- prepared . fn ( )
205- expect ( fn ) . to . have . been . calledWithExactly ( )
206- expect ( capturedThis ) . to . eq ( fakeWorld )
207176 } )
208177
209178 it ( 'matches and prepares a step with parameters' , ( ) => {
210- let capturedThis : any
211- const fn = sinon . spy ( function ( this : any ) {
212- capturedThis = this
213- } )
179+ const fn = sinon . stub ( )
214180
215181 const { gherkinDocument, pickles } = parseGherkin ( 'parameters.feature' , newId )
216182 const supportCodeLibrary = buildSupportCode ( { newId } )
@@ -228,19 +194,13 @@ describe('makeTestPlan', () => {
228194 }
229195 )
230196
231- const fakeWorld = new FakeWorld ( )
232- const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( fakeWorld )
233- expect ( prepared . args ) . to . deep . eq ( [ 4 , 5 ] )
234- prepared . fn ( ...prepared . args )
235- expect ( fn ) . to . have . been . calledWithExactly ( ...prepared . args )
236- expect ( capturedThis ) . to . eq ( fakeWorld )
197+ const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
198+ expect ( prepared . fn ) . to . eq ( fn )
199+ expect ( prepared . args . map ( ( arg ) => arg . getValue ( undefined ) ) ) . to . deep . eq ( [ 4 , 5 ] )
237200 } )
238201
239202 it ( 'matches and prepares a step with a data table' , ( ) => {
240- let capturedThis : any
241- const fn = sinon . spy ( function ( this : any ) {
242- capturedThis = this
243- } )
203+ const fn = sinon . stub ( )
244204
245205 const { gherkinDocument, pickles } = parseGherkin ( 'datatable.feature' , newId )
246206 const supportCodeLibrary = buildSupportCode ( { newId } )
@@ -258,24 +218,14 @@ describe('makeTestPlan', () => {
258218 }
259219 )
260220
261- const fakeWorld = new FakeWorld ( )
262- const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( fakeWorld )
263- expect ( prepared . args ) . to . deep . eq ( [
264- new DataTable ( [
265- [ 'a' , 'b' , 'c' ] ,
266- [ '1' , '2' , '3' ] ,
267- ] ) ,
268- ] )
269- prepared . fn ( ...prepared . args )
270- expect ( fn ) . to . have . been . calledWithExactly ( ...prepared . args )
271- expect ( capturedThis ) . to . eq ( fakeWorld )
221+ const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
222+ expect ( prepared . fn ) . to . eq ( fn )
223+ expect ( prepared . args ) . to . deep . eq ( [ ] )
224+ expect ( prepared . dataTable ) . to . eq ( pickles [ 0 ] . steps [ 0 ] . argument ?. dataTable )
272225 } )
273226
274227 it ( 'matches and prepares a step with a doc string' , ( ) => {
275- let capturedThis : any
276- const fn = sinon . spy ( function ( this : any ) {
277- capturedThis = this
278- } )
228+ const fn = sinon . stub ( )
279229
280230 const { gherkinDocument, pickles } = parseGherkin ( 'docstring.feature' , newId )
281231 const supportCodeLibrary = buildSupportCode ( { newId } )
@@ -293,12 +243,10 @@ describe('makeTestPlan', () => {
293243 }
294244 )
295245
296- const fakeWorld = new FakeWorld ( )
297- const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( fakeWorld )
298- expect ( prepared . args ) . to . deep . eq ( [ 'Hello world' ] )
299- prepared . fn ( ...prepared . args )
300- expect ( fn ) . to . have . been . calledWithExactly ( ...prepared . args )
301- expect ( capturedThis ) . to . eq ( fakeWorld )
246+ const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
247+ expect ( prepared . fn ) . to . eq ( fn )
248+ expect ( prepared . args ) . to . deep . eq ( [ ] )
249+ expect ( prepared . docString ) . to . eq ( pickles [ 0 ] . steps [ 0 ] . argument ?. docString )
302250 } )
303251 } )
304252
@@ -472,11 +420,7 @@ describe('makeTestPlan', () => {
472420 } )
473421
474422 it ( 'prepares Before hooks for execution' , ( ) => {
475- let capturedThis : any
476- const fn = sinon . spy ( function ( this : any ) {
477- capturedThis = this
478- } )
479-
423+ const fn = sinon . stub ( )
480424 const { gherkinDocument, pickles } = parseGherkin ( 'minimal.feature' , newId )
481425 const supportCodeLibrary = buildSupportCode ( { newId } )
482426 . beforeHook ( {
@@ -497,19 +441,13 @@ describe('makeTestPlan', () => {
497441 }
498442 )
499443
500- const fakeWorld = new FakeWorld ( )
501- const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( fakeWorld )
444+ const prepared = result . testCases [ 0 ] . testSteps [ 0 ] . prepare ( )
445+ expect ( prepared . fn ) . to . eq ( fn )
502446 expect ( prepared . args ) . to . deep . eq ( [ ] )
503- prepared . fn ( )
504- expect ( fn ) . to . have . been . calledWithExactly ( )
505- expect ( capturedThis ) . to . eq ( fakeWorld )
506447 } )
507448
508449 it ( 'prepares After hooks for execution' , ( ) => {
509- let capturedThis : any
510- const fn = sinon . spy ( function ( this : any ) {
511- capturedThis = this
512- } )
450+ const fn = sinon . stub ( )
513451
514452 const { gherkinDocument, pickles } = parseGherkin ( 'minimal.feature' , newId )
515453 const supportCodeLibrary = buildSupportCode ( { newId } )
@@ -531,12 +469,9 @@ describe('makeTestPlan', () => {
531469 }
532470 )
533471
534- const fakeWorld = new FakeWorld ( )
535- const prepared = result . testCases [ 0 ] . testSteps [ 3 ] . prepare ( fakeWorld )
472+ const prepared = result . testCases [ 0 ] . testSteps [ 3 ] . prepare ( )
473+ expect ( prepared . fn ) . to . eq ( fn )
536474 expect ( prepared . args ) . to . deep . eq ( [ ] )
537- prepared . fn ( )
538- expect ( fn ) . to . have . been . calledWithExactly ( )
539- expect ( capturedThis ) . to . eq ( fakeWorld )
540475 } )
541476 } )
542477
0 commit comments