Skip to content

add some initial lightweight testing #20

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 4 commits into from
Nov 10, 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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Plugin Tests

on:
push:
branches: [main]
pull_request:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ node_modules
# Optional REPL history
.node_repl_history
.next

# Test
__mocks__
sample
3 changes: 3 additions & 0 deletions __mocks__/cpx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
copySync: jest.fn()
};
1 change: 1 addition & 0 deletions __mocks__/make-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = jest.fn();
3 changes: 3 additions & 0 deletions __mocks__/makef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
createFile: jest.fn()
};
1 change: 1 addition & 0 deletions __mocks__/next-on-netlify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = jest.fn();
28 changes: 19 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs')
const { existsSync, readFileSync } = require('fs')
const { promisify } = require('util')
const exists = promisify(fs.exists)
const path = require('path')
const { appendFile, readdir } = require('fs').promises
const { hasFramework } = require('@netlify/framework-info')
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 cpx = require('cpx')
const isStaticExportProject = require('./helpers/isStaticExportProject')
Expand All @@ -15,14 +15,22 @@ const isStaticExportProject = require('./helpers/isStaticExportProject')
// - Between the build and postbuild steps, any functions are bundled

module.exports = {
async onPreBuild({ netlifyConfig, packageJson: { scripts = {}, dependencies = {} }, utils }) {
async onPreBuild({ netlifyConfig, packageJson, utils }) {
const { failBuild } = utils.build

if (!(await hasFramework('next'))) {
return failBuild(`This application does not use Next.js.`)
if (!packageJson) {
failBuild(`Could not find a package.json for this project`)
return
}

if (!netlifyConfig) {
failBuild(`Could not find a Netlify configuration for this project`)
return
}

const { build } = netlifyConfig
const { scripts = {}, dependencies = {} } = packageJson

// TO-DO: Post alpha, try to remove this workaround for missing deps in
// the next-on-netlify function template
await utils.run.command('npm install next-on-netlify@latest')
Expand All @@ -46,7 +54,8 @@ module.exports = {
)
}

if (existsSync('next.config.js')) {
const hasNextConfig = await exists('next.config.js')
if (hasNextConfig) {
// If the next config exists, fail build if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
Expand All @@ -61,7 +70,7 @@ module.exports = {
target: 'serverless'
}
`
await appendFile('next.config.js', nextConfig)
makef.createFile({ 'next.config.js': nextConfig })
console.log(`** Adding next.config.js with target set to 'serverless' **`)
}
},
Expand All @@ -77,7 +86,8 @@ module.exports = {
// if (!existsSync(FUNCTIONS_DIST)) {
// await makeDir(FUNCTIONS_DIST);
// }
if (!existsSync(PUBLISH_DIR)) {
const hasPublishDir = await exists(PUBLISH_DIR)
if (!hasPublishDir) {
await makeDir(PUBLISH_DIR)
}

Expand Down
Loading