rsnext/test/unit/recursive-readdir.test.ts
Wyatt Johnson 0f822373ee
File Reader Improvements (#54645)
This replaces the existing recursive directory reading beheviour with a more efficient implementation. Rather than previously doing actual recursion with the function calls to go deeper into each directory, this has been rewritten to instead use a while loop and a stack. This should improve memory usage for projects with very deep directory structures. 

The updated design that performs directory scanning on all directories found at each layer, allowing more filesystem calls to be sent to the OS instead of waiting like the previous implementation did.

**Currently the new implementation is about 3x faster.**
2023-08-28 18:09:56 +00:00

35 lines
916 B
TypeScript

/* eslint-env jest */
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { join } from 'path'
const resolveDataDir = join(__dirname, 'isolated', '_resolvedata')
const dirWithPages = join(resolveDataDir, 'readdir', 'pages')
describe('recursiveReadDir', () => {
it('should work', async () => {
const result = await recursiveReadDir(dirWithPages, {
pathnameFilter: (f) => /\.js/.test(f),
})
const pages = [
/^[\\/]index\.js/,
/^[\\/]prefered\.js/,
/^[\\/]nav[\\/]about\.js/,
/^[\\/]nav[\\/]index\.js/,
/^[\\/]nested[\\/]index\.js/,
/^[\\/]prefered[\\/]index\.js/,
/^[\\/]nav[\\/]products[\\/]product\.js/,
]
expect(
result.filter((item) => {
for (const page of pages) {
if (page.test(item)) {
return false
}
}
return true
}).length
).toBe(0)
})
})