簡體   English   中英

開玩笑,測試異步JS代碼總是失敗

[英]Jest , testing Asynchronous JS code always failing

我正在嘗試測試我的節點app.js腳本,其中我對函數sendSmtpMessage()有一個異步請求sendMessageRequest()[一個承諾]

app.js

    const sendSmtpMessage = require("./sendSmtpMessage.js");

    const keys = {....};
    const mailOptions = {...}

    const sendMessageRequest = async () => {
      try {
        const result = await sendSmtpMessage(keys,mailOptions);
        console.log("... SEND MSG REQUEST FULLFILLED: ", result);
      } catch(err){
        console.log("... SEND MSG REQUEST FAILED: ");
      }
    };
    sendMessageRequest();

根據有關測試異步代碼(使用async / await)的文檔,我編寫了以下app.spec.js; 但是我想我的sendSmtpMessage()模擬是錯誤的...

app.spec.js

jest.mock("../sendSmtpMessage.js");
const sendSmtpMessage = require("../sendSmtpMessage.js");
const app = require("../app.js");

// sendSmtpMessage is a mock function
sendSmtpMessage.mockImplementation(() => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      (oauth2ClientMock.refreshToken !== "undefined")? resolve() : reject()
      , 2000
    });
  })
});

describe('app', () => {
  let keys, mailOptions;
  beforeEach(() => {
    keys = {....};
    mailOptions = {....}
  });

  afterEach(() => {
    keys = {};
    mailOptions = {};
  });

  it("should call successfully sendMessageRequest", async () => {
    // GIVEN
    // WHEN
    // THEN
    expect.assertions(1);
    await expect(sendSmtpMessage).resolves.toBe("OK");
  });

  it("should call unsuccessfully sendMessageRequest", async () => {
    // GIVEN
    // WHEN
    keys.oauth.refresh_token = null;
    // THEN
    expect.assertions(1);
    await expect(sendSmtpMessage).rejects.toBeTruthy();
  });

});

由於console.log輸出顯示每個測試的兩個期望值都存在錯誤(解決和拒絕時)

console.log

開玩笑--detectOpenHandles --coverage“ app.spec.js”

失敗test / app.spec.js應用✕應該成功調用sendMessageRequest(15ms)✕應該沒有成功調用sendMessageRequest(2ms)

●應用›應成功調用sendMessageRequest

expect(received).resolves.toBe()

received value must be a Promise.
Received:
  function: [Function mockConstructor]

  52 |     // THEN
  53 |     expect.assertions(1);
> 54 |     await expect(sendSmtpMessage).resolves.toBe("OK");
     |                                            ^
  55 |   });
  56 |
  57 |   it("should call unsuccessfully sendMessageRequest", async () => {

  at Object.toBe (node_modules/expect/build/index.js:158:13)
  at Object.toBe (test/app.spec.js:54:44)

●應用›應成功調用sendMessageRequest

expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

  51 |     // WHEN
  52 |     // THEN
> 53 |     expect.assertions(1);
     |            ^
  54 |     await expect(sendSmtpMessage).resolves.toBe("OK");
  55 |   });
  56 |

  at Object.assertions (test/app.spec.js:53:12)

●應用›應調用失敗sendMessageRequest

expect(received).rejects.toBeTruthy()

received value must be a Promise.
Received:
  function: [Function mockConstructor]

  61 |     // THEN
  62 |     expect.assertions(1);
> 63 |     await expect(sendSmtpMessage).rejects.toBeTruthy();
     |                                           ^
  64 |   });
  65 |
  66 | });

  at Object.toBeTruthy (node_modules/expect/build/index.js:203:13)
  at Object.toBeTruthy (test/app.spec.js:63:43)

●應用›應調用失敗sendMessageRequest

expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

  60 |     keys.oauth.refresh_token = null;
  61 |     // THEN
> 62 |     expect.assertions(1);
     |            ^
  63 |     await expect(sendSmtpMessage).rejects.toBeTruthy();
  64 |   });
  65 |

  at Object.assertions (test/app.spec.js:62:12)

我哪里錯了? 我不太了解此類普通js腳本的測試過程...(用於與vue.js,test-utils一起使用...)

感謝您的反饋,並最終感謝您通過任何鏈接使我了解這種情況下的測試單元...

您不是在等待sendMessageRequest方法本身的調用

const sendSmtpMessage = require("./sendSmtpMessage.js");

        const keys = {....};
        const mailOptions = {...}

        const sendMessageRequest = async () => {
          try {
            const result = await sendSmtpMessage(keys,mailOptions);
            console.log("... SEND MSG REQUEST FULLFILLED: ", result);
          } catch(err){
            console.log("... SEND MSG REQUEST FAILED: ");
          }
        };

    (async function() {
      await sendMessageRequest();
    })();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM