diff --git a/src/compiler/sys/typescript/tests/typescript-config.spec.ts b/src/compiler/sys/typescript/tests/typescript-config.spec.ts new file mode 100644 index 00000000000..495a9ff77fe --- /dev/null +++ b/src/compiler/sys/typescript/tests/typescript-config.spec.ts @@ -0,0 +1,35 @@ +import { hasSrcDirectoryInclude } from '../typescript-config'; + +describe('typescript-config', () => { + describe('hasSrcDirectoryInclude', () => { + it('returns `false` for a non-array argument', () => { + // the intent of this test is to evaluate when a user doesn't provide an array, hence the type assertion + expect(hasSrcDirectoryInclude('src' as unknown as string[], 'src')).toBe(false); + }); + + it('returns `false` for an empty array', () => { + expect(hasSrcDirectoryInclude([], 'src/')).toBe(false); + }); + + it('returns `false` when an entry does not exist in the array', () => { + expect(hasSrcDirectoryInclude(['src'], 'source')).toBe(false); + }); + + it('returns `true` when an entry does exist in the array', () => { + expect(hasSrcDirectoryInclude(['src', 'foo'], 'src')).toBe(true); + }); + + it('returns `true` for globs', () => { + expect(hasSrcDirectoryInclude(['src/**/*.ts', 'foo/'], 'src/**/*.ts')).toBe(true); + }); + + it.each([ + [['src'], './src'], + [['./src'], 'src'], + [['../src'], '../src'], + [['*'], './*'], + ])('returns `true` for relative paths', (includedPaths, srcDir) => { + expect(hasSrcDirectoryInclude(includedPaths, srcDir)).toBe(true); + }); + }); +}); diff --git a/src/compiler/sys/typescript/typescript-config.ts b/src/compiler/sys/typescript/typescript-config.ts index b170a87f136..211adfc9d67 100644 --- a/src/compiler/sys/typescript/typescript-config.ts +++ b/src/compiler/sys/typescript/typescript-config.ts @@ -166,8 +166,21 @@ const createDefaultTsConfig = (config: d.Config) => 2, ); -const hasSrcDirectoryInclude = (includeProp: string[], src: string) => - Array.isArray(includeProp) && includeProp.includes(src); +/** + * Determines if the included `src` argument belongs in `includeProp`. + * + * This function normalizes the paths found in both arguments, to catch cases where it's called with: + * ```ts + * hasSrcDirectoryInclude(['src'], './src'); // should return `true` + * ``` + * + * @param includeProp the paths in `include` that should be tested + * @param src the path to find in `includeProp` + * @returns true if the provided `src` directory is found, `false` otherwise + */ +export const hasSrcDirectoryInclude = (includeProp: string[], src: string): boolean => + Array.isArray(includeProp) && + includeProp.some((included) => normalizePath(included, false) === normalizePath(src, false)); const hasStencilConfigInclude = (includeProp: string[]) => Array.isArray(includeProp) && includeProp.includes('stencil.config.ts');