77import * as path from 'path' ;
88import * as fs from 'fs' ;
99import { execCmd , TestSession } from '@salesforce/cli-plugins-testkit' ;
10- import { expect } from 'chai' ;
10+ import { expect , config } from 'chai' ;
1111import { TestRunIdResult } from '@salesforce/apex-node/lib/src/tests/types' ;
1212import { RunResult } from '../../../../src/reporters' ;
1313
14+ config . truncateThreshold = 0 ;
15+
1416describe ( 'apex run test' , ( ) => {
1517 let session : TestSession ;
1618 before ( async ( ) => {
@@ -54,31 +56,38 @@ describe('apex run test', () => {
5456 } ) ;
5557 } ) ;
5658
57- it ( 'will run default tests and default async' , async ( ) => {
58- const result = execCmd ( 'apex:run:test' , { ensureExitCode : 0 } ) . shellOutput . stdout ;
59- expect ( result ) . to . include ( 'Run "sf apex get test -i 707' ) ;
60- expect ( result ) . to . include ( '-o' ) ;
61- expect ( result ) . to . include ( '" to retrieve test results' ) ;
62- } ) ;
63-
64- it ( 'will run default tests and default async --json' , async ( ) => {
65- const result = execCmd < TestRunIdResult > ( 'apex:run:test' , { ensureExitCode : 0 } ) . jsonOutput ?. result ;
66- expect ( result ?. testRunId ) . to . be . a ( 'string' ) ;
67- expect ( result ?. testRunId . startsWith ( '707' ) ) . to . be . true ;
68- } ) ;
69-
7059 describe ( '--code-coverage' , ( ) => {
7160 it ( 'will run default tests sync with --code-coverage' , async ( ) => {
7261 const result = execCmd ( 'apex:run:test --wait 10 --code-coverage' , { ensureExitCode : 0 } ) . shellOutput . stdout ;
7362 expect ( result ) . to . include ( '=== Apex Code Coverage by Class' ) ;
74- expect ( result ) . to . include ( / C L A S S E S \w + P E R C E N T \w + U N C O V E R E D L I N E S / ) ;
63+ expect ( result ) . to . match ( / C L A S S E S \s + P E R C E N T \s + U N C O V E R E D L I N E S / ) ;
7564 expect ( result ) . to . include ( 'SampleDataController 100%' ) ;
7665 } ) ;
7766 it ( 'will run default tests sync with --code-coverage --json' , async ( ) => {
7867 const result = execCmd < RunResult > ( 'apex:run:test --wait 10 --code-coverage --json' , { ensureExitCode : 0 } )
7968 . jsonOutput ?. result ;
80- expect ( result ?. summary ) . to . have . all . keys ( 'totalLines' , 'coveredLines' , 'orgWideCoverage' , 'testRunCoverage' ) ;
81- expect ( result ?. coverage ?. coverage ) . to . have . all . keys (
69+ expect ( result ?. summary ) . to . have . all . keys (
70+ 'outcome' ,
71+ 'testsRan' ,
72+ 'passing' ,
73+ 'failing' ,
74+ 'skipped' ,
75+ 'passRate' ,
76+ 'failRate' ,
77+ 'testStartTime' ,
78+ 'testExecutionTime' ,
79+ 'testTotalTime' ,
80+ 'commandTime' ,
81+ 'hostname' ,
82+ 'orgId' ,
83+ 'username' ,
84+ 'testRunId' ,
85+ 'userId' ,
86+ 'orgWideCoverage' ,
87+ 'testRunCoverage'
88+ ) ;
89+
90+ expect ( result ?. coverage ?. coverage [ 0 ] ) . to . have . all . keys (
8291 'id' ,
8392 'name' ,
8493 'lines' ,
@@ -91,7 +100,7 @@ describe('apex run test', () => {
91100 const result = execCmd ( 'apex:run:test --wait 10 --code-coverage --detailed-coverage' , { ensureExitCode : 0 } )
92101 . shellOutput . stdout ;
93102 expect ( result ) . to . include ( '=== Apex Code Coverage for Test Run 707' ) ;
94- expect ( result ) . to . include ( / T E S T N A M E \w + C L A S S B E I N G T E S T E D \w + O U T C O M E \w + P E R C E N T \w + M E S S A G E \w + R U N T I M E ( M S ) / ) ;
103+ expect ( result ) . to . match ( / T E S T N A M E \s + C L A S S B E I N G T E S T E D \s + O U T C O M E \s + P E R C E N T \s + M E S S A G E \s + R U N T I M E \ (M S \ )/ ) ;
95104 } ) ;
96105 } ) ;
97106
@@ -101,7 +110,7 @@ describe('apex run test', () => {
101110 expect ( result ) . to . include ( 'Test result files written to testresults' ) ;
102111 const outputDir = path . join ( session . project . dir , 'testresults' ) ;
103112 expect ( fs . statSync ( outputDir ) . isDirectory ( ) ) . to . be . true ;
104- expect ( fs . readdirSync ( outputDir ) ) . length . to . equal ( 6 ) ;
113+ expect ( fs . readdirSync ( outputDir ) . length ) . to . equal ( 6 ) ;
105114 expect ( fs . existsSync ( path . join ( outputDir , 'test-result-codecoverage.json' ) ) ) . to . be . true ;
106115 expect ( fs . existsSync ( path . join ( outputDir , 'test-result.txt' ) ) ) . to . be . true ;
107116 expect ( fs . existsSync ( path . join ( outputDir , 'test-run-id.txt' ) ) ) . to . be . true ;
@@ -116,7 +125,7 @@ describe('apex run test', () => {
116125
117126 it ( 'will run default tests and wait --json' , async ( ) => {
118127 const result = execCmd < RunResult > ( 'apex:run:test --wait 10 --json' , { ensureExitCode : 0 } ) . jsonOutput ?. result ;
119- expect ( result ?. tests ) . length . to . equal ( 11 ) ;
128+ expect ( result ?. tests . length ) . to . equal ( 11 ) ;
120129 expect ( result ?. summary . outcome ) . to . equal ( 'Passed' ) ;
121130 expect ( result ?. summary . testsRan ) . to . equal ( 11 ) ;
122131 expect ( result ?. summary ) . to . have . all . keys (
@@ -154,38 +163,58 @@ describe('apex run test', () => {
154163 it ( 'will run specified classes --class-names' , async ( ) => {
155164 const result = execCmd ( 'apex:run:test -w 10 --class-names TestPropertyController' , { ensureExitCode : 0 } )
156165 . shellOutput . stdout ;
157- expect ( result ) . to . include ( / T e s t s R a n \w + 3 / ) ;
166+ expect ( result ) . to . match ( / T e s t s R a n \s + 3 / ) ;
158167
159168 const result1 = execCmd ( 'apex:run:test -w 10 --class-names TestPropertyController -n GeocodingServiceTest' , {
160169 ensureExitCode : 0 ,
161170 } ) . shellOutput . stdout ;
162- expect ( result1 ) . to . include ( / T e s t s R a n \w + 6 / ) ;
171+ expect ( result1 ) . to . match ( / T e s t s R a n \s + 6 / ) ;
163172
164173 const result2 = execCmd (
165174 'apex:run:test -w 10 --class-names TestPropertyController -n GeocodingServiceTest,FileUtilitiesTest' ,
166175 {
167176 ensureExitCode : 0 ,
168177 }
169178 ) . shellOutput . stdout ;
170- expect ( result2 ) . to . include ( / T e s t s R a n \w + 1 0 / ) ;
179+ expect ( result2 ) . to . match ( / T e s t s R a n \s + 1 0 / ) ;
171180 } ) ;
172181
173182 it ( 'will run specified tests --tests' , async ( ) => {
174183 const result = execCmd ( 'apex:run:test -w 10 --tests TestPropertyController' , { ensureExitCode : 0 } ) . shellOutput
175184 . stdout ;
176- expect ( result ) . to . include ( / T e s t s R a n \w + 3 / ) ;
185+ expect ( result ) . to . match ( / T e s t s R a n \s + 3 / ) ;
177186
178187 const result1 = execCmd ( 'apex:run:test -w 10 --tests TestPropertyController -t GeocodingServiceTest' , {
179188 ensureExitCode : 0 ,
180189 } ) . shellOutput . stdout ;
181- expect ( result1 ) . to . include ( / T e s t s R a n \w + 6 / ) ;
190+ expect ( result1 ) . to . match ( / T e s t s R a n \s + 6 / ) ;
182191
183192 const result2 = execCmd (
184193 'apex:run:test -w 10 --tests TestPropertyController -t GeocodingServiceTest,FileUtilitiesTest' ,
185194 {
186195 ensureExitCode : 0 ,
187196 }
188197 ) . shellOutput . stdout ;
189- expect ( result2 ) . to . include ( / T e s t s R a n \w + 1 0 / ) ;
198+ expect ( result2 ) . to . match ( / T e s t s R a n \s + 1 0 / ) ;
199+ } ) ;
200+
201+ it ( 'will run default tests and default async --json' , async ( ) => {
202+ const result = execCmd < TestRunIdResult > ( 'apex:run:test --json' , { ensureExitCode : 0 } ) . jsonOutput ?. result ;
203+ expect ( result ?. testRunId ) . to . be . a ( 'string' ) ;
204+ expect ( result ?. testRunId . startsWith ( '707' ) ) . to . be . true ;
205+ // get the test results to make sure it's not 'ALREADY IN PROGRESS' or 'QUEUED' for the next test
206+ execCmd ( `apex:get:test -i ${ result ?. testRunId } ` , { ensureExitCode : 0 } ) ;
207+ } ) ;
208+
209+ it ( 'will run default tests and default async' , async ( ) => {
210+ const result = execCmd ( 'apex:run:test' , { ensureExitCode : 0 } ) . shellOutput . stdout ;
211+ expect ( result ) . to . include ( 'apex get test -i 707' ) ;
212+ expect ( result ) . to . include ( '-o' ) ;
213+ expect ( result ) . to . include ( '" to retrieve test results' ) ;
214+ // .match returns RegExpArray | undefined, and typing ?[0] makes TS think it's a ternary, not an undefined array accessor
215+ // and we can't use ?.at yet
216+ const id = result . match ( / 7 0 7 [ \d \w ] + \s / ) ?. find ( ( i ) => i ) ;
217+ // get the test results to make sure it's not 'ALREADY IN PROGRESS' or 'QUEUED' for the next test
218+ execCmd ( `apex:get:test -i ${ id } ` , { ensureExitCode : 0 } ) ;
190219 } ) ;
191220} ) ;
0 commit comments