Skip to content

Commit

Permalink
Add failing tests for matching outside relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Sep 22, 2023
1 parent a4efd87 commit ae8ee58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/jest-util/src/TestPathPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import type {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';

type PatternsFullConfig = Pick<Config.GlobalConfig, 'testPathPatterns'>;
type PatternsConfig = Pick<Config.GlobalConfig, 'rootDir'>;
type PatternsFullConfig = PatternsConfig &
Pick<Config.GlobalConfig, 'testPathPatterns'>;

export default class TestPathPatterns {
readonly patterns: Array<string>;

private _regexString: string | null = null;

constructor(patterns: Array<string>);
constructor(patterns: Array<string>, config?: PatternsConfig);
constructor(config: PatternsFullConfig);
constructor(patternsOrConfig: Array<string> | PatternsFullConfig) {
let patterns;
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-util/src/__tests__/TestPathPatterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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']);

Expand Down

0 comments on commit ae8ee58

Please sign in to comment.