|
1 | 1 | /* eslint-env jest */ |
2 | | -import os from 'os' |
3 | 2 | import { join } from 'path' |
4 | 3 | import loadConfig from 'next/dist/server/config' |
5 | 4 | import { PHASE_DEVELOPMENT_SERVER } from 'next/constants' |
6 | 5 |
|
7 | 6 | const pathToConfig = join(__dirname, '_resolvedata', 'without-function') |
8 | 7 | const pathToConfigFn = join(__dirname, '_resolvedata', 'with-function') |
9 | 8 |
|
10 | | -describe('config', () => { |
11 | | - if (os.platform() === 'linux') { |
12 | | - it('Should get the configuration', async () => { |
13 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig) |
14 | | - expect(config.customConfig).toBe(true) |
15 | | - }) |
16 | | - |
17 | | - it('Should pass the phase correctly', async () => { |
18 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
19 | | - expect(config.phase).toBe(PHASE_DEVELOPMENT_SERVER) |
20 | | - }) |
21 | | - |
22 | | - it('Should pass the defaultConfig correctly', async () => { |
23 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
24 | | - expect(config.defaultConfig).toBeDefined() |
25 | | - }) |
26 | | - |
27 | | - it('Should assign object defaults deeply to user config', async () => { |
28 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
29 | | - expect(config.distDir).toEqual('.next') |
30 | | - expect(config.onDemandEntries.maxInactiveAge).toBeDefined() |
31 | | - }) |
32 | | - |
33 | | - it('Should pass the customConfig correctly', async () => { |
34 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
35 | | - customConfig: true, |
36 | | - }) |
37 | | - expect(config.customConfig).toBe(true) |
38 | | - }) |
| 9 | +// force require usage instead of dynamic import in jest |
| 10 | +// x-ref: https://github.com/nodejs/node/issues/35889 |
| 11 | +process.env.__NEXT_TEST_MODE = 'jest' |
39 | 12 |
|
40 | | - it('Should not pass the customConfig when it is null', async () => { |
41 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, null) |
42 | | - expect(config.webpack).toBe(null) |
43 | | - }) |
44 | | - |
45 | | - it('Should assign object defaults deeply to customConfig', async () => { |
46 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
47 | | - customConfig: true, |
48 | | - onDemandEntries: { custom: true }, |
49 | | - }) |
50 | | - expect(config.customConfig).toBe(true) |
51 | | - expect(config.onDemandEntries.maxInactiveAge).toBeDefined() |
52 | | - }) |
53 | | - |
54 | | - it('Should allow setting objects which do not have defaults', async () => { |
55 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
56 | | - bogusSetting: { custom: true }, |
57 | | - }) |
58 | | - expect(config.bogusSetting).toBeDefined() |
59 | | - expect(config.bogusSetting.custom).toBe(true) |
60 | | - }) |
61 | | - |
62 | | - it('Should override defaults for arrays from user arrays', async () => { |
63 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
64 | | - pageExtensions: ['.bogus'], |
65 | | - }) |
66 | | - expect(config.pageExtensions).toEqual(['.bogus']) |
67 | | - }) |
68 | | - |
69 | | - it('Should throw when an invalid target is provided', async () => { |
70 | | - await expect(async () => { |
| 13 | +describe('config', () => { |
| 14 | + it('Should get the configuration', async () => { |
| 15 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig) |
| 16 | + expect(config.customConfig).toBe(true) |
| 17 | + }) |
| 18 | + |
| 19 | + it('Should pass the phase correctly', async () => { |
| 20 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
| 21 | + expect(config.phase).toBe(PHASE_DEVELOPMENT_SERVER) |
| 22 | + }) |
| 23 | + |
| 24 | + it('Should pass the defaultConfig correctly', async () => { |
| 25 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
| 26 | + expect(config.defaultConfig).toBeDefined() |
| 27 | + }) |
| 28 | + |
| 29 | + it('Should assign object defaults deeply to user config', async () => { |
| 30 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn) |
| 31 | + expect(config.distDir).toEqual('.next') |
| 32 | + expect(config.onDemandEntries.maxInactiveAge).toBeDefined() |
| 33 | + }) |
| 34 | + |
| 35 | + it('Should pass the customConfig correctly', async () => { |
| 36 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 37 | + customConfig: true, |
| 38 | + }) |
| 39 | + expect(config.customConfig).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + it('Should not pass the customConfig when it is null', async () => { |
| 43 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, null) |
| 44 | + expect(config.webpack).toBe(null) |
| 45 | + }) |
| 46 | + |
| 47 | + it('Should assign object defaults deeply to customConfig', async () => { |
| 48 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 49 | + customConfig: true, |
| 50 | + onDemandEntries: { custom: true }, |
| 51 | + }) |
| 52 | + expect(config.customConfig).toBe(true) |
| 53 | + expect(config.onDemandEntries.maxInactiveAge).toBeDefined() |
| 54 | + }) |
| 55 | + |
| 56 | + it('Should allow setting objects which do not have defaults', async () => { |
| 57 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 58 | + bogusSetting: { custom: true }, |
| 59 | + }) |
| 60 | + expect(config.bogusSetting).toBeDefined() |
| 61 | + expect(config.bogusSetting.custom).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it('Should override defaults for arrays from user arrays', async () => { |
| 65 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 66 | + pageExtensions: ['.bogus'], |
| 67 | + }) |
| 68 | + expect(config.pageExtensions).toEqual(['.bogus']) |
| 69 | + }) |
| 70 | + |
| 71 | + it('Should throw when an invalid target is provided', async () => { |
| 72 | + await expect(async () => { |
| 73 | + await loadConfig( |
| 74 | + PHASE_DEVELOPMENT_SERVER, |
| 75 | + join(__dirname, '_resolvedata', 'invalid-target') |
| 76 | + ) |
| 77 | + }).rejects.toThrow(/Specified target is invalid/) |
| 78 | + }) |
| 79 | + |
| 80 | + it('Should pass when a valid target is provided', async () => { |
| 81 | + const config = await loadConfig( |
| 82 | + PHASE_DEVELOPMENT_SERVER, |
| 83 | + join(__dirname, '_resolvedata', 'valid-target') |
| 84 | + ) |
| 85 | + expect(config.target).toBe('serverless') |
| 86 | + }) |
| 87 | + |
| 88 | + it('Should throw an error when next.config.js is not present', async () => { |
| 89 | + await expect( |
| 90 | + async () => |
71 | 91 | await loadConfig( |
72 | 92 | PHASE_DEVELOPMENT_SERVER, |
73 | | - join(__dirname, '_resolvedata', 'invalid-target') |
| 93 | + join(__dirname, '_resolvedata', 'typescript-config') |
74 | 94 | ) |
75 | | - }).rejects.toThrow(/Specified target is invalid/) |
76 | | - }) |
77 | | - |
78 | | - it('Should pass when a valid target is provided', async () => { |
79 | | - const config = await loadConfig( |
80 | | - PHASE_DEVELOPMENT_SERVER, |
81 | | - join(__dirname, '_resolvedata', 'valid-target') |
82 | | - ) |
83 | | - expect(config.target).toBe('serverless') |
84 | | - }) |
85 | | - |
86 | | - it('Should throw an error when next.config.js is not present', async () => { |
87 | | - await expect( |
88 | | - async () => |
89 | | - await loadConfig( |
90 | | - PHASE_DEVELOPMENT_SERVER, |
91 | | - join(__dirname, '_resolvedata', 'typescript-config') |
92 | | - ) |
93 | | - ).rejects.toThrow( |
94 | | - /Configuring Next.js via .+ is not supported. Please replace the file with 'next.config.js'/ |
95 | | - ) |
96 | | - }) |
97 | | - |
98 | | - it('Should not throw an error when two versions of next.config.js are present', async () => { |
99 | | - const config = await loadConfig( |
100 | | - PHASE_DEVELOPMENT_SERVER, |
101 | | - join(__dirname, '_resolvedata', 'js-ts-config') |
102 | | - ) |
103 | | - expect(config.__test__ext).toBe('js') |
104 | | - }) |
105 | | - |
106 | | - it('Should ignore configs set to `undefined`', async () => { |
107 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
108 | | - target: undefined, |
109 | | - }) |
110 | | - expect(config.target).toBe('server') |
111 | | - }) |
112 | | - |
113 | | - it('Should ignore configs set to `null`', async () => { |
114 | | - const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
115 | | - target: null, |
116 | | - }) |
117 | | - expect(config.target).toBe('server') |
118 | | - }) |
119 | | - } else { |
120 | | - // TODO: remove this when segfault no longer occurs with dynamic |
121 | | - // import inside of jest due to vm usage |
122 | | - it('should skip on non-linux platforms', () => { |
123 | | - console.warn( |
124 | | - 'Warning!! These tests can not currently run on non-linux systems' |
125 | | - ) |
126 | | - }) |
127 | | - } |
| 95 | + ).rejects.toThrow( |
| 96 | + /Configuring Next.js via .+ is not supported. Please replace the file with 'next.config.js'/ |
| 97 | + ) |
| 98 | + }) |
| 99 | + |
| 100 | + it('Should not throw an error when two versions of next.config.js are present', async () => { |
| 101 | + const config = await loadConfig( |
| 102 | + PHASE_DEVELOPMENT_SERVER, |
| 103 | + join(__dirname, '_resolvedata', 'js-ts-config') |
| 104 | + ) |
| 105 | + expect(config.__test__ext).toBe('js') |
| 106 | + }) |
| 107 | + |
| 108 | + it('Should ignore configs set to `undefined`', async () => { |
| 109 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 110 | + target: undefined, |
| 111 | + }) |
| 112 | + expect(config.target).toBe('server') |
| 113 | + }) |
| 114 | + |
| 115 | + it('Should ignore configs set to `null`', async () => { |
| 116 | + const config = await loadConfig(PHASE_DEVELOPMENT_SERVER, null, { |
| 117 | + target: null, |
| 118 | + }) |
| 119 | + expect(config.target).toBe('server') |
| 120 | + }) |
128 | 121 | }) |
0 commit comments