Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active February 26, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slavafomin/4745b29422925013937c4062871dbef2 to your computer and use it in GitHub Desktop.
Save slavafomin/4745b29422925013937c4062871dbef2 to your computer and use it in GitHub Desktop.
How to run Jest programmatically in node.js (Jest JavaScript API)
import {runCLI} from 'jest-cli';
import ProjectConfig = jest.ProjectConfig;
const projectRootPath = '/path/to/project/root';
// Add any Jest configuration options here
const jestConfig: ProjectConfig = {
roots: ['./dist/tests'],
testRegex: '\\.spec\\.js$'
};
// Run the Jest asynchronously
const result = await runCLI(jestConfig as any, [projectRootPath]);
// Analyze the results
// (see typings for result format)
if (result.results.success) {
console.log(`Tests completed`);
} else {
console.error(`Tests failed`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment