Want to validate if multiple files with filenames in a specific pattern are created by a function. The test case I have written is
describe('Sitemap', () => {
it('it should create XML sitemap', () => {
let language='hindi'
return sitemapContainer.createSitemap(language).then(() => {
expect(fs.existsSync(`sos/sos_${language}*.xml`)).to.be.true
});
})
})
but it fails. Which function should I use in place of fs.existsSync so that pattern could be matched . Some example files are sos_hindi_1.xml , sos_hindi_2.xml , sos_hindi_3.xml .
existsSync looks for a single file, but you can think out of the box and come with a different approach.
You can instead read the entire folder so that you have a list of files, and then do your manipulations.
expect(fs.readdirSync('sos/').some(file => /* your condition */)).to.be.true
If at least one file matches your condition, it will return true
With a regex, you can do something like that
let lan = "hindi";
var reg = new RegExp(`sos_${lan}_\\S\.xml`, 'g');
expect(fs.readdirSync('sos/').some(file => file.match(reg))).to.be.true