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

标签 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);
        }
    });
});

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

最佳答案

是的,只需像这样加载每个内容:

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

关于javascript - 使用 jest 比较两个文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63499624/

相关文章:

javascript - 如何使用 Kinetic js/html5 为弧形设置动画,以便 "snake"变长?

javascript - Typescript - 强制覆盖子类中的静态变量

javascript - VueJS - mustache 语法瞬间出现

javascript - Angular 2 CLI 变量未更新

javascript - 处理来自 JSON 的 POST 数组对象,避免错误 ECONNREFUSED 套接字挂起

reactjs - 开 Jest 错误,需要 Babel “^7.0.0-0” ,但加载了 “6.26.3”

javascript - jest.fn() 模拟函数没有被第二次调用

javascript - 如何在 Typescript 项目中使用 chakramjs?

node.js - 错误 : ‘REPLACE_INVALID_UTF8’ is not a member of ‘v8::String’

typescript - fp-ts 和 Jest : ergonomic tests for Option and Either?