-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
196 lines (170 loc) · 5.55 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
const fs = require('fs')
const path = require('path')
const process = require('process')
const { promisify } = require('util')
const { copy } = require('cpx')
const nextOnNetlify = require('next-on-netlify')
const pathExists = require('path-exists')
const { dir: getTmpDir } = require('tmp-promise')
const plugin = require('..')
const pCopy = promisify(copy)
const FIXTURES_DIR = `${__dirname}/fixtures`
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)
}
// Copy fixture files to the current directory
const useFixture = async function (fixtureName) {
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
await pCopy(`${fixtureDir}/**`, process.cwd())
}
// 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()
})
jest.mock('next-on-netlify')
const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
describe('preBuild()', () => {
test('fail build if the app has static html export in npm script', async () => {
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
})
test('fail build if the app has static html export in toml/ntl config', async () => {
await expect(
plugin.onPreBuild({
netlifyConfig: { build: { command: 'next build && next export' } },
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
})
test('fail build if app has next-on-netlify installed', async () => {
const packageJson = {
dependencies: { 'next-on-netlify': '123' },
}
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson,
utils,
constants: {},
}),
).rejects.toThrow(
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
)
})
test('fail build if app has next-on-netlify postbuild script', async () => {
const packageJson = {
scripts: { postbuild: 'next-on-netlify' },
}
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson,
utils,
constants: {},
}),
).rejects.toThrow(
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
)
})
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`)
})
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.each(['invalid_next_config', 'deep_invalid_next_config'])(
`fail build if the app's next config has an invalid target`,
async (fixtureName) => {
await useFixture(fixtureName)
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`)
},
)
})
describe('onBuild()', () => {
test('runs NoN with functions_src & publish_dir options', async () => {
const PUBLISH_DIR = 'some/path'
const FUNCTIONS_SRC = 'other/path'
await plugin.onBuild({
constants: {
PUBLISH_DIR,
FUNCTIONS_SRC,
},
})
const nextOnNetlifyOptions = nextOnNetlify.mock.calls[0][0]
expect(nextOnNetlifyOptions.functionsDir).toEqual(FUNCTIONS_SRC)
expect(nextOnNetlifyOptions.publishDir).toEqual(PUBLISH_DIR)
})
test('runs NoN with publish_dir option only', async () => {
const defaultFunctionsSrc = 'netlify-automatic-functions'
const PUBLISH_DIR = 'some/path'
await plugin.onBuild({
constants: {
PUBLISH_DIR,
},
})
const nextOnNetlifyOptions = nextOnNetlify.mock.calls[0][0]
expect(nextOnNetlifyOptions.functionsDir).toEqual(defaultFunctionsSrc)
expect(nextOnNetlifyOptions.publishDir).toEqual(PUBLISH_DIR)
})
test('calls makeDir with correct path', async () => {
const PUBLISH_DIR = 'some/path'
await plugin.onBuild({
constants: {
PUBLISH_DIR,
},
})
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
})
})