是否可以在Azure函数运行库中运行Jest



这可能是一种"你在工作中使用了错误的工具"的情况,但我无论如何都会解决我的问题,因为这是我现在必须解决的问题。

所以,这是:

我必须制作相对较小的应用程序,这些应用程序作为功能定期在Azure环境中运行。这些应用程序执行诸如从API获取数据并将数据存储在SFTP服务器上的任务。当我创建这些应用程序时,我使用Jest的TDD方法。

我想主动应对任何问题,并在计划功能运行之前解决这些问题。如果我在本地运行Jest,我会注意到这些问题,但我想自动化这个过程。因此,我想知道是否可以从Azure功能运行这些测试,并在其中一次运行失败时让Azure警告通知我。

我试过什么

  • 创建了新的功能文件夹"Jest_Function">
  • 在一个单独的文件中添加了一个总是失败的测试
/main_functions_folder
/jest_function
- index.js
- function.json
- failingTest.test.js
  • 在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;
}
};

转换功能并在终端中运行会产生预期行为:

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

我在Azure中部署了该功能,并手动运行了它。这导致了一个失败的功能,正如我所预期的,但原因是错误的。它显示以下错误消息:

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

我真的不知道该何去何从,任何帮助或建议都将不胜感激!

不确定你是否可以直接从函数中使用jest,但我知道你可以在Azure函数中无头运行pupeer:

https://anthonychu.ca/post/azure-functions-headless-chromium-puppeteer-playwright/

还有jest-pupeteer包,但如果所有dep都作为运行时依赖项安装,则不确定函数中的jest-in-Function是否有特定限制。

我能够使用npx而不是npm:

const { exec } = require('child_process');
function checkTests() {
return new Promise((resolve, reject) => {
exec('npx jest jest_function/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;
}
};

查看日志,我真的不确定"330"到底是什么,但假设它正在安装jest?

2022-04-19T09:54:06Z   [Information]   Error: Command failed: npx jest
npx: installed 330 in 32.481s

无论如何,我很高兴我现在能做到这一点:(。

最新更新