Skip to content

Commit

Permalink
test: ensure schematic utils are tested properly
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Nov 20, 2024
1 parent 0bd829a commit a936877
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ module.exports = {
'<rootDir>/**/*.ts',
'!**/testing/**',
'!**/external-utils/**',
'!**/utils/**',
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it, jest } from '@jest/globals'
import {
getAllTypescriptFiles,
GetAllTypescriptFilesOptions,
} from './get-all-typescript-files'
import { Tree } from '@angular-devkit/schematics'

describe('get all typescript files', () => {
const sut = getAllTypescriptFiles

describe('should return Typescript files in the project', () => {
const EXTENSIONS = ['.ts', '.mts']
EXTENSIONS.forEach((extension) => {
it(`like ${extension} files`, () => {
const tree = Tree.empty()
const file = `/index${extension}`
const fileInSubdir = `/subdir/index${extension}`
;[file, fileInSubdir].forEach((filePath) => tree.create(filePath, ''))

const files = getFilePathsFromResult(sut(tree))
expect(files).toEqual([file, fileInSubdir])
})
})
})

describe('should not return Typescript definition files in the project', () => {
const EXTENSIONS = ['.d.ts', '.d.mts']

EXTENSIONS.forEach((extension) => {
it(`like ${extension} files`, () => {
const tree = Tree.empty()
tree.create(`/index${extension}`, '')

const files = getFilePathsFromResult(sut(tree))
expect(files).toHaveLength(0)
})
})
})

it('should not return Typescript files inside node_modules directory', () => {
const tree = Tree.empty()
tree.create(`/node_modules/@angular/core/index.ts`, '')

const files = getFilePathsFromResult(sut(tree))
expect(files).toHaveLength(0)
})

it('should not return Typescript files not matching the content filter when provided', () => {
const contentFilter = jest
.fn<Exclude<GetAllTypescriptFilesOptions['contentFilter'], undefined>>()
.mockReturnValue(false)
const tree = Tree.empty()
const DUMMY_CONTENT = 'DUMMY_CONTENT'
tree.create(`/index.ts`, DUMMY_CONTENT)

const files = getFilePathsFromResult(sut(tree, { contentFilter }))
expect(files).toHaveLength(0)
expect(contentFilter).toHaveBeenCalledWith(DUMMY_CONTENT)
expect(contentFilter).toHaveBeenCalledTimes(1)
})
})

const getFilePathsFromResult = (
iterableIterator: ReturnType<typeof getAllTypescriptFiles>,
): ReadonlyArray<string> => [...iterableIterator].map(([filePath]) => filePath)
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { createSourceFile, ScriptTarget, SourceFile } from 'typescript'
// https://github.com/ngrx/platform/blob/18.1.1/modules/store/schematics-core/utility/visitors.ts
export function* getAllTypescriptFiles(
tree: Tree,
options: {
contentFilter?: (content: string) => boolean
} = {},
options: GetAllTypescriptFilesOptions = {},
): IterableIterator<[string, SourceFile]> {
for (const filePath of getAllTypescriptFilePaths(tree.root)) {
const content = tree.readText(filePath)
Expand All @@ -24,6 +22,9 @@ export function* getAllTypescriptFiles(
yield [filePath, sourceFile]
}
}
export type GetAllTypescriptFilesOptions = Partial<{
contentFilter: (content: string) => boolean
}>

const TYPESCRIPT_FILE_EXTENSIONS = ['.mts', '.ts']
const TYPESCRIPT_DEFINITION_FILE_EXTENSION_PREFIX = '.d'
Expand Down

0 comments on commit a936877

Please sign in to comment.