Skip to content

Commit

Permalink
Add action to test netlify functions (#51)
Browse files Browse the repository at this point in the history
* Downgrade to Jest@27

ts-jest does not yet support Jest@28. Follow the issue here:
<kulshekhar/ts-jest#3453>

* Move cypress and netlify to optionalDep

Begin general dep cleanup

* Enforce unix-style endings

* Add netlify-api-test GitHub action

* Move aws-lambda to devDeps

* Create test .env in CI
  • Loading branch information
rendall committed May 2, 2022
1 parent a5714a2 commit ab882f6
Show file tree
Hide file tree
Showing 5 changed files with 11,745 additions and 11,675 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/netlify-api-test.yml
@@ -0,0 +1,28 @@
name: Simple Comment

on:
pull_request:
branches: [ master ]

jobs:
test-netlify-functions:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Use Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Build Netlify functions
run: |
npm install yarn@^1 --no-package-lock -g
yarn --frozen-lockfile --production=false --ignore-optional
yarn run build:netlify
- name: Test Netlify functions
run: |
node ./scripts/createTestEnv.mjs
yarn test
17 changes: 9 additions & 8 deletions .vscode/settings.json
@@ -1,8 +1,9 @@
{
"json.schemas": [
{
"fileMatch": ["simple-comment-api.json"],
"url": "./src/schema/openapi.schema.json"
}
]
}
{
"json.schemas": [
{
"fileMatch": ["simple-comment-api.json"],
"url": "./src/schema/openapi.schema.json"
}
],
"files.eol": "\n"
}
22 changes: 12 additions & 10 deletions package.json
Expand Up @@ -6,27 +6,30 @@
"@types/jsonwebtoken": "^8.5.0",
"@types/uuid": "^8.3.0",
"babel-jest": "^27.5.1",
"cypress": "^9.6.0",
"concurrently": "^7.1.0",
"eslint": "^8.14.0",
"http-server": "^14.1.0",
"jest": "^28.0.3",
"jest": "27",
"jest-environment-node": "^27.5.1",
"merge": "^2.1.1",
"mongodb-memory-server": "^8.5.1",
"prettier": "2.6.2",
"ts-jest": "^27.1.4"
},
"optionalDependencies": {
"cypress": "^9.6.0",
"netlify-cli": "^10.0.0"
},
"scripts": {
"build": "yarn run build:backend && yarn run build:frontend",
"build:backend": "yarn run build:netlify",
"build:frontend": "webpack",
"build:netlify": "webpack --config ./webpack.netlify.functions.js",
"posttest": "rimraf ./globalConfig.json",
"prebuild": "rimraf ./functions",
"start": "yarn run build && concurrently \"yarn run watch:backend\" \"yarn run start:backend\" \"yarn run watch:frontend\" \"yarn run start:frontend\" --kill-others",
"start:backend": "",
"start:frontend": "yarn run build:frontend && http-server ./dist --proxy http://localhost:9000 -a localhost",
"test": "jest",
"start": "yarn netlify dev --port 8080",
"test": "jest --verbose",
"test:e2e": "concurrently \"netlify dev --port 8080\" \"cypress run\" --kill-others",
"watch:test": "jest --watch --noStackTrace",
"watch:backend": "tsc -p tsconfig.netlify.functions.json --listEmittedFiles --watch",
"watch:frontend": "tsc --listEmittedFiles --watch"
Expand All @@ -36,21 +39,20 @@
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.7",
"@netlify/functions": "^1.0.0",
"@types/aws-lambda": "^8.10.64",
"@types/aws-lambda": "^8.10.95",
"@types/node": "^17.0.25",
"bcryptjs": "^2.4.3",
"concurrently": "^7.1.0",
"dotenv": "^16.0.0",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.5.0",
"netlify-cli": "^10.0.0",
"rimraf": "^3.0.2",
"ts-loader": "^9.3.0",
"ts-node": "^10.1.0",
"typescript": "^4.6.4",
"uuid": "8.3.2",
"webpack": "^5.4.0",
"webpack-cli": "^4.9.2"
"webpack-cli": "^4.9.2",
"yarn": "^1.22.18"
},
"resolutions": {
"glob-parent": "^6.0.2"
Expand Down
34 changes: 34 additions & 0 deletions scripts/createTestEnv.mjs
@@ -0,0 +1,34 @@
#!/usr/bin/env node

import { existsSync, readFileSync, writeFileSync } from "fs"
import { exit } from "process"

const envExists = existsSync(`${process.cwd()}/.env`)

if (envExists) {
console.error("Error: file .env already exists");
exit(1);
}

const randomString = (length = 10, chars ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz", str = "" ) => {
if (length === 0) return str;
return randomString(length - 1, chars, str + chars[Math.floor(Math.random() * chars.length)]);
}

const exampleEnv = readFileSync(`${process.cwd()}/example.env`, "utf8")
const newEnv = exampleEnv
.replace(/\r/g, "\n")
.split("\n")
.map(l => l.trim())
.filter(l => !l.startsWith("#")) // eliminate comments
.filter(l => l.length > 0) // eliminate blank lines
.map(line => line.split("="))
.map(([key, value]) => /PASSWORD/.test(key) | /SECRET/.test(key) ? [key, randomString()] : [key, value]) // replace secrets
.map( keyVal => keyVal.join("=")) // make it a string
.join("\n");

writeFileSync(`${process.cwd()}/.env`, newEnv);

console.info("Success: file .env created");

exit(0);

0 comments on commit ab882f6

Please sign in to comment.