1
1
import tl = require( 'azure-pipelines-task-lib/task' ) ;
2
2
import utils = require( '../utils' ) ;
3
3
import constants = require( '../constants' ) ;
4
+ import { executeJestCommand } from '../testLibExecutor' ;
4
5
6
+ //Jest command like: >set JEST_JUNIT_OUTPUT_NAME=TEST-Jest0-junit.xml
7
+ //>npx jest --ci --reporters=default --reporters=jest-junit -t "JestTestName"
5
8
export async function executeJestTests ( testsToBeExecuted : string [ ] ) : Promise < number > {
6
9
7
- // jest execution will be added
8
- /*executable = npm
9
- args = test
10
- */
10
+ //Jest-Junit Link:https://github.com/jest-community/jest-junit
11
+ let finalStatus = 0 ;
12
+ let npmPath = tl . which ( "npm" , true ) ;
13
+ try {
14
+ await executeJestCommand ( npmPath , constants . INSTALL_JESTJUNIT ) ;
15
+ } catch ( error ) {
16
+ tl . error ( `Error installing Jest-Junit: ${ error } ` ) ;
17
+ return 1 ;
18
+ }
19
+ //testToBeExecuted: <TestSuiteName1> <TestCase1>. <TestSuiteName1> <TestCase1>,<TestSuiteName2> <TestCase3>. <TestSuiteName2> <TestCase3>
20
+ let npxPath = tl . which ( "npx" , true ) ;
21
+ let i = 0 ;
22
+ for ( let tests of testsToBeExecuted ) {
23
+ const JestTestName = utils . separateJestTestName ( tests ) ;
24
+ try {
25
+ let junitFileName : string = `TEST-Jest${ i } -junit.xml` ;
26
+ try {
27
+ tl . setVariable ( 'JEST_JUNIT_OUTPUT_NAME' , junitFileName ) ;
28
+ let junitName = tl . getVariable ( 'JEST_JUNIT_OUTPUT_NAME' ) ;
29
+ if ( junitName !== junitFileName ) {
30
+ throw new Error ( `Retrieved JEST_JUNIT_OUTPUT_NAME (${ junitName } ) does not match the set value (${ junitFileName } )` ) ;
31
+ }
32
+ tl . debug ( `Junit Filename ${ junitName } environment set and retrieved successfully.` ) ;
33
+ } catch ( error ) {
34
+ tl . error ( `Error setting or getting JEST_JUNIT_OUTPUT_NAME variable: ${ error } ` ) ;
35
+ finalStatus = 1 ;
36
+ continue ;
37
+ }
11
38
12
- console . log ( "jest changes1" ) ;
13
- return 1 ;
39
+ const jestCommand = `jest --ci --reporters=default --reporters=jest-junit -t "${ JestTestName } "` ;
40
+ const status = await executeJestCommand ( npxPath , jestCommand ) ;
41
+ if ( status != 0 ) {
42
+ finalStatus = 1 ;
43
+ }
44
+ tl . debug ( `Test case ${ JestTestName } executed successfully.` ) ;
45
+ } catch ( error ) {
46
+ tl . error ( `Error executing ${ JestTestName } test case: ${ error } ` ) ;
47
+ finalStatus = 1 ;
48
+ }
49
+ i ++ ;
50
+ }
51
+ return finalStatus ;
14
52
}
0 commit comments