Skip to content

Commit b31cf24

Browse files
committed
Fix mocking
1 parent daba4cf commit b31cf24

File tree

11 files changed

+69
-47
lines changed

11 files changed

+69
-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: 42 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,44 @@ 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

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-
4459
describe('preBuild()', () => {
4560
test('fail build if the app has static html export in npm script', async () => {
4661
await plugin.onPreBuild({
@@ -102,19 +117,17 @@ describe('preBuild()', () => {
102117
constants: { FUNCTIONS_SRC: 'out_functions' },
103118
})
104119

105-
expect(makef.createFile.mock.calls.length).toEqual(1)
120+
expect(await pathExists('next.config.js')).toBeTruthy()
106121
})
107122

108123
test(`fail build if the app's next config has an invalid target`, async () => {
109-
const restoreCwd = useFixture('invalid_next_config')
110-
124+
const { restoreCwd } = useFixture('invalid_next_config')
111125
await plugin.onPreBuild({
112126
netlifyConfig: {},
113127
packageJson: DUMMY_PACKAGE_JSON,
114128
utils,
115129
constants: { FUNCTIONS_SRC: 'out_functions' },
116130
})
117-
118131
restoreCwd()
119132

120133
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
@@ -143,17 +156,19 @@ describe('onBuild()', () => {
143156
},
144157
})
145158

146-
expect(makeDir.mock.calls[0][0]).toEqual(PUBLISH_DIR)
159+
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
147160
})
148161

149162
test('calls copySync with correct args', async () => {
150-
const PUBLISH_DIR = 'some/path'
163+
const { restoreCwd, fixtureDir } = useFixture('publish_copy_files')
164+
const PUBLISH_DIR = `${fixtureDir}/publish`
151165
await plugin.onBuild({
152166
constants: {
153167
PUBLISH_DIR,
154168
},
155169
})
170+
restoreCwd()
156171

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

0 commit comments

Comments
 (0)