How to use gherkin - 10 common examples

To help you get started, we’ve selected a few gherkin examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cucumber / cucumber / fake-cucumber / javascript / src / main.ts View on Github external
const fakeTestResultsStream = new FakeTestResultsStream(program.results)
fakeTestResultsStream.on('error', (err: Error) => exit(err))

let formatStream: Transform
switch (program.format) {
  case 'ndjson':
    formatStream = new ProtobufNdjsonStream()
    break
  case 'protobuf':
    formatStream = new ProtobufBinaryStream(messages.Envelope.encodeDelimited)
    break
  default:
    throw new Error(`Unsupported format: '${program.format}'`)
}

gherkin
  .fromPaths(paths, {})
  .pipe(fakeTestResultsStream)
  .pipe(formatStream)
  .pipe(process.stdout)

function exit(err: Error) {
  // tslint:disable-next-line:no-console
  console.error(err)
  process.exit(1)
}
github cucumber / cucumber-js / lib / cucumber / gherkin_lexer.js View on Github external
function GherkinLexer(content, gherkinListener) {
  // Strip BOM
  content = content.replace(/^\ufeff/g, '');

  var language = 'en';
  var lines = content.split('\n');
  var len = lines.length;
  for (var i = 0; i < len; i++) {
    var line = lines[i];
    if(!/^\s*#/.exec(line)) break;
    var languageMatch = /^\s*#\s*language:\s*([a-zA-Z-_]+)\s*$/m.exec(line);
    language = languageMatch === null ? 'en' : languageMatch[1].toLowerCase();
  }
  language = language.replace('-', '_');
  var Lexer = gherkin.Lexer(language);
  var lexer = new Lexer(gherkinListener);

  this.scan = function () {
    lexer.scan(content);
  };
}
github mklabs / mocha-gherkin / lib / parser.js View on Github external
var fs = require('fs'),
  path = require('path'),
  util = require('util'),
  vm = require('vm'),
  stream = require('stream'),
  Lexer = require('gherkin').Lexer('en'),
  template = require('../template'),
  sandbox = require('./sandbox');

//
// Basic gherkin feature -> mocha bdd test suite
//
//    fs.createReadStream(something).pipe(new Parser).pipe(process.stdout);
//
// Basic gherkin to mocha mapping:
//
// - feature      -> top-level describe()
// - scenario(s)  -> inner describe(), might have one or more
// - step(s)      -> it()
//

module.exports = Parser;
github TradeMe / tractor / server / actions / open-file.js View on Github external
function lexFeature (contents, name) {
    var tokens;
    try {
        var formatter = new Formatter();
        /* eslint-disable new-cap */
        var EnLexer = gherkin.Lexer('en');
        /* eslint-enable new-cap */
        var enLexer = new EnLexer(formatter);
        enLexer.scan(contents);
        tokens = formatter.done();
    } catch (e) {
        throw new LexFeatureError('Lexing "' + name + '" failed.');
    }
    return tokens;
}
github TradeMe / tractor / server / utils / feature-lexer.js View on Github external
function lex (file) {
    try {
        var formatter = new Formatter();
        /* eslint-disable new-cap */
        var EnLexer = gherkin.Lexer('en');
        /* eslint-enable new-cap */
        var enLexer = new EnLexer(formatter);
        enLexer.scan(file.content);
        file.tokens = formatter.done();
        delete file.content;
    } catch (e) {
        throw new LexFeatureError('Lexing "' + file.name + '" failed.');
    }
}
github TradeMe / tractor / packages / tractor / server / files / FeatureFile.js View on Github external
.then(() => {
            let formatter = new FeatureLexerFormatter();
            /* eslint-disable new-cap */
            let EnLexer = gherkin.Lexer('en');
            /* eslint-enable new-cap */
            let enLexer = new EnLexer(formatter);
            enLexer.scan(this.content);
            this.tokens = formatter.done();
        })
        .catch((error) => {
github TradeMe / tractor / server / actions / file-actions / get-file-structure.js View on Github external
featureFiles.forEach(function (file) {
        try {
            var formatter = new Formatter();
            /* eslint-disable new-cap */
            var EnLexer = gherkin.Lexer('en');
            /* eslint-enable new-cap */
            var enLexer = new EnLexer(formatter);
            enLexer.scan(file.content);
            file.tokens = formatter.done();
        } catch (e) {
            throw new LexFeatureError('Lexing "' + file.name + '" failed.');
        }
    });
}
github cucumber / cucumber-electron / renderer.js View on Github external
const electron = require('electron')
const Convert = require('ansi-to-html')
const convert = new Convert()
const Cucumber = require('cucumber')

// HACK:
// window.Gherkin !== require('gherkin') in browsers, so parsing fails unless:
require('gherkin').Parser = window.Gherkin.Parser
require('gherkin').Compiler = window.Gherkin.Compiler
require('gherkin').DIALECTS = window.Gherkin.DIALECTS


const cli = require('./cli')(electron.remote.process.argv)

function log() {
  const args = [].slice.apply(arguments)
  const htmlSafeOutput = args[0].toString().replace(//g, '>')
  const pre = document.createElement('pre')
  pre.innerHTML = convert.toHtml(htmlSafeOutput, { fg: '#000', bg: '#fff' })
  document.body.appendChild(pre)
}

const PrettyFormatter = Cucumber.Listener.PrettyFormatter
Cucumber.Listener.PrettyFormatter = function(options) {
  options.logToFunction = log
  return PrettyFormatter(options)
github cucumber / cucumber-js / src / formatter / helpers / keyword_type.js View on Github external
export function getStepKeywordType({ keyword, language, previousKeywordType }) {
  const dialect = Gherkin.dialects()[language]
  const type = _.find(['given', 'when', 'then', 'and', 'but'], key =>
    _.includes(dialect[key], keyword)
  )
  switch (type) {
    case 'when':
      return types.EVENT
    case 'then':
      return types.OUTCOME
    case 'and':
    case 'but':
      if (previousKeywordType) {
        return previousKeywordType
      }
    // fallthrough
    default:
      return types.PRECONDITION
github cucumber / cucumber / fake-cucumber / javascript / src / cli.ts View on Github external
for (const key of Object.keys(supportCode)) {
      // @ts-ignore
      global[key] = supportCode[key]
    }
  }
  await loadSupportCode()
  const cucumberStream = new CucumberStream(
    supportCode.parameterTypes,
    supportCode.stepDefinitions,
    supportCode.beforeHooks,
    supportCode.afterHooks,
    supportCode.newId,
    supportCode.clock
  )
  await pipelinePromise(
    gherkin.fromPaths(paths, options),
    cucumberStream,
    formatStream(program.format),
    process.stdout
  )
}