小编dsi*_*cos的帖子

在 Jest 中测试失败时如何打印请求和响应?

我正在使用 Supertest 和 Jest 来测试 Node.js API。

示例测试具有以下格式

it('Read a note for a user', (done) => {
      request(graphqlURL)
        .post('/graphql')
        .set(baseHeaders())
        .send({
          query: graphQLQuery
        })
        .end((err, res) => {
          expect(res.status).toBe(200);          
          done();
        })

    });
Run Code Online (Sandbox Code Playgroud)

当前,当期望失败时,会记录以下内容

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

    Expected: 200
    Received: 404

Run Code Online (Sandbox Code Playgroud)

我还想在失败的测试旁边记录请求和响应,以便在调试时获得更多上下文。

有没有办法打印那些,仅用于失败的测试?

node.js supertest jestjs

5
推荐指数
1
解决办法
3728
查看次数

为什么在Node.js中使用async-await时,我会丢失堆栈跟踪?

当我运行以下程序时

async function functionOne() {

  throw new Error('Error here prints the complete stack');

  await new Promise((resolve) => {
    setTimeout(() => { resolve(); }, 1000);
  });
}

async function functionTwo() {
  await functionOne();
}

async function functionThree() {
  await functionTwo();
}

functionThree()
  .catch((error) => {
    console.error(error);
  });
Run Code Online (Sandbox Code Playgroud)

我得到以下输出,该输出通过各种调用的函数打印堆栈

Error: Error here prints the complete stack
    at functionOne (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:3:9)
    at functionTwo (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:11:9)
    at functionThree (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:15:9)
    at Object.<anonymous> (/home/divyanshu/programming/errorHandlingAsyncAwait/index.js:18:1)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load …
Run Code Online (Sandbox Code Playgroud)

javascript error-handling stack-trace node.js async-await

5
推荐指数
1
解决办法
1389
查看次数

如何将 Docker 构建的上下文定义为本地分支之一的特定提交?

我想使用引用本地存储库的提交哈希来提供 Docker 构建命令的上下文。

涵盖 Docker 构建的文档指定如何引用远程存储库上而不是本地存储库上的分支或标签。参考

我尝试用协议替换 URLfile://以引用本地 git 存储库,但这会返回错误

docker build file:///home/username/repositories/hello-world

错误

无法准备上下文:找不到路径“file:///home/username/repositories/hello-world”

我想将本地 Git 存储库(特定提交、标签或分支)作为 Docker 构建映像的构建上下文。

我已经看过这个问题Docker build Specific local gitbranch但我不希望必须克隆或签出分支来从中构建。

git docker

5
推荐指数
1
解决办法
1219
查看次数