@@ -4,42 +4,37 @@ import { join } from 'path';
44import * as Paths from '../../scripts/paths' ;
55import * as fs from 'fs-extra' ;
66
7- const jestDescribe = describe ;
8- const jestIt = it ;
9- const jestExpect = expect ;
10-
117const TEMPLATE_EXCLUDED_ITEMS = [ 'node_modules' , 'package-lock.json' ] ;
128
139type RunWithTemplatesIterator = (
1410 runtTest : ( ) => TestRunResult ,
15- templateName : string ,
11+ context : RunWithTemplateIteratorContext ,
1612) => void ;
17- interface RunWithTemplatesOptions {
18- iterator ?: RunWithTemplatesIterator ;
19- logUnlessStatus ?: number ;
20- }
21- interface WithTemplatesIteratorOptions {
22- describe ?: string | false ;
23- it ?: string ;
24- expect ?: RunWithTemplatesIterator ;
13+
14+ interface RunWithTemplateIteratorContext {
15+ templateName : string ;
16+ describeLabel : string ;
17+ itLabel : string ;
18+ testLabel : string ;
2519}
2620
27- export function withTemplatesIterator ( {
28- describe = 'with template "__TEMPLATE_NAME__"' ,
29- it = 'should run as expected' ,
30- expect = runTest => {
31- jestExpect ( runTest ( ) ) . toMatchSnapshot ( ) ;
32- } ,
33- } : WithTemplatesIteratorOptions = { } ) : RunWithTemplatesIterator {
34- return ( runTest , name ) => {
35- const interpolate = ( msg : string ) => msg . replace ( '__TEMPLATE_NAME__' , name ) ;
36- if ( describe ) {
37- jestDescribe ( interpolate ( describe ) , ( ) => {
38- jestIt ( interpolate ( it ) , ( ) => expect ( runTest , name ) ) ;
39- } ) ;
40- } else {
41- jestIt ( interpolate ( it ) , ( ) => expect ( runTest , name ) ) ;
21+ function createIteratorContext (
22+ templateName : string ,
23+ expectedStatus ?: number ,
24+ ) : RunWithTemplateIteratorContext {
25+ const actionForExpectedStatus = ( status ?: number ) : string => {
26+ if ( status == null ) {
27+ return 'run' ;
4228 }
29+ return status === 0 ? 'pass' : 'fail' ;
30+ } ;
31+ return {
32+ templateName,
33+ describeLabel : `with template "${ templateName } "` ,
34+ itLabel : `should ${ actionForExpectedStatus ( expectedStatus ) } ` ,
35+ testLabel : `should ${ actionForExpectedStatus (
36+ expectedStatus ,
37+ ) } using template "${ templateName } "`,
4338 } ;
4439}
4540
@@ -94,13 +89,15 @@ class TestCaseRunDescriptor {
9489
9590 runWithTemplates < T extends string > (
9691 templates : T [ ] ,
97- { iterator, logUnlessStatus } : RunWithTemplatesOptions = { } ,
92+ expectedStatus ?: number ,
93+ iterator ?: RunWithTemplatesIterator ,
9894 ) : TestRunResultsMap < T > {
9995 if ( templates . length < 1 ) {
10096 throw new RangeError (
10197 `There must be at least one template to run the test case with.` ,
10298 ) ;
10399 }
100+
104101 if ( ! templates . every ( ( t , i ) => templates . indexOf ( t , i + 1 ) === - 1 ) ) {
105102 throw new Error (
106103 `Each template must be unique. Given ${ templates . join ( ', ' ) } ` ,
@@ -113,12 +110,12 @@ class TestCaseRunDescriptor {
113110 template,
114111 } ) ;
115112 const runTest = ( ) => {
116- const out = desc . run ( logUnlessStatus ) ;
113+ const out = desc . run ( expectedStatus ) ;
117114 map [ template ] = { ...out } ;
118115 return out ;
119116 } ;
120117 if ( iterator ) {
121- iterator ( runTest , template ) ;
118+ iterator ( runTest , createIteratorContext ( template , expectedStatus ) ) ;
122119 } else {
123120 runTest ( ) ;
124121 }
@@ -151,7 +148,7 @@ export type TestRunResultsMap<T extends string = string> = {
151148 [ key in T ] : TestRunResult
152149} ;
153150
154- export default function configureTestCase (
151+ export function configureTestCase (
155152 name : string ,
156153 options : RunTestOptions = { } ,
157154) : TestCaseRunDescriptor {
@@ -246,17 +243,19 @@ function stripAnsiColors(stringToStrip: string): string {
246243
247244function prepareTest ( name : string , template : string ) : string {
248245 const sourceDir = join ( Paths . e2eSourceDir , name ) ;
249- // working directory is in the temp directory, different for each tempalte name
246+ // working directory is in the temp directory, different for each template name
250247 const caseDir = join ( Paths . e2eWorkDir , template , name ) ;
251248 const templateDir = join ( Paths . e2eWorkTemplatesDir , template ) ;
252249
253- // ensure directory exists
250+ // recreate the directory
251+ fs . removeSync ( caseDir ) ;
254252 fs . mkdirpSync ( caseDir ) ;
255253
256- // link the node_modules dir if the template has
257254 const tmplModulesDir = join ( templateDir , 'node_modules' ) ;
258255 const caseModulesDir = join ( caseDir , 'node_modules' ) ;
259- if ( ! fs . existsSync ( caseModulesDir ) && fs . existsSync ( tmplModulesDir ) ) {
256+
257+ // link the node_modules dir if the template has one
258+ if ( fs . existsSync ( tmplModulesDir ) ) {
260259 fs . symlinkSync ( tmplModulesDir , caseModulesDir ) ;
261260 }
262261
0 commit comments