From ae8ee58f4b8bc3bfc9568595d51861c526606116 Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Tue, 25 Apr 2023 19:02:03 -0700 Subject: [PATCH] Add failing tests for matching outside relative path --- packages/jest-util/src/TestPathPatterns.ts | 6 ++++-- .../src/__tests__/TestPathPatterns.test.ts | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/jest-util/src/TestPathPatterns.ts b/packages/jest-util/src/TestPathPatterns.ts index 2770b8728b8d..587c13c24901 100644 --- a/packages/jest-util/src/TestPathPatterns.ts +++ b/packages/jest-util/src/TestPathPatterns.ts @@ -8,14 +8,16 @@ import type {Config} from '@jest/types'; import {replacePathSepForRegex} from 'jest-regex-util'; -type PatternsFullConfig = Pick; +type PatternsConfig = Pick; +type PatternsFullConfig = PatternsConfig & + Pick; export default class TestPathPatterns { readonly patterns: Array; private _regexString: string | null = null; - constructor(patterns: Array); + constructor(patterns: Array, config?: PatternsConfig); constructor(config: PatternsFullConfig); constructor(patternsOrConfig: Array | PatternsFullConfig) { let patterns; diff --git a/packages/jest-util/src/__tests__/TestPathPatterns.test.ts b/packages/jest-util/src/__tests__/TestPathPatterns.test.ts index b5be21943629..0ecdb932393e 100644 --- a/packages/jest-util/src/__tests__/TestPathPatterns.test.ts +++ b/packages/jest-util/src/__tests__/TestPathPatterns.test.ts @@ -77,6 +77,11 @@ describe('TestPathPatterns', () => { expect(testPathPatterns.isMatch('/a/b/c/d')).toBe(true); }); + it('returns true for explicit relative path', () => { + const testPathPatterns = new TestPathPatterns(['./b/c']); + expect(testPathPatterns.isMatch('/a/b/c')).toBe(true); + }); + it('returns true for partial file match', () => { const testPathPatterns = new TestPathPatterns(['aaa']); expect(testPathPatterns.isMatch('/foo/..aaa..')).toBe(true); @@ -102,6 +107,21 @@ describe('TestPathPatterns', () => { expect(testPathPatterns.isMatch('/foo/bc')).toBe(false); }); + it('returns true only if matches relative path', () => { + const testPathPatterns = new TestPathPatterns(['home'], { + rootDir: '/home/myuser/', + }); + expect(testPathPatterns.isMatch('/home/myuser/LoginPage.js')).toBe(false); + expect(testPathPatterns.isMatch('/home/myuser/HomePage.js')).toBe(true); + }); + + it('matches absolute paths regardless of rootDir', () => { + const testPathPatterns = new TestPathPatterns(['/a/b'], { + rootDir: '/foo/bar', + }); + expect(testPathPatterns.isMatch('/a/b')).toBe(true); + }); + it('returns true if match any paths', () => { const testPathPatterns = new TestPathPatterns(['a/b', 'c/d']);