Skip to content

Commit 492899a

Browse files
committed
Remove Jest testMatch discovery override
1 parent 727b035 commit 492899a

2 files changed

Lines changed: 105 additions & 11 deletions

File tree

internal/framework/jest.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ func (j *Jest) DiscoverTestFiles(ctx context.Context, testFiles discovery.TestFi
100100
command, baseArgs := j.getJestCommand()
101101
args := slices.Clone(baseArgs)
102102
args = append(args, "--listTests")
103-
if settings.GetTestsLocation() != "" && testFiles.Pattern != "" {
104-
args = append(args, "--testMatch", testFiles.Pattern)
105-
}
106103

107104
slog.Info("Discovering Jest test files with command", "command", command, "args", args)
108105
output, err := j.executor.CombinedOutput(ctx, command, args, j.discoveryEnv())
@@ -114,7 +111,12 @@ func (j *Jest) DiscoverTestFiles(ctx context.Context, testFiles discovery.TestFi
114111
return nil, fmt.Errorf("failed to discover Jest test files: %s: %w", message, err)
115112
}
116113

117-
return parseJestListTestsOutput(output), nil
114+
discoveredFiles := parseJestListTestsOutput(output)
115+
if settings.GetTestsLocation() == "" && settings.GetTestsExcludePattern() == "" {
116+
return discoveredFiles, nil
117+
}
118+
119+
return filterJestTestFiles(discoveredFiles, testFiles)
118120
}
119121

120122
func (j *Jest) RunTests(ctx context.Context, testFiles []string, envMap map[string]string) error {
@@ -167,6 +169,31 @@ func jestTestFileExtensionPattern() string {
167169
return "{" + strings.Join(jestTestFileExtensions, ",") + "}"
168170
}
169171

172+
func filterJestTestFiles(testFiles []string, selectedTestFiles discovery.TestFileSet) ([]string, error) {
173+
if settings.GetTestsLocation() == "" {
174+
selectedTestFiles.Pattern = ""
175+
}
176+
177+
testFileMatcher, err := discovery.NewTestFileSetMatcher(selectedTestFiles, settings.GetTestsExcludePattern())
178+
if err != nil {
179+
return nil, err
180+
}
181+
182+
filteredFiles := make([]string, 0, len(testFiles))
183+
for _, testFile := range testFiles {
184+
normalizedTestFile := utils.NormalizePath(testFile)
185+
if normalizedTestFile == "" {
186+
continue
187+
}
188+
if testFileMatcher.MatchNormalizedPath(normalizedTestFile) {
189+
filteredFiles = append(filteredFiles, normalizedTestFile)
190+
}
191+
}
192+
193+
slices.Sort(filteredFiles)
194+
return slices.Compact(filteredFiles), nil
195+
}
196+
170197
func stripNodeOptionsRequire(nodeOptions string, module string) string {
171198
fields := strings.Fields(nodeOptions)
172199
stripped := make([]string, 0, len(fields))

internal/framework/jest_test.go

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,20 @@ func TestJest_DiscoverTestFiles_StripsInheritedNodeOptions(t *testing.T) {
204204
}
205205
}
206206

207-
func TestJest_DiscoverTestFiles_WithTestsLocationUsesTestMatch(t *testing.T) {
207+
func TestJest_DiscoverTestFiles_WithTestsLocationFiltersListTestsOutput(t *testing.T) {
208208
tempDir := t.TempDir()
209209
oldWd, _ := os.Getwd()
210210
defer func() { _ = os.Chdir(oldWd) }()
211211
if err := os.Chdir(tempDir); err != nil {
212212
t.Fatalf("failed to chdir: %v", err)
213213
}
214214

215-
for _, file := range []string{"custom/a.check.js", "custom/b.check.js", "src/c.test.js"} {
215+
for _, file := range []string{
216+
"custom/unit/a.check.js",
217+
"custom/unit/b.check.js",
218+
"custom/not-listed.check.js",
219+
"src/c.test.js",
220+
} {
216221
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
217222
t.Fatalf("failed to create dir for %s: %v", file, err)
218223
}
@@ -221,13 +226,14 @@ func TestJest_DiscoverTestFiles_WithTestsLocationUsesTestMatch(t *testing.T) {
221226
}
222227
}
223228

224-
setTestsLocation(t, "custom/*.check.js")
229+
setTestsLocation(t, "./custom/**/*.check.js")
225230

226231
var capturedName string
227232
var capturedArgs []string
228233
mockExecutor := &jestCommandExecutor{
229-
output: []byte(filepath.Join(tempDir, "custom", "b.check.js") + "\n" +
230-
filepath.Join(tempDir, "custom", "a.check.js") + "\n"),
234+
output: []byte(filepath.Join(tempDir, "custom", "unit", "b.check.js") + "\n" +
235+
filepath.Join(tempDir, "src", "c.test.js") + "\n" +
236+
filepath.Join(tempDir, "custom", "unit", "a.check.js") + "\n"),
231237
onExecution: func(name string, args []string) {
232238
capturedName = name
233239
capturedArgs = slices.Clone(args)
@@ -242,12 +248,73 @@ func TestJest_DiscoverTestFiles_WithTestsLocationUsesTestMatch(t *testing.T) {
242248
if capturedName != "npx" {
243249
t.Errorf("expected command %q, got %q", "npx", capturedName)
244250
}
245-
expectedArgs := []string{"jest", "--listTests", "--testMatch", "custom/*.check.js"}
251+
expectedArgs := []string{"jest", "--listTests"}
246252
if !slices.Equal(capturedArgs, expectedArgs) {
247253
t.Errorf("expected args %v, got %v", expectedArgs, capturedArgs)
248254
}
249255

250-
expected := []string{"custom/a.check.js", "custom/b.check.js"}
256+
expected := []string{"custom/unit/a.check.js", "custom/unit/b.check.js"}
257+
if !slices.Equal(files, expected) {
258+
t.Errorf("expected files %v, got %v", expected, files)
259+
}
260+
}
261+
262+
func TestJest_DiscoverTestFiles_WithTestsLocationReturnsInvalidPatternError(t *testing.T) {
263+
setTestsLocation(t, "custom/[")
264+
265+
mockExecutor := &jestCommandExecutor{
266+
output: []byte("custom/a.check.js\n"),
267+
}
268+
jest := &Jest{executor: mockExecutor, platformEnv: make(map[string]string)}
269+
270+
_, err := jest.DiscoverTestFiles(context.Background(), discovery.TestFileSet{Pattern: jest.TestPattern()})
271+
if err == nil {
272+
t.Fatal("expected error")
273+
}
274+
if !strings.Contains(err.Error(), "invalid tests location pattern") {
275+
t.Fatalf("unexpected error: %v", err)
276+
}
277+
}
278+
279+
func TestJest_DiscoverTestFiles_WithTestsExcludePatternFiltersListTestsOutput(t *testing.T) {
280+
tempDir := t.TempDir()
281+
oldWd, _ := os.Getwd()
282+
defer func() { _ = os.Chdir(oldWd) }()
283+
if err := os.Chdir(tempDir); err != nil {
284+
t.Fatalf("failed to chdir: %v", err)
285+
}
286+
287+
for _, file := range []string{"src/a.test.js", "src/system/b.test.js"} {
288+
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
289+
t.Fatalf("failed to create dir for %s: %v", file, err)
290+
}
291+
if err := os.WriteFile(file, []byte("test"), 0644); err != nil {
292+
t.Fatalf("failed to create %s: %v", file, err)
293+
}
294+
}
295+
296+
setTestsExcludePattern(t, "src/system/**/*.test.js")
297+
298+
var capturedArgs []string
299+
mockExecutor := &jestCommandExecutor{
300+
output: []byte(filepath.Join(tempDir, "src", "system", "b.test.js") + "\n" +
301+
filepath.Join(tempDir, "src", "a.test.js") + "\n"),
302+
onExecution: func(name string, args []string) {
303+
capturedArgs = slices.Clone(args)
304+
},
305+
}
306+
jest := &Jest{executor: mockExecutor, platformEnv: make(map[string]string)}
307+
files, err := jest.DiscoverTestFiles(context.Background(), discovery.TestFileSet{Pattern: jest.TestPattern()})
308+
if err != nil {
309+
t.Fatalf("DiscoverTestFiles failed: %v", err)
310+
}
311+
312+
expectedArgs := []string{"jest", "--listTests"}
313+
if !slices.Equal(capturedArgs, expectedArgs) {
314+
t.Errorf("expected args %v, got %v", expectedArgs, capturedArgs)
315+
}
316+
317+
expected := []string{"src/a.test.js"}
251318
if !slices.Equal(files, expected) {
252319
t.Errorf("expected files %v, got %v", expected, files)
253320
}

0 commit comments

Comments
 (0)