Last active
February 26, 2020 11:01
-
-
Save slavafomin/4745b29422925013937c4062871dbef2 to your computer and use it in GitHub Desktop.
How to run Jest programmatically in node.js (Jest JavaScript API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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