Skip to content

Commit 727b035

Browse files
committed
Jest test file discovery is now performed uninstrumented
1 parent bea5e28 commit 727b035

2 files changed

Lines changed: 93 additions & 5 deletions

File tree

internal/framework/jest.go

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2330
var 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
128152
func (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+
146197
func parseJestListTestsOutput(output []byte) []string {
147198
cwd, _ := os.Getwd()
148199
if resolvedCwd, err := filepath.EvalSymlinks(cwd); err == nil {

internal/framework/jest_test.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestJest_DiscoverTestFiles_UsesLocalJestListTests(t *testing.T) {
143143
}
144144
jest := &Jest{
145145
executor: mockExecutor,
146-
platformEnv: map[string]string{"NODE_OPTIONS": "-r dd-trace/ci/init"},
146+
platformEnv: map[string]string{"NODE_OPTIONS": "-r dd-trace/ci/init --max-old-space-size=4096", "CUSTOM_ENV": "value"},
147147
}
148148
files, err := jest.DiscoverTestFiles(context.Background(), discovery.TestFileSet{Pattern: jest.TestPattern()})
149149
if err != nil {
@@ -157,8 +157,11 @@ func TestJest_DiscoverTestFiles_UsesLocalJestListTests(t *testing.T) {
157157
if !slices.Equal(capturedArgs, expectedArgs) {
158158
t.Errorf("expected args %v, got %v", expectedArgs, capturedArgs)
159159
}
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"])
160+
if mockExecutor.capturedEnvMap["NODE_OPTIONS"] != "--max-old-space-size=4096" {
161+
t.Errorf("expected NODE_OPTIONS without dd-trace init, got %q", mockExecutor.capturedEnvMap["NODE_OPTIONS"])
162+
}
163+
if mockExecutor.capturedEnvMap["CUSTOM_ENV"] != "value" {
164+
t.Errorf("expected CUSTOM_ENV from platform env, got %q", mockExecutor.capturedEnvMap["CUSTOM_ENV"])
162165
}
163166

164167
expectedFiles := []string{"src/b.test.ts", "src/foo.test.js"}
@@ -167,6 +170,40 @@ func TestJest_DiscoverTestFiles_UsesLocalJestListTests(t *testing.T) {
167170
}
168171
}
169172

173+
func TestJest_DiscoverTestFiles_StripsInheritedNodeOptions(t *testing.T) {
174+
t.Setenv("NODE_OPTIONS", "--require dd-trace/ci/init --max-old-space-size=4096")
175+
176+
tempDir := t.TempDir()
177+
oldWd, _ := os.Getwd()
178+
defer func() { _ = os.Chdir(oldWd) }()
179+
if err := os.Chdir(tempDir); err != nil {
180+
t.Fatalf("failed to chdir: %v", err)
181+
}
182+
if err := os.MkdirAll("src", 0755); err != nil {
183+
t.Fatalf("failed to create src dir: %v", err)
184+
}
185+
if err := os.WriteFile(filepath.Join("src", "a.test.js"), []byte("test"), 0644); err != nil {
186+
t.Fatalf("failed to create test file: %v", err)
187+
}
188+
189+
mockExecutor := &jestCommandExecutor{
190+
output: []byte(filepath.Join(tempDir, "src", "a.test.js") + "\n"),
191+
}
192+
jest := &Jest{executor: mockExecutor, platformEnv: make(map[string]string)}
193+
194+
files, err := jest.DiscoverTestFiles(context.Background(), discovery.TestFileSet{Pattern: jest.TestPattern()})
195+
if err != nil {
196+
t.Fatalf("DiscoverTestFiles failed: %v", err)
197+
}
198+
199+
if mockExecutor.capturedEnvMap["NODE_OPTIONS"] != "--max-old-space-size=4096" {
200+
t.Errorf("expected inherited NODE_OPTIONS without dd-trace init, got %q", mockExecutor.capturedEnvMap["NODE_OPTIONS"])
201+
}
202+
if !slices.Equal(files, []string{"src/a.test.js"}) {
203+
t.Errorf("expected discovered file, got %v", files)
204+
}
205+
}
206+
170207
func TestJest_DiscoverTestFiles_WithTestsLocationUsesTestMatch(t *testing.T) {
171208
tempDir := t.TempDir()
172209
oldWd, _ := os.Getwd()

0 commit comments

Comments
 (0)