Skip to content

Commit ed8152f

Browse files
authored
Merge pull request #37 from netlify/test/fixtures-2
Improve fixtures setup
2 parents e456a79 + 6d34694 commit ed8152f

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

test/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const path = require('path')
22
const process = require('process')
3+
const { promisify } = require('util')
34

5+
const { copy } = require('cpx')
46
const nextOnNetlify = require('next-on-netlify')
57
const pathExists = require('path-exists')
68
const { dir: getTmpDir } = require('tmp-promise')
79

810
const plugin = require('..')
911

12+
const pCopy = promisify(copy)
13+
1014
const FIXTURES_DIR = `${__dirname}/fixtures`
1115

1216
const utils = {
@@ -27,11 +31,10 @@ const changeCwd = function (cwd) {
2731
return process.chdir.bind(process, originalCwd)
2832
}
2933

30-
// Switch cwd to a fixture directory
31-
const useFixture = function (fixtureName) {
34+
// Copy fixture files to the current directory
35+
const useFixture = async function (fixtureName) {
3236
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
33-
const restoreCwd = changeCwd(fixtureDir)
34-
return { restoreCwd, fixtureDir }
37+
await pCopy(`${fixtureDir}/**`, process.cwd())
3538
}
3639

3740
// In each test, we change cwd to a temporary directory.
@@ -115,7 +118,7 @@ describe('preBuild()', () => {
115118
})
116119

117120
test(`fail build if the app's next config has an invalid target`, async () => {
118-
const { restoreCwd } = useFixture('invalid_next_config')
121+
await useFixture('invalid_next_config')
119122
await expect(
120123
plugin.onPreBuild({
121124
netlifyConfig: {},
@@ -124,7 +127,6 @@ describe('preBuild()', () => {
124127
constants: { FUNCTIONS_SRC: 'out_functions' },
125128
}),
126129
).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`)
127-
restoreCwd()
128130
})
129131
})
130132

@@ -151,14 +153,13 @@ describe('onBuild()', () => {
151153
})
152154

153155
test('calls copySync with correct args', async () => {
154-
const { restoreCwd, fixtureDir } = useFixture('publish_copy_files')
155-
const PUBLISH_DIR = `${fixtureDir}/publish`
156+
await useFixture('publish_copy_files')
157+
const PUBLISH_DIR = 'publish'
156158
await plugin.onBuild({
157159
constants: {
158160
PUBLISH_DIR,
159161
},
160162
})
161-
restoreCwd()
162163

163164
expect(await pathExists(`${PUBLISH_DIR}/subdir/dummy.txt`)).toBeTruthy()
164165
})

0 commit comments

Comments
 (0)