Skip to content

Commit b1a9e64

Browse files
committed
Fix mocking
1 parent daba4cf commit b1a9e64

File tree

11 files changed

+67
-47
lines changed

11 files changed

+67
-47
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 & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 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"
@@ -37,7 +36,8 @@
3736
"jest": "^26.6.1",
3837
"prettier": "^2.1.2",
3938
"react": "^17.0.1",
40-
"react-dom": "^17.0.1"
39+
"react-dom": "^17.0.1",
40+
"tmp-promise": "^3.0.2"
4141
},
4242
"husky": {
4343
"hooks": {

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: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const path = require('path')
22
const process = require('process')
3+
34
const nextOnNetlify = require('next-on-netlify')
4-
const makef = require('makef')
5-
const makeDir = require('make-dir')
6-
const cpx = require('cpx')
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,32 +18,42 @@ 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+
this.restoreCwd()
50+
await this.cleanup()
2351
})
2452

2553
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')
3354

3455
const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
3556

36-
const FIXTURES_DIR = `${__dirname}/fixtures`
37-
38-
const useFixture = function (fixtureName) {
39-
const originalCwd = process.cwd()
40-
process.chdir(`${FIXTURES_DIR}/${fixtureName}`)
41-
return process.chdir.bind(process, originalCwd)
42-
}
43-
4457
describe('preBuild()', () => {
4558
test('fail build if the app has static html export in npm script', async () => {
4659
await plugin.onPreBuild({
@@ -102,19 +115,17 @@ describe('preBuild()', () => {
102115
constants: { FUNCTIONS_SRC: 'out_functions' },
103116
})
104117

105-
expect(makef.createFile.mock.calls.length).toEqual(1)
118+
expect(await pathExists('next.config.js')).toBeTruthy()
106119
})
107120

108121
test(`fail build if the app's next config has an invalid target`, async () => {
109-
const restoreCwd = useFixture('invalid_next_config')
110-
122+
const { restoreCwd } = useFixture('invalid_next_config')
111123
await plugin.onPreBuild({
112124
netlifyConfig: {},
113125
packageJson: DUMMY_PACKAGE_JSON,
114126
utils,
115127
constants: { FUNCTIONS_SRC: 'out_functions' },
116128
})
117-
118129
restoreCwd()
119130

120131
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
@@ -143,17 +154,19 @@ describe('onBuild()', () => {
143154
},
144155
})
145156

146-
expect(makeDir.mock.calls[0][0]).toEqual(PUBLISH_DIR)
157+
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
147158
})
148159

149160
test('calls copySync with correct args', async () => {
150-
const PUBLISH_DIR = 'some/path'
161+
const { restoreCwd, fixtureDir } = useFixture('publish_copy_files')
162+
const PUBLISH_DIR = `${fixtureDir}/publish`
151163
await plugin.onBuild({
152164
constants: {
153165
PUBLISH_DIR,
154166
},
155167
})
168+
restoreCwd()
156169

157-
expect(cpx.copySync.mock.calls[0][0]).toEqual('out_publish/**/*', PUBLISH_DIR)
170+
expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy()
158171
})
159172
})

0 commit comments

Comments
 (0)