Skip to content

Commit ea07d97

Browse files
authored
Merge pull request #34 from netlify/chore/test-fixtures
2 parents 4ceb71f + b31cf24 commit ea07d97

File tree

12 files changed

+76
-53
lines changed

12 files changed

+76
-53
lines changed

__mocks__/cpx.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

__mocks__/make-dir.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

__mocks__/makef.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

__mocks__/next-on-netlify.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
const fs = require('fs')
12
const path = require('path')
3+
const util = require('util')
4+
25
const nextOnNetlify = require('next-on-netlify')
36
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
47
const { default: loadConfig } = require('next/dist/next-server/server/config')
5-
const makef = require('makef')
68
const makeDir = require('make-dir')
79
const pathExists = require('path-exists')
810
const cpx = require('cpx')
11+
912
const isStaticExportProject = require('./helpers/isStaticExportProject')
1013

14+
const pWriteFile = util.promisify(fs.writeFile)
15+
1116
// * Helpful Plugin Context *
1217
// - Between the prebuild and build steps, the project's build command is run
1318
// - Between the build and postbuild steps, any functions are bundled
@@ -64,7 +69,7 @@ module.exports = {
6469
target: 'serverless'
6570
}
6671
`
67-
makef.createFile({ 'next.config.js': nextConfig })
72+
await pWriteFile('next.config.js', nextConfig)
6873
console.log(`** Adding next.config.js with target set to 'serverless' **`)
6974
}
7075
},

package-lock.json

Lines changed: 18 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"dependencies": {
2727
"cpx": "^1.5.0",
2828
"make-dir": "^3.1.0",
29-
"makef": "^1.0.0",
3029
"next": "^9.5.3",
3130
"next-on-netlify": "^2.6.0",
3231
"path-exists": "^4.0.0"
@@ -35,10 +34,10 @@
3534
"@netlify/eslint-config-node": "^0.3.0",
3635
"husky": "^4.3.0",
3736
"jest": "^26.6.1",
38-
"mock-fs": "^4.13.0",
3937
"prettier": "^2.1.2",
4038
"react": "^17.0.1",
41-
"react-dom": "^17.0.1"
39+
"react-dom": "^17.0.1",
40+
"tmp-promise": "^3.0.2"
4241
},
4342
"husky": {
4443
"hooks": {
@@ -47,7 +46,8 @@
4746
},
4847
"jest": {
4948
"testMatch": [
50-
"**/test/**/*.js"
49+
"**/test/**/*.js",
50+
"!**/test/fixtures/**"
5151
],
5252
"verbose": true
5353
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
target: 'server',
3+
}

test/fixtures/publish_copy_files/out_publish/subdir/dummy.txt

Whitespace-only changes.

test/fixtures/publish_copy_files/publish/subdir/dummy.txt

Whitespace-only changes.

test/fixtures/publish_copy_files/subdir/dummy.txt

Whitespace-only changes.

test/index.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const path = require('path')
2+
const process = require('process')
3+
24
const nextOnNetlify = require('next-on-netlify')
3-
const makef = require('makef')
4-
const makeDir = require('make-dir')
5-
const cpx = require('cpx')
6-
const mockFs = require('mock-fs')
7-
const plugin = require('../index')
5+
const pathExists = require('path-exists')
6+
const { dir: getTmpDir } = require('tmp-promise')
7+
8+
const plugin = require('..')
9+
10+
const FIXTURES_DIR = `${__dirname}/fixtures`
811

912
const utils = {
1013
run: {
@@ -15,21 +18,41 @@ const utils = {
1518
},
1619
}
1720

18-
afterEach(() => {
21+
// Temporary switch cwd
22+
const changeCwd = function (cwd) {
23+
const originalCwd = process.cwd()
24+
process.chdir(cwd)
25+
return process.chdir.bind(process, originalCwd)
26+
}
27+
28+
// Switch cwd to a fixture directory
29+
const useFixture = function (fixtureName) {
30+
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
31+
const restoreCwd = changeCwd(fixtureDir)
32+
return { restoreCwd, fixtureDir }
33+
}
34+
35+
// In each test, we change cwd to a temporary directory.
36+
// This allows us not to have to mock filesystem operations.
37+
beforeEach(async () => {
38+
const { path, cleanup } = await getTmpDir({ unsafeCleanup: true })
39+
const restoreCwd = changeCwd(path)
40+
Object.assign(this, { cleanup, restoreCwd })
41+
})
42+
43+
afterEach(async () => {
1944
utils.build.failBuild.mockReset()
2045
utils.run.command.mockReset()
2146
jest.clearAllMocks()
2247
jest.resetAllMocks()
48+
49+
// Cleans up the temporary directory from `getTmpDir()` and do not make it
50+
// the current directory anymore
51+
this.restoreCwd()
52+
await this.cleanup()
2353
})
2454

2555
jest.mock('next-on-netlify')
26-
jest.mock('makef')
27-
jest.mock('make-dir')
28-
jest.mock('cpx')
29-
30-
// See: https://github.com/tschaub/mock-fs/issues/234#issuecomment-377862172
31-
// for why this log is required
32-
console.log('Initializing tests')
3356

3457
const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
3558

@@ -94,24 +117,18 @@ describe('preBuild()', () => {
94117
constants: { FUNCTIONS_SRC: 'out_functions' },
95118
})
96119

97-
expect(makef.createFile.mock.calls.length).toEqual(1)
120+
expect(await pathExists('next.config.js')).toBeTruthy()
98121
})
99122

100123
test(`fail build if the app's next config has an invalid target`, async () => {
101-
mockFs({
102-
'next.config.js': {
103-
target: 'nonsense',
104-
},
105-
})
106-
124+
const { restoreCwd } = useFixture('invalid_next_config')
107125
await plugin.onPreBuild({
108126
netlifyConfig: {},
109127
packageJson: DUMMY_PACKAGE_JSON,
110128
utils,
111129
constants: { FUNCTIONS_SRC: 'out_functions' },
112130
})
113-
114-
mockFs.restore()
131+
restoreCwd()
115132

116133
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
117134
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
@@ -139,17 +156,19 @@ describe('onBuild()', () => {
139156
},
140157
})
141158

142-
expect(makeDir.mock.calls[0][0]).toEqual(PUBLISH_DIR)
159+
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
143160
})
144161

145162
test('calls copySync with correct args', async () => {
146-
const PUBLISH_DIR = 'some/path'
163+
const { restoreCwd, fixtureDir } = useFixture('publish_copy_files')
164+
const PUBLISH_DIR = `${fixtureDir}/publish`
147165
await plugin.onBuild({
148166
constants: {
149167
PUBLISH_DIR,
150168
},
151169
})
170+
restoreCwd()
152171

153-
expect(cpx.copySync.mock.calls[0][0]).toEqual('out_publish/**/*', PUBLISH_DIR)
172+
expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy()
154173
})
155174
})

0 commit comments

Comments
 (0)