gligoran
gligoran

Reputation: 3338

Use jest to compare content of two files

I'm trying to switch from Mocha and Chai to Jest. In my current setup I'm also using chai-files to compare the contents of two files:

import chai, { expect } from 'chai';
import chaiFiles, { file } from 'chai-files';
import fs from 'fs-extra';
import { exec } from 'child-process-promise';

chai.use(chaiFiles);

describe('cli', () => {
    before(() => {
        process.chdir(__dirname);
    });

    it('should run', async () => {
        // make a copy of entry file
        fs.copySync('./configs/entry/config.version-and-build.xml', './config.xml');

        // executes code that changes temp files
        await exec('../dist/cli.js -v 2.4.9 -b 86');

        // checks if target file and change temp file are equal
        expect(file('./config.xml')).to.equal(file('./configs/expected/config.version-and-build.to.version-and-build.xml'));
    });

    afterEach(() => {
        if (fs.existsSync(tempConfigFile)) {
            fs.removeSync(tempConfigFile);
        }
    });
});

How should this be done in Jest? Will I need to load both files and compare the content?

Upvotes: 6

Views: 3870

Answers (1)

Amit Levy
Amit Levy

Reputation: 1183

Yes, simply load the contents of each like so:

expect(fs.readFileSync(actualPath)).toEqual(fs.readFileSync(expectedPath));

Upvotes: 4

Related Questions