Skip to content
Back

how to run function before deploy

  • 0
  • Self Hosted
  • Functions
elemin67
9 Sep, 2023, 22:48

I want to test my nodejs funtion before I deploy it to git. Im using V1.4.2 and node-18.

How I can run the function to test the functionality?

TL;DR
To run a function before deployment, you can follow these steps: 1. Create a file called 'local-test.mjs' in the '/src' directory. 2. In 'local-test.mjs', import the main function that you want to test from 'main.js' (assuming it's in the same directory). 3. Install the 'dotenv' package and import it into 'local-test.mjs'. This will allow you to load environment variables. 4. Simulate the req and res objects, which represent the request and response objects. 5. Set up console.log and console.error functions for logging purposes. 6. Execute the main
Drake
9 Sep, 2023, 23:01

It's easiest to just deploy it to Appwrite. You can deploy to a local Appwrite instance. Or, you can deploy to your prod project but with a different function Id.

Technically, it's also possible to run the open Runtimes container yourself: https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/node-18.0#usage

elemin67
9 Sep, 2023, 23:14

I find an another way to do it. Simple and fast:

  1. create a file like local-test.mjs in /src
  2. With the content content:
TypeScript
// local-test.mjs
import main from './main.js'; // Assuming 'main.js' is in the same directory
import dotenv from 'dotenv';
dotenv.config();

// Simulate the req and res objects
const req = {
  method: 'GET', // Change this to 'POST' if needed
};

const res = {
  send: (data) => {
    console.log(data);
  },
  json: (data) => {
    console.log(JSON.stringify(data, null, 2));
  },
};

const log = console.log;
const error = console.error;

// Execute the main script
main({ req, res, log, error });
  1. Start it with the command:
TypeScript
node --experimental-modules src/local-test.mjs

Now, you have the respnse, logs, errors, etc like in appwrite console 🙂

Drake
9 Sep, 2023, 23:16

You may run into problems where it works locally, but not in Appwrite. I always feel like working incrementally and deploying to Appwrite is the best approach to make sure things work.

elemin67
9 Sep, 2023, 23:17

Of corse, but during devolopment its not so good deploy always to appwrite to test only 1 change. Its take to much time. Therefore Im looking something to test localy.

Drake
9 Sep, 2023, 23:28

It typically takes 30 seconds or so to deploy for me.

A local instance of Appwrite could be faster too

elemin67
9 Sep, 2023, 23:31

Could be 🙂 But I have some functions also in prod. I can not deploy any changes before testing. That is my pain point

Drake
9 Sep, 2023, 23:37

Can't you deploy a duplicate function (different id)?

Drake
9 Sep, 2023, 23:37

Although local instance is a better approach

elemin67
9 Sep, 2023, 23:39

thats possible, thanks. I can do both.

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more