dimanche 21 avril 2019

How to check if file name contains a string with Mocha/Chai?

I created an NPM package that contains a collection of Gulp tasks. I added some tests using Mocha + Chai to be sure that it keeps building assets in the proper way.

One of the tests, needs to comprove that the generated compressed file exists and is not empty.

I used chai-fs to check if the file exists.

The issue is that the generated file is hashed so it looks like main-07781b0850.min.css. 07781b0850 is generated by gulp-rev.

Here what I'm doing:

const chai = require('chai');
const expect = require('chai').expect;
const fs = require('chai-fs');
chai.use(fs);

describe('production', function () {
  // Styles.
  it('CSS file is minified.', function(done) {
    expect('test-website/dist/styles/main-07781b0850.min.css').to.be.a.file().and.have.contents.that.match(/\*\/(?!\n).*$/);
    done();
  });
});

Since I know the hash always contains 10 chars/digits, I'd like to test if the generated file match this pattern: main-[a-z0-9]{10}.min.css.

I did not find a way to use regex with chai-fs neither with chai-files.

Aucun commentaire:

Enregistrer un commentaire