File size: 1,714 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { nextTestSetup } from 'e2e-utils'
import { findAllTelemetryEvents } from 'next-test-utils'

describe('build-lifecycle-hooks', () => {
  const { next } = nextTestSetup({
    files: __dirname,
    env: {
      NEXT_TELEMETRY_DEBUG: '1',
    },
  })

  it('should run runAfterProductionCompile', async () => {
    const output = next.cliOutput

    expect(output).toContain('')
    expect(output).toContain(`Using distDir: ${next.testDir}/.next`)
    expect(output).toContain(`Using projectDir: ${next.testDir}`)
    expect(output).toContain(`Total files in ${next.testDir}/.next folder:`)
    expect(output).toContain('Completed runAfterProductionCompile in')

    // Ensure telemetry event is recorded
    const events = findAllTelemetryEvents(output, 'NEXT_BUILD_FEATURE_USAGE')
    expect(events).toContainEqual({
      featureName: 'runAfterProductionCompile',
      invocationCount: 1,
    })
  })

  it('should allow throwing error in runAfterProductionCompile', async () => {
    try {
      await next.stop()
      await next.patchFile('next.config.ts', (content) => {
        return content.replace(
          `import { after } from './after'`,
          `import { after } from './bad-after'`
        )
      })

      const getCliOutput = next.getCliOutputFromHere()
      await next.build()
      expect(getCliOutput()).toContain('error after production build')
      expect(getCliOutput()).not.toContain(
        'Completed runAfterProductionCompile in'
      )
    } finally {
      await next.patchFile('next.config.ts', (content) => {
        return content.replace(
          `import { after } from './bad-after'`,
          `import { after } from './after'`
        )
      })
    }
  })
})