簡體   English   中英

可以在測試塊輸出中開玩笑輸出控制台日志

[英]can jest output console log within test block output

因此,如果我將console.log放入test ,則測試后將出現console.log ,例如

  authentication.spec.js
    register
      ✓ should be able to insert (128ms)
      ✓ should fail because of duplicate (100ms)
      ✓ should have user id assigned (1ms)
    login
      ✓ should be able to login for correct user (117ms)
      ✓ should fail for incorrect user (14ms)

  console.log tests/unit/authentication.spec.js:110
    we can see a message

我想看到的是:

  authentication.spec.js
    register
      ✓ should be able to insert (128ms)
      ✓ should fail because of duplicate (100ms)
      ✓ should have user id assigned (1ms)
    login
      ✓ should be able to login for correct user (117ms)
        console.log tests/unit/authentication.spec.js:110
           we can see a message
      ✓ should fail for incorrect user (14ms)

所以在這種情況下, console.log應該顯示為✓ should be able to login for correct user

當我使用 Mocha 時,我使用的是mocha-logger

據我所知,這並不容易,盡管有幾個地方可以尋找更多信息(不是開箱即用的):

Jest 允許使用自定義報告器: https : //jestjs.io/docs/en/configuration.html#reporters-array-modulename-modulename-options ,因此您可以編寫自己的報告器並以不同方式顯示輸出。 目前,雖然您沒有獲得單獨測試的更新,只是測試服,但這里是 Dan Abramov 創建的一個問題: https : //github.com/facebook/jest/issues/6616

從上面的 github 線程 - 現在的 Reporter 界面看起來像:

export default class BaseReporter {
  onRunStart(results: AggregatedResult, options: ReporterOnStartOptions) {}

  // these are actually for the test suite, not individual test results
  onTestStart(test: Test) {}
  onTestResult(test: Test, testResult: TestResult, results: AggregatedResult) {}

  onRunComplete(contexts: Set<Context>, results: AggregatedResult ): ?Promise<void> {}
}

我沒有找到將參數從 test 傳遞到testResult對象的預定義方法。 因此,您主要限於根據測試名稱記錄信息。 下面是testResult對象中testResult屬性的示例:

testResults:
  [ { ancestorTitles: [Array],
       duration: 5,
       failureMessages: [],
       fullName: 'add should add two numbers',
       location: null,
       numPassingAsserts: 0,
       status: 'passed',
       title: 'should add two numbers' } ],

如您所見,這是標准記者使用的所有信息:測試名稱、持續時間、狀態。 作為參考,默認記者在這里: https : //github.com/facebook/jest/blob/7b7fd01350/packages/jest-cli/src/reporters/default_reporter.js

是的,您可以將其記錄下來。 您可能需要將--verbose false添加到 package.json "test"

例如: "scripts": { "test": "jest --watch --verbose false" }

請在 github 上查看此處的詳細信息以了解玩笑錯誤

我用這個:

// 包.json

...
    "scripts": {
        ...
        "test": "react-scripts test --verbose=true",
        ...
    }
...

在控制台中,它看起來像......

在此處輸入圖片說明

暫無
暫無

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

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