@amiceli/vitest-cucumber
Version:
vitest tools to use Gherkin feature in unit tests
28 lines (27 loc) • 1.04 kB
JavaScript
import { dirname } from 'node:path';
import callsites from 'callsites';
import { GherkinParser } from '../parser/parser';
import { FeatureFileReader } from '../parser/readfile';
import { getVitestCucumberConfiguration } from './configuration';
function getCallerPath() {
const { 2: callerFilePath } = callsites();
const callerFileName = callerFilePath?.getFileName() || ``;
const callerFileDir = dirname(callerFileName);
return callerFileDir;
}
export function loadFeatureFromText(featureContent, options) {
const parser = new GherkinParser(getVitestCucumberConfiguration(options));
featureContent.split(/\r?\n|\r|\n/g).forEach((line) => {
parser.addLine(line);
});
return parser.finish()[0];
}
export async function loadFeature(featureFilePath, options) {
const callerFileDir = getCallerPath();
const [feature] = await FeatureFileReader.fromPath({
featureFilePath,
callerFileDir,
options: getVitestCucumberConfiguration(options),
}).parseFile();
return feature;
}