使用 jest 比较两个文件的内容

gli*_*ran 6 javascript node.js jestjs

我正在尝试从 Mocha 和 Chai 切换到 Jest。在我当前的设置中,我还用于chai-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);
        }
    });
});

Run Code Online (Sandbox Code Playgroud)

在 Jest 中应该如何完成此操作?我需要加载两个文件并比较内容吗?

Ami*_*evy 3

是的,只需加载每个内容,如下所示:

expect(fs.readFileSync(actualPath)).toEqual(fs.readFileSync(expectedPath));
Run Code Online (Sandbox Code Playgroud)