if i test my function with curl it works fine and deploys on netlify fine too, but i want to write some tests with jest to ensure everything is right
netlify/edge-functions/status.js: (my edge function)
export default () => new Response('good');
export const config = {path: '/status'};
jest.config.js
module.exports = {
displayName: 'edge-functions',
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
transform: {
'^.+\\.[tj]sx?$': 'ts-jest'
},
testMatch: ['**/?(*.)+(spec|test).[tj]s?(x)'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.json'
}
}
};
tests/setup.js:
const { createServer } = require('netlify-cli');
createServer({
functions: 'netlify/edge-functions',
port: 8888,
})
.then(() => {
console.log('Netlify Dev server started on port 8888');
})
.catch((err) => {
console.error('Error starting Netlify Dev server:', err);
});
tests/status.test.ts:
import edge from '../netlify/edge-functions/status';
describe('Status Edge Function', () => {
it('should return a 200 response with "good"', async () => {
const request = new Request('http://localhost:8888/status');
const response = await edge();
expect(response.status).toBe(200);
expect(await response.text()).toBe('good');
});
});
the setup file is having trouble finding netlify-cli, and im not even sure im doing that right, and cant even find documentation for using it in code or any good examples of testing edge functions, so any help/examples is appreciated
think i got it now, just made my own http server
import http from 'http';
import path from 'path';
import { promises as fs } from 'fs';
let server;
beforeAll(() => {
server = http.createServer(async (req, res) => {
try {
const functionName = req.url.split('/').filter(Boolean)[0] || 'index';
const functionPath = path.resolve(`./netlify/edge-functions/${functionName}.js`);
try {
await fs.access(functionPath);
const handler = (await import(functionPath)).default;
const request = new Request(req.url, { method: req.method });
const response = await handler(request);
res.writeHead(response.status, response.headers);
response.body.pipe(res);
} catch (fileError) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Function Not Found');
}
} catch (error) {
console.error('Error in handler:', error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
}
});
server.listen(8888, () => {console.log('Server listening on port 8888');});
});
afterAll(() => {
return new Promise((resolve) => {
server.close(() => {
resolve();
});
});
});
which requires maxWorkers: 1
in jest.config.js
actually, there still seems to be a problem. it can run normal requests just fine, but when i try to test on a route that uses “@netlify/blobs”, it throws an error:
“The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: siteID, token”
this doesn’t really make sense to me since i dont need those things when just running “netlify dev”, but perhaps im missing something or doing this http server isnt the way and i do need to invoke netlify-cli somehow…
It’s still having lots of issues. I tried a new approach making it just start the server with exec() but that still causes issues trying to wait for it to start properly.
I’m giving up on Jest. Please someone tell me what I should use to test these functions. It’s impossible that I’m the first person to ever want to use testing on Edge Functions, so please tell me what to use.
What exactly do you wish to test for in your Edge Function? Most people are able to send a mock request and test out the response because any other functions used within the Edge Function might be abstracted out into its own unit tests.