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');

describe('production', function () {
  // Styles.
  it('CSS file is minified.', function(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.

1 Answers

Luka Kralj On

If you have a name of the generated file then you can test if it matches the pattern with a RegExp.test() method:

const regex = /main-[a-z0-9]{10}.min.css/;

In this case the test will succeed if you enter a filename as main-07781b0850.min.css as well as if you enter a full path like test-website/dist/styles/main-07781b0850.min.css.

If you want to verify the contents of the file against a pattern you can load them into a string and use the same approach.