Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Jest Platform


You can use specific features of Jest as standalone packages by cherry picking them.

Below is a list of the available packages:

jest-changed-files

This is the tool for identifying modified files in a git/hg repository. It exports two functions:

  • getChangedFilesForRoots will return a promise that resolves to an object with the changed files and repos.
  • findRepos will return a promise that resolves to a set of repositories contained in the specified path.

Example

const {getChangedFilesForRoots} = require('jest-changed-files');

// prints the set of modified files since last commit in the current repo
getChangedFilesForRoots(['./'], {
  lastCommit: true,
}).then(result => console.log(result.changedFiles));

jest-diff

The jest-diff is a tool for visualizing changes in data. It exports a function that compares two values of any type and will return a "pretty-printed" string illustrating the difference between the two arguments.

Example

const diff = require('jest-diff');

const a = {a: {b: {c: 5}}};
const b = {a: {b: {c: 6}}};

const result = diff(a, b);
// print diff
console.log(result);

jest-docblock

The jest-docblock is a tool for extracting and parsing the comments at the top of a JavaScript file. It exports various functions to manipulate the data inside the comment block.

Example

const {parseWithComments} = require('jest-docblock');

const code = `
/**
 * This is a sample
 *
 * @flow
 */
 
 console.log('Hello World!');
`;

const parsed = parseWithComments(code);

// will print an object with two attributes: comments and pragmas.
console.log(parsed);

jest-get-type

The jest-get-type is a module that identifies the primitive type of any JavaScript value. It exports a function that returns a string with the type of the value passed as argument.

Example

const getType = require('jest-get-type');

const array = [1, 2, 3];
const nullValue = null;
const undefinedValue = undefined;

// prints 'array'
console.log(getType(array));
// prints 'null'
console.log(getType(nullValue));
// prints 'undefined'
console.log(getType(undefinedValue));

jest-validate

This is a tool for validating configurations submitted by users. It will export a function that takes two arguments: one is the user's configuration and the other is an object containing an example configuration and other options. The return value is an object that has two attributes:

  • hasDeprecationWarnings, a boolean that indicates whether the submitted configuration has deprecation warnings,
  • isValid, a boolean that indicates whether the configuration is correct or not.

Example

const {validate} = require('jest-validate');

const configByUser = {
  transform: '<rootDir>/node_modules/my-custom-transform',
};

const result = validate(configByUser, {
  comment: '  Documentation: https://custom-docs.com',
  exampleConfig: {transform: '<rootDir>/node_modules/babel-jest'},
});

console.log(result);

jest-worker

This is a module used for parallelization of tasks. It exports a class Worker that takes the path of Node.js module and allows you call the module's exported methods as if they were class methods, it will return a promise that resolves when the specified method finishes its execution in a forked process.

Example

// heavy-task.js

```module.exports = {
  myHeavyTask: args => {
    // long running CPU intensive task.
  },
};```
// main.js

```async function main() {
  const worker = new Worker(require.resolve('./heavy-task.js'));

  // run 2 tasks in parallel with different arguments
  const results = await Promise.all([
    worker.myHeavyTask({foo: 'bar'}),
    worker.myHeavyTask({bar: 'foo'}),
  ]);

  console.log(results);
}

main();

pretty-format

This will export a function that converts any JavaScript value into a human-readable string. It supports all built-in JavaScript types out of the box and allows extension for application-specific types via user-defined plugins.

Example

const prettyFormat = require('pretty-format');

const val = {object: {}};
val.circularReference = val;
val[Symbol('foo')] = 'foo';
val.map = new Map([['prop', 'value']]);
val.array = [-0, Infinity, NaN];

console.log(prettyFormat(val));

Previous: Setup and Teardown
Next: Mock Functions