File size: 4,509 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import path from 'path'
import fs from 'fs-extra'
import stripAnsi from 'strip-ansi'
import resolveFrom from 'resolve-from'
import { NextInstance, createNext } from 'e2e-utils'

type File = {
  filename: string
  content: string
}

const appDirFiles: File[] = [
  {
    filename: 'app/page.js',
    content: `
    export default function Page() {
      return <p>hello world</p>
    }
  `,
  },
  {
    filename: 'app/layout.js',
    content: `
    export default function Layout({ children }) {
      return (
        <html>
          <head />
          <body>{children}</body>
        </html>
      )
    }
  `,
  },
]
const pagesFiles: File[] = [
  {
    filename: 'pages/another.js',
    content: `
    export default function Page() {
      return (
        <p>another page</p>
      )
    }
  `,
  },
]

let next: NextInstance

describe('build-spinners', () => {
  beforeAll(async () => {
    next = await createNext({
      skipStart: true,
      files: {},
      dependencies: {
        'node-pty': '0.10.1',
      },
      packageJson: {
        pnpm: {
          onlyBuiltDependencies: ['node-pty'],
        },
      },
    })
  })

  afterAll(() => next.destroy())

  beforeEach(async () => {
    await fs.remove(path.join(next.testDir, 'pages'))
    await fs.remove(path.join(next.testDir, 'app'))
  })

  it.each([
    { name: 'app dir - basic', files: appDirFiles },
    {
      name: 'app dir - (compile workers)',
      files: [
        ...appDirFiles,
        {
          filename: 'next.config.js',
          content: `
        module.exports = {
          experimental: {
            webpackBuildWorker: true,
          }
        }
      `,
        },
      ],
    },
    {
      name: 'page dir',
      files: [
        ...pagesFiles,
        {
          filename: 'next.config.js',
          content: `
        module.exports = {
          experimental: {
            webpackBuildWorker: true,
          }
        }
      `,
        },
      ],
    },
    { name: 'page dir (compile workers)', files: pagesFiles },
    { name: 'app and pages', files: [...appDirFiles, ...pagesFiles] },
  ])('should handle build spinners correctly $name', async ({ files }) => {
    for (const { filename, content } of files) {
      await next.patchFile(filename, content)
    }

    const appDir = next.testDir

    const ptyPath = resolveFrom(appDir, 'node-pty')
    const pty = require(ptyPath)
    const output = []
    const ptyProcess = pty.spawn('pnpm', ['next', 'build'], {
      name: 'xterm-color',
      cols: 80,
      rows: 30,
      cwd: appDir,
      env: process.env,
    })

    ptyProcess.onData(function (data) {
      stripAnsi(data)
        .split('\n')
        .forEach((line) => output.push(line))
      process.stdout.write(data)
    })

    await new Promise<void>((resolve, reject) => {
      ptyProcess.onExit(({ exitCode, signal }) => {
        if (exitCode) {
          return reject(`failed with code ${exitCode}`)
        }
        resolve()
      })
    })

    let compiledIdx = -1
    let optimizedBuildIdx = -1
    let collectingPageDataIdx = -1
    let generatingStaticIdx = -1
    let finalizingOptimization = -1

    // order matters so we check output from end to start
    for (let i = output.length - 1; i--; i >= 0) {
      const line = output[i]

      if (compiledIdx === -1 && line.includes('Compiled successfully')) {
        compiledIdx = i
      }

      if (
        optimizedBuildIdx === -1 &&
        line.includes('Creating an optimized production build')
      ) {
        optimizedBuildIdx = i
      }

      if (
        collectingPageDataIdx === -1 &&
        line.includes('Collecting page data')
      ) {
        collectingPageDataIdx = i
      }

      if (
        generatingStaticIdx === -1 &&
        line.includes('Generating static pages')
      ) {
        generatingStaticIdx = i
      }

      if (
        finalizingOptimization === -1 &&
        line.includes('Finalizing page optimization')
      ) {
        finalizingOptimization = i
      }
    }

    expect(compiledIdx).not.toBe(-1)
    expect(optimizedBuildIdx).not.toBe(-1)
    expect(collectingPageDataIdx).not.toBe(-1)
    expect(generatingStaticIdx).not.toBe(-1)
    expect(finalizingOptimization).not.toBe(-1)

    expect(optimizedBuildIdx).toBeLessThan(compiledIdx)
    expect(compiledIdx).toBeLessThan(collectingPageDataIdx)
    expect(collectingPageDataIdx).toBeLessThan(generatingStaticIdx)
    expect(generatingStaticIdx).toBeLessThan(finalizingOptimization)
  })
})