-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
213 lines (182 loc) · 6.03 KB
/
index.js
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const path = require('path')
const process = require('process')
const nextOnNetlify = require('next-on-netlify')
const pathExists = require('path-exists')
const { dir: getTmpDir } = require('tmp-promise')
const cpy = require('cpy')
const plugin = require('..')
const FIXTURES_DIR = `${__dirname}/fixtures`
const SAMPLE_PROJECT_DIR = `${__dirname}/sample`
const utils = {
run: {
command() {},
},
build: {
failBuild(message) {
throw new Error(message)
},
},
}
// Temporary switch cwd
const changeCwd = function (cwd) {
const originalCwd = process.cwd()
process.chdir(cwd)
return process.chdir.bind(process, originalCwd)
}
// Move .next from sample project to current directory
const moveNextDist = async function () {
await cpy('.next/**', process.cwd(), { cwd: SAMPLE_PROJECT_DIR, parents: true, overwrite: false, dot: true })
}
// Copy fixture files to the current directory
const useFixture = async function (fixtureName) {
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
await cpy('**', process.cwd(), { cwd: fixtureDir, parents: true, overwrite: false, dot: true })
}
// In each test, we change cwd to a temporary directory.
// This allows us not to have to mock filesystem operations.
beforeEach(async () => {
const { path, cleanup } = await getTmpDir({ unsafeCleanup: true })
const restoreCwd = changeCwd(path)
Object.assign(this, { cleanup, restoreCwd })
})
afterEach(async () => {
jest.clearAllMocks()
jest.resetAllMocks()
// Cleans up the temporary directory from `getTmpDir()` and do not make it
// the current directory anymore
this.restoreCwd()
await this.cleanup()
})
const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
const netlifyConfig = { build: {} }
describe('preBuild()', () => {
test('create next.config.js with correct target if file does not exist', async () => {
await plugin.onPreBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
expect(await pathExists('next.config.js')).toBeTruthy()
})
test('do nothing if the app has static html export in npm script', async () => {
await plugin.onPreBuild({
netlifyConfig: { build: { command: 'npm run build' } },
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
expect(await pathExists('next.config.js')).toBeFalsy()
})
test('run plugin if the app has next export in an unused script', async () => {
await plugin.onPreBuild({
netlifyConfig,
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { export: 'next export' } },
utils,
constants: {},
})
expect(await pathExists('next.config.js')).toBeTruthy()
})
test('do nothing if app has static html export in toml/ntl config', async () => {
await plugin.onPreBuild({
netlifyConfig: { build: { command: 'next build && next export' } },
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
expect(await pathExists('next.config.js')).toBeFalsy()
})
test('do nothing if app has next-on-netlify installed', async () => {
const packageJson = {
dependencies: { 'next-on-netlify': '123' },
}
await plugin.onPreBuild({
netlifyConfig,
packageJson,
utils,
})
expect(await pathExists('next.config.js')).toBeFalsy()
})
test('do nothing if app has next-on-netlify postbuild script', async () => {
const packageJson = {
scripts: { postbuild: 'next-on-netlify' },
}
await plugin.onPreBuild({
netlifyConfig,
packageJson,
utils,
})
expect(await pathExists('next.config.js')).toBeFalsy()
})
test('fail build if the app has no package.json', async () => {
await expect(
plugin.onPreBuild({
netlifyConfig,
packageJson: {},
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow(`Could not find a package.json for this project`)
})
})
describe('onBuild()', () => {
test('does not run onBuild if using next-on-netlify', async () => {
const packageJson = {
scripts: { postbuild: 'next-on-netlify' },
}
await useFixture('publish_copy_files')
await moveNextDist()
const PUBLISH_DIR = 'publish'
await plugin.onBuild({
netlifyConfig,
packageJson,
constants: {},
})
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
})
test.each(['invalid_next_config', 'deep_invalid_next_config'])(
`do nothing if the app's next config has an invalid target`,
async (fixtureName) => {
await useFixture(fixtureName)
const PUBLISH_DIR = 'publish'
await plugin.onBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
},
)
test('copy files to the publish directory', async () => {
await useFixture('publish_copy_files')
await moveNextDist()
const PUBLISH_DIR = 'publish'
await plugin.onBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
constants: {
PUBLISH_DIR,
FUNCTIONS_SRC: 'functions',
},
})
expect(await pathExists(`${PUBLISH_DIR}/_redirects`)).toBeTruthy()
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeTruthy()
})
test.each([
{ FUNCTIONS_SRC: 'functions', resolvedFunctions: 'functions' },
{ FUNCTIONS_SRC: undefined, resolvedFunctions: 'netlify-automatic-functions' },
])('copy files to the functions directory', async ({ FUNCTIONS_SRC, resolvedFunctions }) => {
await useFixture('functions_copy_files')
await moveNextDist()
await plugin.onBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
constants: {
FUNCTIONS_SRC,
PUBLISH_DIR: '.',
},
})
expect(await pathExists(`${resolvedFunctions}/next_api_test/next_api_test.js`)).toBeTruthy()
})
})