File size: 2,222 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
import { mkdtemp, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { findConfig } from './find-config'

// Jest does not support `import('file://something')` (file: imports) yet.
describe('findConfig()', () => {
  const exampleConfig = {
    basePath: '/docs',
  }
  const configCode = {
    mjs: `
      const config = ${JSON.stringify(exampleConfig)}

      export default config;
    `,
    cjs: `
      const config = ${JSON.stringify(exampleConfig)}

      module.exports = config;
    `,
  }
  type TestPatterns = {
    pkgConfigTypes: ('module' | 'commonjs')[]
    exts: ('js' | 'mjs' | 'cjs')[]
  }
  const testPatterns: TestPatterns = {
    pkgConfigTypes: ['module', 'commonjs'],
    exts: ['js', 'mjs', 'cjs'],
  }

  for (const pkgConfigType of testPatterns.pkgConfigTypes) {
    for (const ext of testPatterns.exts) {
      it(`should load config properly from *.config.* file (type: "${pkgConfigType}", config: awsome.config.${ext})`, async () => {
        // Create fixtures
        const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))

        await writeFile(
          join(tmpDir, 'package.json'),
          JSON.stringify({
            name: 'nextjs-test',
            type: pkgConfigType,
          })
        )

        let configCodeType = ext
        if (configCodeType === 'js') {
          configCodeType = pkgConfigType === 'module' ? 'mjs' : 'cjs'
        }
        await writeFile(
          join(tmpDir, `awsome.config.${ext}`),
          configCode[configCodeType]
        )

        // Test
        const actualConfig = await findConfig(tmpDir, 'awsome')
        expect(actualConfig).toStrictEqual(exampleConfig)
      })
    }
  }

  it(`should load config properly from the config in package.json)`, async () => {
    // Create fixtures
    const tmpDir = await mkdtemp(join(tmpdir(), 'nextjs-test-'))

    await writeFile(
      join(tmpDir, 'package.json'),
      JSON.stringify({
        name: 'nextjs-test',
        awsome: {
          basePath: '/docs',
        },
      })
    )

    // Test
    const actualConfig = await findConfig(tmpDir, 'awsome')
    expect(actualConfig).toStrictEqual(exampleConfig)
  })
})