|
| 1 | +import { Then } from '../../' |
| 2 | +import { World } from '../support/world' |
| 3 | +import _, { Dictionary } from 'lodash' |
| 4 | +import { messages } from '@cucumber/messages' |
| 5 | +import { expect } from 'chai' |
| 6 | + |
| 7 | +interface TestCaseParser { |
| 8 | + pickles: Dictionary<messages.IPickle> |
| 9 | + testCases: Dictionary<string> |
| 10 | + running: Set<string> |
| 11 | + testStarts: Dictionary<string> |
| 12 | +} |
| 13 | + |
| 14 | +function defaultHandlers({ |
| 15 | + pickles, |
| 16 | + testCases, |
| 17 | + running, |
| 18 | + testStarts, |
| 19 | +}: TestCaseParser): Record<string, Function> { |
| 20 | + return { |
| 21 | + pickle: (p: messages.IPickle) => (pickles[p.id] = p), |
| 22 | + testCase: (t: messages.TestCase) => (testCases[t.id] = t.pickleId), |
| 23 | + testCaseStarted: (t: messages.TestCaseStarted) => { |
| 24 | + running.add(t.testCaseId) |
| 25 | + testStarts[t.id] = t.testCaseId |
| 26 | + }, |
| 27 | + testCaseFinished: (t: messages.TestCaseFinished) => |
| 28 | + running.delete(testStarts[t.testCaseStartedId]), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function parse( |
| 33 | + envelopes: messages.IEnvelope[], |
| 34 | + handlers: Record<string, Function> |
| 35 | +): void { |
| 36 | + _.each(envelopes, (envelope) => |
| 37 | + _.forOwn(envelope, (value, key) => { |
| 38 | + ;(handlers[key] || _.noop)(value) |
| 39 | + }) |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +Then(/^it runs tests in order (.+)$/, function (this: World, order: string) { |
| 44 | + const running = new Set<string>() |
| 45 | + const pickles: Dictionary<messages.IPickle> = {} |
| 46 | + const testCases: Dictionary<string> = {} |
| 47 | + const testStarts: Dictionary<string> = {} |
| 48 | + const scenarioNames = order.split(/,\s*/) |
| 49 | + const handlers = defaultHandlers({ pickles, running, testCases, testStarts }) |
| 50 | + handlers.testCaseStarted = (t: messages.TestCaseStarted) => |
| 51 | + expect(pickles[testCases[t.testCaseId]].name).to.eq(scenarioNames.shift()) |
| 52 | + parse(this.lastRun.envelopes, handlers) |
| 53 | +}) |
| 54 | + |
| 55 | +Then(/^tandem tests verified$/, function (this: World, assertion: string) { |
| 56 | + const running = new Set<string>() |
| 57 | + const pickles: Dictionary<messages.IPickle> = {} |
| 58 | + const testCases: Dictionary<string> = {} |
| 59 | + const testStarts: Dictionary<string> = {} |
| 60 | + const assertFn = ( |
| 61 | + _pickle1: messages.IPickle, |
| 62 | + _pickle2: messages.IPickle |
| 63 | + ): boolean => { |
| 64 | + // eslint-disable-next-line no-eval |
| 65 | + return eval(` |
| 66 | + const { expect } = require('chai') |
| 67 | + ${assertion} |
| 68 | + `) |
| 69 | + } |
| 70 | + |
| 71 | + const handlers = defaultHandlers({ pickles, running, testCases, testStarts }) |
| 72 | + handlers.testCaseStarted = _.wrap( |
| 73 | + handlers.testCaseStarted, |
| 74 | + (fn, t: messages.TestCaseStarted) => { |
| 75 | + running.forEach((tcId) => |
| 76 | + assertFn(pickles[testCases[tcId]], pickles[testCases[t.testCaseId]]) |
| 77 | + ) |
| 78 | + fn(t) |
| 79 | + } |
| 80 | + ) |
| 81 | + parse(this.lastRun.envelopes, handlers) |
| 82 | +}) |
0 commit comments