@@ -18,7 +18,14 @@ import (
1818 "github.com/DataDog/ddtest/internal/utils"
1919)
2020
21- const binJestPath = "node_modules/.bin/jest"
21+ const (
22+ binJestPath = "node_modules/.bin/jest"
23+ nodeOptionsEnvVar = "NODE_OPTIONS"
24+ ddTraceCIInitModule = "dd-trace/ci/init"
25+ nodeRequireShortArg = "-r"
26+ nodeRequireLongArg = "--require"
27+ nodeRequireLongArgWith = nodeRequireLongArg + "="
28+ )
2229
2330var ErrFullTestDiscoveryUnsupported = errors .New ("full test discovery is not supported" )
2431
@@ -98,7 +105,7 @@ func (j *Jest) DiscoverTestFiles(ctx context.Context, testFiles discovery.TestFi
98105 }
99106
100107 slog .Info ("Discovering Jest test files with command" , "command" , command , "args" , args )
101- output , err := j .executor .CombinedOutput (ctx , command , args , j .platformEnv )
108+ output , err := j .executor .CombinedOutput (ctx , command , args , j .discoveryEnv () )
102109 if err != nil {
103110 message := strings .TrimSpace (string (output ))
104111 if message == "" {
@@ -124,6 +131,23 @@ func (j *Jest) RunTests(ctx context.Context, testFiles []string, envMap map[stri
124131 return j .executor .Run (ctx , command , args , mergedEnv )
125132}
126133
134+ func (j * Jest ) discoveryEnv () map [string ]string {
135+ envMap := make (map [string ]string , len (j .platformEnv )+ 1 )
136+ maps .Copy (envMap , j .platformEnv )
137+
138+ nodeOptions , ok := envMap [nodeOptionsEnvVar ]
139+ if ! ok {
140+ var found bool
141+ nodeOptions , found = os .LookupEnv (nodeOptionsEnvVar )
142+ if ! found {
143+ return envMap
144+ }
145+ }
146+
147+ envMap [nodeOptionsEnvVar ] = stripNodeOptionsRequire (nodeOptions , ddTraceCIInitModule )
148+ return envMap
149+ }
150+
127151// Decide between user custom command, local jest binary and npx jest
128152func (j * Jest ) getJestCommand () (string , []string ) {
129153 if len (j .commandOverride ) > 0 {
@@ -143,6 +167,33 @@ func jestTestFileExtensionPattern() string {
143167 return "{" + strings .Join (jestTestFileExtensions , "," ) + "}"
144168}
145169
170+ func stripNodeOptionsRequire (nodeOptions string , module string ) string {
171+ fields := strings .Fields (nodeOptions )
172+ stripped := make ([]string , 0 , len (fields ))
173+ for i := 0 ; i < len (fields ); i ++ {
174+ field := fields [i ]
175+
176+ if field == nodeRequireShortArg || field == nodeRequireLongArg {
177+ if i + 1 < len (fields ) && fields [i + 1 ] == module {
178+ i ++
179+ continue
180+ }
181+ }
182+
183+ if strings .HasPrefix (field , nodeRequireShortArg ) && strings .TrimPrefix (field , nodeRequireShortArg ) == module {
184+ continue
185+ }
186+
187+ if strings .HasPrefix (field , nodeRequireLongArgWith ) && strings .TrimPrefix (field , nodeRequireLongArgWith ) == module {
188+ continue
189+ }
190+
191+ stripped = append (stripped , field )
192+ }
193+
194+ return strings .Join (stripped , " " )
195+ }
196+
146197func parseJestListTestsOutput (output []byte ) []string {
147198 cwd , _ := os .Getwd ()
148199 if resolvedCwd , err := filepath .EvalSymlinks (cwd ); err == nil {
0 commit comments