Is it possible to run Jest in Azure function runtime?

This might be a case of ‘you’re using the wrong tools for the job’ but I’m going to shoot my question anyways, because this is what I have to work with for now.

So, here goes:

I have to make relatively small applications that periodically run as functions in an Azure environment. These applications perform tasks like fetching data from an API and storing that data on a SFTP server. When I create these applications I use a TDD approach with Jest.

I’d like to react to any problems proactively and solve them before the function runs are scheduled. If I run Jest locally I would notice any of these problems but I’d like to automate this proces. Therefor I’d like to know if it’s possible to run these tests from an Azure function and have Azure Warnings notify me when one these runs fail.

What have I tried?

  • Created new function folder “Jest_Function”
  • Added an always failing test in a separate file.
/main_functions_folder
    /jest_function
        - index.js
        - function.json
        - failingTest.test.js
  • added the following code to index.js:
const { exec } = require('child_process');

function checkTests() {
  return new Promise((resolve, reject) => {
    exec('npm run test failingTest.test.js', (error) => {
      if (error) reject(error);
      else resolve();
    });
  });
}

module.exports = async function (context) {
  try {
    await checkTests();
  } catch (err) {
    context.log('tests failed!');
    throw err;
  }
};

Transforming the function and running it in the terminal results in expected behaviour:

const { exec } = require('child_process');

function checkTests() {
  return new Promise((resolve, reject) => {
    exec('npm run test failingTest.test.js', (error) => {
      if (error) reject(error);
      else resolve();
    });
  });
}

async function myTest() {
  try {
    await checkTests();
  } catch (err) {
    console.log('tests failed!');
    throw err;
  }
}

myTest();
tests failed!
node:child_process:399
      ex = new Error('Command failed: ' + cmd + 'n' + stderr);
           ^

Error: Command failed: npm run test failingTest.test.js
FAIL jest_function/failingTest.test.js
  ✕ short test (3 ms)

  ● short test

    expect(received).toBe(expected) // Object.is equality

    Expected: 1
    Received: 0

      1 | test('short test', () => {
    > 2 |   expect(0).toBe(1);
        |             ^
      3 | });
      4 |

      at Object.<anonymous> (jest_function/failingTest.test.js:2:13)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.227 s, estimated 1 s
Ran all test suites matching /failingTest.test.js/i.

    at ChildProcess.exithandler (node:child_process:399:12)
    at ChildProcess.emit (node:events:520:28)
    at maybeClose (node:internal/child_process:1092:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'npm run test failingTest.test.js'
}

Azure

I deployed the function in Azure and manualy ran it. This resulted in a failing function as I expected, but for the wrong reason. It displayed the following error message:

Result: Failure Exception: Error: Command failed: npm run test failingTest.test.js sh: 1: jest: Permission denied

I’m not really sure where to go from here, any help or advice will be appreciated!