我正在尝试使用Jest运行样本测试,以验证我的Google Cloud Function是否正常运行,但是我一直在跟踪错误。
错误:命令失败:gcloud beta函数调用cf-1 --region europe-west1 --data'{“ data”:“ eyJkYXRhIjoiMSJ9”}'
错误:(gcloud.beta.functions.call)[--data]的值无效:不是有效的JSON:无法解码JSON对象
我知道在Windows终端中运行命令时我可以用反斜杠转义双引号,但是如何在JavaScript中做到这一点。
test.js
const childProcess = require('child_process');
describe('Test CF', () => {
it('print outs the error message when received JSON is blank', done => {
const msg = { data: '1' };
const encodedMsg = Buffer.from(JSON.stringify(msg)).toString('base64');
const data = JSON.stringify({ data: encodedMsg });
const executeResultOutput = childProcess.execSync(`gcloud beta functions call cf-1 --region europe-west1 --data '${data}'`).toString();
const logs = childProcess
.execSync(
`gcloud functions logs read cf-1 --region europe-west1 --execution-id ${executionIdObj}`,
)
.toString();
expect(logs).toEqual(expect.stringContaining('Error..'));
});
});
答案 0 :(得分:0)
尝试两次:
data = {"data":"eyJkYXRhIjoiMSJ9"}
console.log(
JSON.stringify(JSON.stringify(data))
)