Skip to content

Commit 2849dc5

Browse files
add some initial lightweight testing (#20)
* add some initial lightweight testing * test changes and updates * gh action * fix packageJson default to empty obj
1 parent 9c7adfa commit 2849dc5

File tree

23 files changed

+21726
-4901
lines changed

23 files changed

+21726
-4901
lines changed

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Plugin Tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
node-version: [10.x, 12.x, 14.x]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
- run: npm install
23+
- run: npm test

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ node_modules
1515
# Optional REPL history
1616
.node_repl_history
1717
.next
18+
19+
# Test
20+
__mocks__
21+
sample

__mocks__/cpx.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
copySync: jest.fn()
3+
};

__mocks__/make-dir.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = jest.fn();

__mocks__/makef.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
createFile: jest.fn()
3+
};

__mocks__/next-on-netlify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = jest.fn();

index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const fs = require('fs')
2-
const { existsSync, readFileSync } = require('fs')
2+
const { promisify } = require('util')
3+
const exists = promisify(fs.exists)
34
const path = require('path')
4-
const { appendFile, readdir } = require('fs').promises
5-
const { hasFramework } = require('@netlify/framework-info')
65
const nextOnNetlify = require('next-on-netlify')
76
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
87
const { default: loadConfig } = require('next/dist/next-server/server/config')
8+
const makef = require('makef')
99
const makeDir = require('make-dir')
1010
const cpx = require('cpx')
1111
const isStaticExportProject = require('./helpers/isStaticExportProject')
@@ -15,14 +15,22 @@ const isStaticExportProject = require('./helpers/isStaticExportProject')
1515
// - Between the build and postbuild steps, any functions are bundled
1616

1717
module.exports = {
18-
async onPreBuild({ netlifyConfig, packageJson: { scripts = {}, dependencies = {} }, utils }) {
18+
async onPreBuild({ netlifyConfig, packageJson, utils }) {
1919
const { failBuild } = utils.build
2020

21-
if (!(await hasFramework('next'))) {
22-
return failBuild(`This application does not use Next.js.`)
21+
if (!packageJson) {
22+
failBuild(`Could not find a package.json for this project`)
23+
return
24+
}
25+
26+
if (!netlifyConfig) {
27+
failBuild(`Could not find a Netlify configuration for this project`)
28+
return
2329
}
2430

2531
const { build } = netlifyConfig
32+
const { scripts = {}, dependencies = {} } = packageJson
33+
2634
// TO-DO: Post alpha, try to remove this workaround for missing deps in
2735
// the next-on-netlify function template
2836
await utils.run.command('npm install next-on-netlify@latest')
@@ -46,7 +54,8 @@ module.exports = {
4654
)
4755
}
4856

49-
if (existsSync('next.config.js')) {
57+
const hasNextConfig = await exists('next.config.js')
58+
if (hasNextConfig) {
5059
// If the next config exists, fail build if target isnt in acceptableTargets
5160
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
5261
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
@@ -61,7 +70,7 @@ module.exports = {
6170
target: 'serverless'
6271
}
6372
`
64-
await appendFile('next.config.js', nextConfig)
73+
makef.createFile({ 'next.config.js': nextConfig })
6574
console.log(`** Adding next.config.js with target set to 'serverless' **`)
6675
}
6776
},
@@ -77,7 +86,8 @@ module.exports = {
7786
// if (!existsSync(FUNCTIONS_DIST)) {
7887
// await makeDir(FUNCTIONS_DIST);
7988
// }
80-
if (!existsSync(PUBLISH_DIR)) {
89+
const hasPublishDir = await exists(PUBLISH_DIR)
90+
if (!hasPublishDir) {
8191
await makeDir(PUBLISH_DIR)
8292
}
8393

0 commit comments

Comments
 (0)