可以在测试块输出中嘲笑输出控制台日志

时间:2018-10-29 03:17:30

标签: javascript testing jestjs

因此,如果我将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

3 个答案:

答案 0 :(得分:6)

据我所知,这很难实现,尽管有几个地方可以查找更多信息(并非开箱即用):

Jest允许使用自定义报告程序:https://jestjs.io/docs/en/configuration.html#reporters-array-modulename-modulename-options,因此您可以编写自己的报告程序并以不同方式显示输出。目前,虽然您没有获得单独测试的更新,而只是测试服,但这是Dan Abramov创建的一个问题:https://github.com/facebook/jest/issues/6616

从上面的github线程-现在的Reporter界面看起来像:

df = pd.DataFrame(index=idx)

我没有找到一种预定义的方法来将参数从测试传递到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> {} } 对象中。因此,您通常仅限于根据测试名称记录信息。以下是testResult对象中的testResult属性的示例:

testResult

您可以看到这是标准报告者使用的所有信息:测试名称,持续时间,状态。供参考,默认的报告者在这里:https://github.com/facebook/jest/blob/7b7fd01350/packages/jest-cli/src/reporters/default_reporter.js

答案 1 :(得分:2)

是的,您可以将其记录下来。您可能需要将--verbose false添加到package.json "test";

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

See the details here at github for jest bugs

答案 2 :(得分:0)

我用这个:

// package.json

modelBuilder.Entity<NxProperty>().HasMany(...)

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

enter image description here

相关问题