Skip to content

Commit

Permalink
Add test to ensure fs.readFile() works with Output File Tracing (#3…
Browse files Browse the repository at this point in the history
…5338)

Add test for `fs.readFile()`

- Related to vercel/vercel#7256
- Related to #32236

Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
  • Loading branch information
styfle and ijjk committed Mar 18, 2022
1 parent 7ca958d commit 73b83a0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Expand Up @@ -360,6 +360,9 @@ export class TraceEntryPointsPlugin implements webpack5.WebpackPluginInstance {
fileList,
reasons,
(file) => {
// if a file was imported and a loader handled it
// we don't include it in the trace e.g.
// static image imports, CSS imports
file = nodePath.join(this.tracingRoot, file)
const depMod = depModMap.get(file)
const isAsset = reasons.get(file)?.type.includes('asset')
Expand Down
13 changes: 13 additions & 0 deletions test/integration/production/pages/api/readfile-dirname.js
@@ -0,0 +1,13 @@
import { readFileSync } from 'fs'
import { join } from 'path'

// __dirname is going to be different after build since the file
// is located in .next/server/pages/api instead of the src location
// so this is not currently expected to work
const file = join(__dirname, '../../static/data/item.txt')
const content = readFileSync(file, 'utf8')
console.log({ file, content })

export default (req, res) => {
res.end(content)
}
9 changes: 9 additions & 0 deletions test/integration/production/pages/api/readfile-processcwd.js
@@ -0,0 +1,9 @@
import { readFileSync } from 'fs'
import { join } from 'path'
const file = join(process.cwd(), 'static/data/item.txt')
const content = readFileSync(file, 'utf8')
console.log({ file, content })

export default (req, res) => {
res.end(content)
}
38 changes: 38 additions & 0 deletions test/integration/production/test/index.test.js
Expand Up @@ -234,6 +234,28 @@ describe('Production Usage', () => {
/!/,
],
},
{
page: '/api/readfile-dirname',
tests: [/webpack-api-runtime\.js/, /static\/data\/item\.txt/],
notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
},
{
page: '/api/readfile-processcwd',
tests: [/webpack-api-runtime\.js/, /static\/data\/item\.txt/],
notTests: [
/next\/dist\/server\/next\.js/,
/next\/dist\/bin/,
/\0/,
/\?/,
/!/,
],
},
]

for (const check of checks) {
Expand Down Expand Up @@ -600,6 +622,22 @@ describe('Production Usage', () => {
expect(body).toEqual('API hello works')
})

// Today, `__dirname` usage fails because Next.js moves the source file
// to .next/server/pages/api but it doesn't move the asset file.
// In the future, it would be nice to make `__dirname` work too.
it('does not work with pages/api/readfile-dirname.js', async () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/readfile-dirname`)
expect(res.status).toBe(500)
})

it('should work with pages/api/readfile-processcwd.js', async () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/readfile-processcwd`)
const body = await res.text()
expect(body).toBe('item')
})

it('should work with dynamic params and search string', async () => {
const url = `http://localhost:${appPort}`
const res = await fetchViaHTTP(url, `/api/post-1?val=1`)
Expand Down

0 comments on commit 73b83a0

Please sign in to comment.