Skip to content

Commit

Permalink
tests(ci): add basic CI tests and fix POST requests bug (#68)
Browse files Browse the repository at this point in the history
* Update everything

* Fix POST requests

* Formatting

* Formatting

* Resurrect POST example

* Fix formatting

* Add simple test

* Working local test

* Docs and formatting

* Self-review

* Review comments

* Add examples checks

* npm cache

* Default dir

* More GHA syntax

* Formatting

* Tests only with 18

* Install local version of framework

* Link in script

* Reduce sleep

* Remove workflow names

* Update test

* try to retrigger GHA

* Reinstate example runner

* self-hosted runners

* Revert self-hosted change

* Fix package-locks

* Add scope to link command

* Attempt linking

* Avoid starting twice

* Revert dodgy package-lock changes
  • Loading branch information
Shillaker authored Nov 8, 2023
1 parent ee5b830 commit 514a1c1
Show file tree
Hide file tree
Showing 15 changed files with 9,296 additions and 613 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/npmtest.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
name: npmtest

on:
push:
branches: ["main"]
Expand All @@ -20,18 +18,15 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Set up node ${{ matrix.node-version }}
- name: "Set up node ${{ matrix.node-version }}"
id: setup-node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Install dependencies
- name: "Install dependencies"
run: npm ci

- name: Check formatting
run: npx prettier --check .

- name: Build package
- name: "Build package"
run: npm run build
14 changes: 6 additions & 8 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Publish to NPM

---
on:
release:
types: [published]
Expand All @@ -8,19 +7,18 @@ jobs:
release:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/checkout@v4

- name: Setup Node
- name: "Set up Node"
uses: actions/setup-node@v4
with:
node-version: "18"
node-version: "18.x"
registry-url: "https://registry.npmjs.org"

- name: Install dependencies and build
- name: "Install dependencies and build"
run: npm ci && npm run build

- name: Publish package on NPM
- name: "Publish package on NPM"
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
83 changes: 83 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- name: "Set up node ${{ matrix.node-version }}"
id: setup-node
uses: actions/setup-node@v4
with:
node-version: "18.x"
cache: "npm"

- name: "Install dependencies"
run: npm ci

- name: "Check formatting"
run: npx prettier --check .

- name: "Run tests"
run: npm test

examples:
defaults:
run:
working-directory: ./examples

strategy:
matrix:
node-version: ["16.x", "18.x"]
example:
[
"not_stringified_body",
"print_event_context",
"stringified_body",
"upload_file_multipart",
"with_callback",
"with_event_components",
"with_http_get",
"with_http_post",
"with_object",
"with_promise",
]

runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- name: "Set up node ${{ matrix.node-version }}"
id: setup-node
uses: actions/setup-node@v4
with:
node-version: "${{ matrix.node-version }}"
cache: "npm"

- name: "Link top-level"
run: npm link

- name: "Install dependencies"
run: npm ci
working-directory: "examples"

- name: "Link local framework"
run: npm link @scaleway/serverless-functions
working-directory: "examples"

- name: "Run example"
run: node ${{ matrix.example }}/handler.js &
working-directory: "examples"

- name: "Wait for it to start"
run: sleep 1

- name: "Launch a request"
run: curl http://localhost:8080
working-directory: "examples"
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ By running a function locally like this, we can be sure it will work when deploy
You can find a number of examples in the [`examples` folder](examples/). These include:
- [Accessing the event context](examples/print_event_context)
- [Making HTTP GET requests](examples/with_http_services)
- [Making HTTP POST requests](examples/with_http_services)
- [Uploading files](examples/upload_file_multipart)
- [Using promises](examples/with_promise)
- [Using callbacks](examples/with_callback)
- [Interacting with HTTP services](examples/with_http_services)
## 🏡 Local testing
Expand Down
64 changes: 64 additions & 0 deletions docs/dev.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Development

To test your local version of this package, you can use [`npm link`](https://docs.npmjs.com/cli/v6/commands/npm-link).

You can do this from the `examples` directory with:

```sh
cd examples
npm link ..

node with_object/handler.js
```

## Tests

You can run tests locally with:

```sh
npm test
```

Make sure you have at least Node 18 installed (see below).

## Releasing

To release a new version:

- Update the version in `package.json`
- Create a PR (or add to an existing PR)
- Merge the PR to `main`
- Create a new Release [in Github](https://github.com/scaleway/serverless-functions-node/releases)

Please follow [Semantic Versioning](https://semver.org/) guidelines when deciding which version to go to.

## Managing multiple Node versions

The build runs on multiple versions of Node. To manage multiple Node versions locally, you can use [`nvm`](https://github.com/nvm-sh/nvm).

Once installed, you can install versions with:

```sh
# Install versions
nvm install 16
nvm install 18

# Check
nvm use 16
npm i
which node

nvm use 18
npm i
which node
```

From there, you can check the build:

```sh
nvm use 16
npm run build

nvm use 18
npm run build
```
Loading

0 comments on commit 514a1c1

Please sign in to comment.