Feature: Testing file reading and matchingScenario: Read file and match content Given the file "file1.txt" existsWhen I read the fileThen the file should contain the following data | data | | foo | | bar |
constfs=require('fs');Given('the file {string} exists',function(filename){constfilePath=`./test-data/${filename}`;if(!fs.existsSync(filePath)){thrownewError(`File '${filePath}' does not exist`);}this.currentFile=filePath;});When('I read the file',function(){constfileContent=fs.readFileSync(this.currentFile,'utf8');if(!fileContent){thrownewError(`File '${this.currentFile}' is empty`);}this.currentFileContent=fileContent;});Then('the file should contain the following data',function(dataTable){constexpectedData=dataTable.raw().slice(1).map(row=>row[0]);constactualData=this.currentFileContent.split('\n').map(line=>line.trim());for(constdataofexpectedData){if(!actualData.includes(data)){thrownewError(`Expected data '${data}' not found in file '${this.currentFile}'`);}}});
Feature: Verify file content using regexScenario Outline: Verify file content Given the file "<filename>" existsWhen I read the fileThen the content should match "<content_regex>"Examples: | filename | content_regex | | file1.txt | Hello, (.*)! | | file2.txt | (.*) World! |
constfs=require('fs');Given('the file {string} exists',function(filename){if(!fs.existsSync(filename)){thrownewError(`File '${filename}' does not exist`);}});When('I read the file',function(){// Nothing to do here, file contents will be read in next step});Then('the content should match {string}',function(content_regex){constfileContent=fs.readFileSync(this.currentFile,'utf-8');constmatches=fileContent.match(newRegExp(content_regex));if(!matches){thrownewError(`File content does not match '${content_regex}'`);}});