Skip to content

Use test fixtures #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions __mocks__/cpx.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/make-dir.js

This file was deleted.

3 changes: 0 additions & 3 deletions __mocks__/makef.js

This file was deleted.

1 change: 0 additions & 1 deletion __mocks__/next-on-netlify.js

This file was deleted.

9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const fs = require('fs')
const path = require('path')
const util = require('util')

const nextOnNetlify = require('next-on-netlify')
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const { default: loadConfig } = require('next/dist/next-server/server/config')
const makef = require('makef')
const makeDir = require('make-dir')
const pathExists = require('path-exists')
const cpx = require('cpx')

const isStaticExportProject = require('./helpers/isStaticExportProject')

const pWriteFile = util.promisify(fs.writeFile)

// * Helpful Plugin Context *
// - Between the prebuild and build steps, the project's build command is run
// - Between the build and postbuild steps, any functions are bundled
Expand Down Expand Up @@ -64,7 +69,7 @@ module.exports = {
target: 'serverless'
}
`
makef.createFile({ 'next.config.js': nextConfig })
await pWriteFile('next.config.js', nextConfig)
console.log(`** Adding next.config.js with target set to 'serverless' **`)
}
},
Expand Down
32 changes: 18 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"dependencies": {
"cpx": "^1.5.0",
"make-dir": "^3.1.0",
"makef": "^1.0.0",
"next": "^9.5.3",
"next-on-netlify": "^2.6.0",
"path-exists": "^4.0.0"
Expand All @@ -35,10 +34,10 @@
"@netlify/eslint-config-node": "^0.3.0",
"husky": "^4.3.0",
"jest": "^26.6.1",
"mock-fs": "^4.13.0",
"prettier": "^2.1.2",
"react": "^17.0.1",
"react-dom": "^17.0.1"
"react-dom": "^17.0.1",
"tmp-promise": "^3.0.2"
},
"husky": {
"hooks": {
Expand All @@ -47,7 +46,8 @@
},
"jest": {
"testMatch": [
"**/test/**/*.js"
"**/test/**/*.js",
"!**/test/fixtures/**"
],
"verbose": true
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/invalid_next_config/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
target: 'server',
}
Empty file.
Empty file.
Empty file.
69 changes: 44 additions & 25 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const path = require('path')
const process = require('process')

const nextOnNetlify = require('next-on-netlify')
const makef = require('makef')
const makeDir = require('make-dir')
const cpx = require('cpx')
const mockFs = require('mock-fs')
const plugin = require('../index')
const pathExists = require('path-exists')
const { dir: getTmpDir } = require('tmp-promise')

const plugin = require('..')

const FIXTURES_DIR = `${__dirname}/fixtures`

const utils = {
run: {
Expand All @@ -15,21 +18,41 @@ const utils = {
},
}

afterEach(() => {
// Temporary switch cwd
const changeCwd = function (cwd) {
const originalCwd = process.cwd()
process.chdir(cwd)
return process.chdir.bind(process, originalCwd)
}

// Switch cwd to a fixture directory
const useFixture = function (fixtureName) {
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
const restoreCwd = changeCwd(fixtureDir)
return { restoreCwd, fixtureDir }
}

// 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 () => {
utils.build.failBuild.mockReset()
utils.run.command.mockReset()
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does cleanup do and why is it async?

Copy link
Author

@ehmicky ehmicky Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some code comment 👍

})

jest.mock('next-on-netlify')
jest.mock('makef')
jest.mock('make-dir')
jest.mock('cpx')

// See: https://github.com/tschaub/mock-fs/issues/234#issuecomment-377862172
// for why this log is required
console.log('Initializing tests')

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

Expand Down Expand Up @@ -94,24 +117,18 @@ describe('preBuild()', () => {
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(makef.createFile.mock.calls.length).toEqual(1)
expect(await pathExists('next.config.js')).toBeTruthy()
})

test(`fail build if the app's next config has an invalid target`, async () => {
mockFs({
'next.config.js': {
target: 'nonsense',
},
})

const { restoreCwd } = useFixture('invalid_next_config')
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})

mockFs.restore()
restoreCwd()

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

expect(makeDir.mock.calls[0][0]).toEqual(PUBLISH_DIR)
expect(await pathExists(PUBLISH_DIR)).toBeTruthy()
})

test('calls copySync with correct args', async () => {
const PUBLISH_DIR = 'some/path'
const { restoreCwd, fixtureDir } = useFixture('publish_copy_files')
const PUBLISH_DIR = `${fixtureDir}/publish`
await plugin.onBuild({
constants: {
PUBLISH_DIR,
},
})
restoreCwd()

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