我正在尝试从 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/