@@ -6,11 +6,35 @@ import (
66 "os"
77 "path/filepath"
88 "slices"
9+ "strings"
910 "testing"
1011
1112 "github.com/DataDog/ddtest/internal/discovery"
1213)
1314
15+ type jestCommandExecutor struct {
16+ output []byte
17+ err error
18+ onExecution func (name string , args []string )
19+ capturedEnvMap map [string ]string
20+ }
21+
22+ func (m * jestCommandExecutor ) CombinedOutput (ctx context.Context , name string , args []string , envMap map [string ]string ) ([]byte , error ) {
23+ m .capturedEnvMap = envMap
24+ if m .onExecution != nil {
25+ m .onExecution (name , args )
26+ }
27+ return m .output , m .err
28+ }
29+
30+ func (m * jestCommandExecutor ) Run (ctx context.Context , name string , args []string , envMap map [string ]string ) error {
31+ m .capturedEnvMap = envMap
32+ if m .onExecution != nil {
33+ m .onExecution (name , args )
34+ }
35+ return m .err
36+ }
37+
1438func TestNewJest (t * testing.T ) {
1539 jest := NewJest ()
1640 if jest == nil {
@@ -79,23 +103,23 @@ func TestJest_HasUnskippableMarker(t *testing.T) {
79103 }
80104}
81105
82- func TestJest_DiscoverTestFiles_DefaultPatterns (t * testing.T ) {
106+ func TestJest_DiscoverTestFiles_UsesLocalJestListTests (t * testing.T ) {
83107 tempDir := t .TempDir ()
84108 oldWd , _ := os .Getwd ()
85109 defer func () { _ = os .Chdir (oldWd ) }()
86110 if err := os .Chdir (tempDir ); err != nil {
87111 t .Fatalf ("failed to chdir: %v" , err )
88112 }
113+ if err := os .MkdirAll (filepath .Dir (binJestPath ), 0755 ); err != nil {
114+ t .Fatalf ("failed to create jest bin dir: %v" , err )
115+ }
116+ if err := os .WriteFile (binJestPath , []byte ("#!/usr/bin/env node\n " ), 0755 ); err != nil {
117+ t .Fatalf ("failed to create jest bin: %v" , err )
118+ }
89119
90120 filesToCreate := []string {
121+ "src/b.test.ts" ,
91122 "src/foo.test.js" ,
92- "src/foo.spec.ts" ,
93- "src/__tests__/bar.jsx" ,
94- "src/not-test.js" ,
95- "node_modules/pkg/bad.test.js" ,
96- "dist/bad.spec.js" ,
97- "coverage/bad.test.ts" ,
98- ".next/bad.test.tsx" ,
99123 }
100124 for _ , file := range filesToCreate {
101125 if err := os .MkdirAll (filepath .Dir (file ), 0755 ); err != nil {
@@ -106,26 +130,44 @@ func TestJest_DiscoverTestFiles_DefaultPatterns(t *testing.T) {
106130 }
107131 }
108132
109- jest := NewJest ()
110- files , err := discovery .DiscoverTestFiles (jest .TestPattern (), "" )
133+ var capturedName string
134+ var capturedArgs []string
135+ mockExecutor := & jestCommandExecutor {
136+ output : []byte (filepath .Join (tempDir , "src" , "b.test.ts" ) + "\n " +
137+ filepath .Join (tempDir , "src" , "foo.test.js" ) + "\n " +
138+ "warning: ignored because it is not a file\n " ),
139+ onExecution : func (name string , args []string ) {
140+ capturedName = name
141+ capturedArgs = slices .Clone (args )
142+ },
143+ }
144+ jest := & Jest {
145+ executor : mockExecutor ,
146+ platformEnv : map [string ]string {"NODE_OPTIONS" : "-r dd-trace/ci/init" },
147+ }
148+ files , err := jest .DiscoverTestFiles (context .Background (), discovery.TestFileSet {Pattern : jest .TestPattern ()})
111149 if err != nil {
112- t .Fatalf ("generic discovery failed: %v" , err )
150+ t .Fatalf ("DiscoverTestFiles failed: %v" , err )
113151 }
114152
115- expected := []string {
116- ".next/bad.test.tsx" ,
117- "coverage/bad.test.ts" ,
118- "dist/bad.spec.js" ,
119- "src/__tests__/bar.jsx" ,
120- "src/foo.spec.ts" ,
121- "src/foo.test.js" ,
153+ if capturedName != binJestPath {
154+ t .Errorf ("expected command %q, got %q" , binJestPath , capturedName )
122155 }
123- if ! slices .Equal (files , expected ) {
124- t .Errorf ("expected files %v, got %v" , expected , files )
156+ expectedArgs := []string {"--listTests" }
157+ if ! slices .Equal (capturedArgs , expectedArgs ) {
158+ t .Errorf ("expected args %v, got %v" , expectedArgs , capturedArgs )
159+ }
160+ if mockExecutor .capturedEnvMap ["NODE_OPTIONS" ] != "-r dd-trace/ci/init" {
161+ t .Errorf ("expected NODE_OPTIONS from platform env, got %q" , mockExecutor .capturedEnvMap ["NODE_OPTIONS" ])
162+ }
163+
164+ expectedFiles := []string {"src/b.test.ts" , "src/foo.test.js" }
165+ if ! slices .Equal (files , expectedFiles ) {
166+ t .Errorf ("expected files %v, got %v" , expectedFiles , files )
125167 }
126168}
127169
128- func TestJest_DiscoverTestFiles_WithTestsLocation (t * testing.T ) {
170+ func TestJest_DiscoverTestFiles_WithTestsLocationUsesTestMatch (t * testing.T ) {
129171 tempDir := t .TempDir ()
130172 oldWd , _ := os .Getwd ()
131173 defer func () { _ = os .Chdir (oldWd ) }()
@@ -144,10 +186,28 @@ func TestJest_DiscoverTestFiles_WithTestsLocation(t *testing.T) {
144186
145187 setTestsLocation (t , "custom/*.check.js" )
146188
147- jest := NewJest ()
148- files , err := discovery .DiscoverTestFiles (jest .TestPattern (), "" )
189+ var capturedName string
190+ var capturedArgs []string
191+ mockExecutor := & jestCommandExecutor {
192+ output : []byte (filepath .Join (tempDir , "custom" , "b.check.js" ) + "\n " +
193+ filepath .Join (tempDir , "custom" , "a.check.js" ) + "\n " ),
194+ onExecution : func (name string , args []string ) {
195+ capturedName = name
196+ capturedArgs = slices .Clone (args )
197+ },
198+ }
199+ jest := & Jest {executor : mockExecutor , platformEnv : make (map [string ]string )}
200+ files , err := jest .DiscoverTestFiles (context .Background (), discovery.TestFileSet {Pattern : jest .TestPattern ()})
149201 if err != nil {
150- t .Fatalf ("generic discovery failed: %v" , err )
202+ t .Fatalf ("DiscoverTestFiles failed: %v" , err )
203+ }
204+
205+ if capturedName != "npx" {
206+ t .Errorf ("expected command %q, got %q" , "npx" , capturedName )
207+ }
208+ expectedArgs := []string {"jest" , "--listTests" , "--testMatch" , "custom/*.check.js" }
209+ if ! slices .Equal (capturedArgs , expectedArgs ) {
210+ t .Errorf ("expected args %v, got %v" , expectedArgs , capturedArgs )
151211 }
152212
153213 expected := []string {"custom/a.check.js" , "custom/b.check.js" }
@@ -156,6 +216,65 @@ func TestJest_DiscoverTestFiles_WithTestsLocation(t *testing.T) {
156216 }
157217}
158218
219+ func TestJest_DiscoverTestFiles_WithOverride (t * testing.T ) {
220+ tempDir := t .TempDir ()
221+ oldWd , _ := os .Getwd ()
222+ defer func () { _ = os .Chdir (oldWd ) }()
223+ if err := os .Chdir (tempDir ); err != nil {
224+ t .Fatalf ("failed to chdir: %v" , err )
225+ }
226+ if err := os .MkdirAll ("src" , 0755 ); err != nil {
227+ t .Fatalf ("failed to create src dir: %v" , err )
228+ }
229+ if err := os .WriteFile (filepath .Join ("src" , "a.test.js" ), []byte ("test" ), 0644 ); err != nil {
230+ t .Fatalf ("failed to create test file: %v" , err )
231+ }
232+
233+ var capturedName string
234+ var capturedArgs []string
235+ mockExecutor := & jestCommandExecutor {
236+ output : []byte (filepath .Join (tempDir , "src" , "a.test.js" ) + "\n " ),
237+ onExecution : func (name string , args []string ) {
238+ capturedName = name
239+ capturedArgs = slices .Clone (args )
240+ },
241+ }
242+ jest := & Jest {
243+ executor : mockExecutor ,
244+ commandOverride : []string {"pnpm" , "jest" , "--runInBand" },
245+ platformEnv : make (map [string ]string ),
246+ }
247+
248+ if _ , err := jest .DiscoverTestFiles (context .Background (), discovery.TestFileSet {Pattern : jest .TestPattern ()}); err != nil {
249+ t .Fatalf ("DiscoverTestFiles failed: %v" , err )
250+ }
251+
252+ if capturedName != "pnpm" {
253+ t .Errorf ("expected command %q, got %q" , "pnpm" , capturedName )
254+ }
255+ expectedArgs := []string {"jest" , "--runInBand" , "--listTests" }
256+ if ! slices .Equal (capturedArgs , expectedArgs ) {
257+ t .Errorf ("expected args %v, got %v" , expectedArgs , capturedArgs )
258+ }
259+ }
260+
261+ func TestJest_DiscoverTestFiles_CommandError (t * testing.T ) {
262+ mockExecutor := & jestCommandExecutor {
263+ output : []byte ("invalid jest config" ),
264+ err : errors .New ("exit status 1" ),
265+ }
266+ jest := & Jest {executor : mockExecutor , platformEnv : make (map [string ]string )}
267+
268+ _ , err := jest .DiscoverTestFiles (context .Background (), discovery.TestFileSet {Pattern : jest .TestPattern ()})
269+ if err == nil {
270+ t .Fatal ("expected error" )
271+ }
272+ if ! strings .Contains (err .Error (), "failed to discover Jest test files" ) ||
273+ ! strings .Contains (err .Error (), "invalid jest config" ) {
274+ t .Fatalf ("unexpected error: %v" , err )
275+ }
276+ }
277+
159278func TestJest_RunTests_UsesLocalJestBinary (t * testing.T ) {
160279 tempDir := t .TempDir ()
161280 oldWd , _ := os .Getwd ()
0 commit comments