What is jest-changed-files?
The jest-changed-files npm package is a utility that allows you to determine which files have changed in a git or hg (Mercurial) repository. It is often used in testing environments, particularly with Jest, to identify which tests need to be run based on the changes made to the codebase.
What are jest-changed-files's main functionalities?
Getting changed files from a Git repository
This feature allows you to get a list of files that have changed since the last commit in a Git repository. The 'lastCommit' option specifies that only changes from the last commit should be considered.
const { getChangedFilesForRoots } = require('jest-changed-files');
(async () => {
const changedFiles = await getChangedFilesForRoots(['./'], { lastCommit: true });
console.log(changedFiles.changedFiles);
})();
Getting changed files from a Mercurial repository
This feature is similar to the previous one but is used for Mercurial repositories. The 'withAncestor' option can be used to find changes that have occurred since the last common ancestor of the current head and the default branch.
const { getChangedFilesForRoots } = require('jest-changed-files');
(async () => {
const changedFiles = await getChangedFilesForRoots(['./'], { withAncestor: true });
console.log(changedFiles.changedFiles);
})();
Other packages similar to jest-changed-files
git-diff-apply
The git-diff-apply package provides functionality to apply the changes between two commits, branches, or tags. It is similar to jest-changed-files in that it deals with changes in a repository, but it focuses on applying changes rather than just listing them.
node-diff3
node-diff3 is a package for three-way file merging. It compares the differences between files, which can be related to the functionality of jest-changed-files, but it goes further by attempting to merge these differences automatically.
simple-git
simple-git is a light-weight interface for running git commands in any node.js application. It can be used to get a list of changed files, similar to jest-changed-files, but it also provides a full range of commands to manage Git repositories programmatically.
jest-changed-files
A module used internally by Jest to check which files have changed since you
last committed in git or hg.
Install
$ npm install --save jest-changed-files
API
hg.isHGRepository(cwd: string): Promise<?string>
Get the root of the mercurial repository containing cwd
or return null
if
cwd
is not inside a mercurial repository.
git.isGitRepository(cwd: string): Promise<?string>
Get the root of the git repository containing cwd
or return null
if
cwd
is not inside a git repository.
hg.findChangedFiles / git.findChangedFiles (root: string): Promise<Array<string>>
Get the list of files in a git/mecurial repository that have changed since the
last commit.
Usage
import {git, hg} from 'jest-changed-files';
function changedFiles(cwd) {
return Promise.all([
git.isGitRepository(cwd),
hg.isHGRepository(cwd),
]).then(([gitRoot, hgRoot]) => {
if (gitRoot !== null) {
return git.findChangedFiles(gitRoot);
} else if (hgRoot !== null) {
return hg.findChangedFiles(hgRoot);
} else {
throw new Error('Not in a git or hg repo');
}
});
}