File size: 3,692 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
/* eslint-env jest */
import path from 'path'
import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import {
  findPort,
  initNextServerScript,
  killApp,
  renderViaHTTP,
  shouldRunTurboDevTest,
} from 'next-test-utils'

describe('pnpm support', () => {
  let next: NextInstance | undefined

  afterEach(async () => {
    try {
      await next?.destroy()
    } catch (_) {}
  })

  it('should build with dependencies installed via pnpm', async () => {
    next = await createNext({
      files: {
        pages: new FileRef(path.join(__dirname, 'app/pages')),
        'next.config.js': new FileRef(
          path.join(__dirname, 'app/next.config.js')
        ),
      },
      packageJson: {
        scripts: {
          build: 'next build',
          start: 'next start',
        },
      },
      buildCommand: 'pnpm run build',
    })

    expect(await next.readFile('pnpm-lock.yaml')).toBeTruthy()

    expect(next.cliOutput).toMatch(/Compiled successfully/)

    const html = await renderViaHTTP(next.url, '/')
    expect(html).toContain('Hello World')

    const manifest = JSON.parse(
      await next.readFile('.next/next-server.js.nft.json')
    )
    for (const ignore of ['next/dist/pages', '.wasm', 'compiled/@ampproject']) {
      let matchingFile
      try {
        matchingFile = manifest.files.some((file) => file.includes(ignore))
        expect(!!matchingFile).toBe(false)
      } catch (err) {
        require('console').error(
          `Found unexpected file in manifest ${matchingFile} matched ${ignore}`
        )
        throw err
      }
    }
  })

  it('should execute client-side JS on each page in output: "standalone"', async () => {
    next = await createNext({
      files: {
        pages: new FileRef(path.join(__dirname, 'app-multi-page/pages')),
        '.npmrc': new FileRef(path.join(__dirname, 'app-multi-page/.npmrc')),
        'next.config.js': new FileRef(
          path.join(__dirname, 'app-multi-page/next.config.js')
        ),
      },
      packageJson: {
        scripts: {
          dev: `next ${shouldRunTurboDevTest() ? 'dev --turbo' : 'dev'}`,
          build: 'next build',
          start: 'next start',
        },
      },
      buildCommand: 'pnpm run build',
    })
    await next.stop()
    expect(next.cliOutput).toMatch(/Compiled successfully/)

    let appPort
    let server
    let browser
    try {
      appPort = await findPort()
      const standaloneDir = path.join(
        next.testDir,
        '.next/standalone/',
        path.basename(next.testDir)
      )

      // simulate what happens in a Dockerfile
      await fs.remove(path.join(next.testDir, 'node_modules'))
      await fs.copy(
        path.join(next.testDir, './.next/static'),
        path.join(standaloneDir, './.next/static'),
        { overwrite: true }
      )
      server = await initNextServerScript(
        path.join(standaloneDir, 'server.js'),
        /- Local:/,
        {
          ...process.env,
          PORT: appPort,
        },
        undefined,
        {
          cwd: standaloneDir,
        }
      )

      await renderViaHTTP(appPort, '/')

      browser = await webdriver(appPort, '/', {
        waitHydration: false,
      })
      expect(await browser.waitForElementByCss('#world').text()).toBe('World')
      await browser.close()

      browser = await webdriver(appPort, '/about', {
        waitHydration: false,
      })
      expect(await browser.waitForElementByCss('#world').text()).toBe('World')
      await browser.close()
    } finally {
      if (server) {
        await killApp(server)
      }
    }
  })
})