Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mocha-teamcity-reporter

travisjeffery116.4kMIT4.2.0

teamcity reporter for mocha

mocha, teamcity, reporter, jetbrains

readme

NPM version TeamCity Build Status Monthly Downloads

mocha-teamcity-reporter

mocha-teamcity-reporter Teamcity reporter which makes it possible to display test results in real-time, makes test information available on the Tests tab of the Build Results page.

Version 4.x changes

  • Breaking: Only supported on node.js 6 and above
  • Breaking: Only Mocha version 6 and above is now supported
    • Please remain on mocha-teamcity-reporter@3 if this is an issue
  • New reporter option ignoreHookWithName to skip reporting for hooks with title containing some word (@DJ-Glock)
  • Added postfix _hook for flowId for hooks to ensure flowIds will never intersect.
  • Implement 'hook end' event (@DJ-GLock)
  • General maintenance and tidy up (@DJ-Glock)

Mocha@6 notes

  • recordHookFailures option may not work as intended as mocha6 is now doing this itself

Requirements

  • NodeJs 6+
  • Mocha 6+
  • Web Browser supporting ES5

To Install

In your project run a npm install command:

npm install mocha-teamcity-reporter --save-dev

Basically, have your project's package.json be like:

{
  "devDependencies": {
    "mocha-teamcity-reporter": ">=2.0.0"
  }
}

Usage

https://github.com/visionmedia/mocha/wiki/Third-party-reporters describes using third party reporters in mocha.

Then call mocha with:

mocha --reporter mocha-teamcity-reporter test

Running In Browser

  • Use lib/teamcityBrowser
  • Has option parsing stripped out for the moment
  • Example use can be found in test\browser
  • Custom log function can be set with window.customLogFunction

Customisation

TeamCity flowId

Can set flowId like: mocha test --reporter mocha-teamcity-reporter --reporter-options flowId=gobbledygook

Top-level suite name

Can set a top-level suite name, which will wrap all other suites.
This is useful for reading test output when running multiple suites in a single build

  • Environment variable: MOCHA_TEAMCITY_TOP_LEVEL_SUITE=<suiteName>
  • Reporter option: topLevelSuite=<suiteName>

log test failures with std error

To enable this please Please note this will probaly be made default in the next major version

  • Environment variable: USE_STD_ERROR=true
  • Reporter option: useStdError=true

Record hook failures

Record failures for hooks such as before/after etc Please note this will probably be made default in the next major version

  • Environment variable: RECORD_HOOK_FAILURES=true
  • Reporter option: recordHookFailures=true

Display Ignored tests as ignored

Display skip tests as ignored

  • Environment variable: DISPLAY_IGNORED_AS_IGNORED=true
  • Reporter option: displayIgnoredAsIgnored=true

Ignore hooks with title contains some text

This option should be used in pair with recordHookFailures. It allows you to skip reporting of hooks containing some word. Including root hooks.

  • Environment variable: IGNORE_HOOK_WITH_NAME=HookNoReporting
  • Reporter option: ignoreHookWithName=HookNoReporting

Example: mocha test --reporter mocha-teamcity-reporter --reporter-options recordHookFailures --reporter-options ignoreHookWithName=HookNoReporting

For root hooks defined the following way:

exports.mochaHooks = () => {
 return {
  beforeEach: [
   function beforeEachRootHookNoReporting() {
    assert.strictEqual(1, 1);
   }
  ],
  afterEach: [
   function afterEachRoot() {
    assert.strictEqual(1, 1);
   },
  ]
 };
};

beforeEach hook beforeEachRootHookNoReporting() will not be reported as testStarted. But hook afterEachRoot() will be reported:

Show diff between expected and actual values

This will allow a hyperlink to appear to compare actual vs expected Please note this requires the error thrown in mocha to have the properties actual and expected. For example an assertionError has this

  • Environment variable: ACTUAL_VS_EXPECTED=true
  • Reporter option: actualVsExpected=true

This will be shown in teamcity like this:

AssertionError [ERR_ASSERTION]: 2 == 1
     at Context.<anonymous> (test/test_data/simple.js:11:11)
 ======= Failed test run #10 ==========
 Show diff between expected and actual values

Setting options

  • Set with reporter-options:

mocha test --reporter mocha-teamcity-reporter --reporter-options topLevelSuite=top-level-suite-name mocha test --reporter mocha-teamcity-reporter --reporter-options useStdError=true mocha test --reporter mocha-teamcity-reporter --reporter-options useStdError=true

  • Set with environment variable

MOCHA_TEAMCITY_TOP_LEVEL_SUITE='top-level-suite-name' mocha test --reporter mocha-teamcity-reporter

Multiple reporters

This is not supported out of the box by this plugin but have a look at the following:

View on live Teamcity

Contributions

  • Always Welcome
  • Would prefer if customisation is added it is controlled via mocha options or environment variables
  • Only requirement is for code to pass linting and functional tests

Run example test in project

mocha test/test_data/simple.js --reporter mocha-teamcity-reporter or npm run test-teamcity-example

Reference Information

https://confluence.jetbrains.com/display/TCD10/Build+Script+Interaction+with+TeamCity

changelog

4.2.0 / 2021-11-21

  • Solve # Issue 65 "Pending tests are displayed as Passed in teamcity UI" by PR #66 (@fernyb )
    • Solved by producing correct output as per teamcity documentation, functionality can be disabled by reporter option displayIgnoredAsIgnored

4.1.0 / 2021-05-30

  • Solve # Issue 61 "Failed tests appears as successful in TeamCity" by PR #63 (@DJ-Glock)
    • Solved by adding postfix _hook for flowId for hooks. FlowIds will never intersect.

4.0.0 / 2021-05-03

  • Breaking: Only supported on node.js 6 and above
  • Breaking: Only Mocha version 6 and above is now supported
    • Please remain on mocha-teamcity-reporter@3 if this is an issue
  • New reporter option ignoreHookWithName to skip reporting for hooks with title containing some word (@DJ-Glock)
  • Implement 'hook end' event (@DJ-GLock)
  • General maintenance and tidy up (@DJ-Glock)

3.0.0 / 2019-01-21

  • Change mocha to peer dependency
  • Support mocha version 6
  • Breaking: focus on only support node.js environments (Please )
  • Breaking: Remove phantomJs support only supports environments which have require
  • Potential Breaking: Remove Redundant top level mocha.suite
  • Drop the duration on messages if mocha returns undefined/null (for example skipped test) TeamCity will then use received timestamps to calculate duration
  • Support Show diff between expected and actual values

TODO comparisonFailure service message attributes

2.5.2 / 2019-01-21

  • Restrict mocha dependency to less than 6 due to compatibility issues

2.5.1 / 2018-09-27

  • Vuejs/Webpack compatibility, solves #45

2.5.0 / 2018-09-18

  • Add test in hook option, solves #35

2.4.0 / 2018-04-18

  • Add Browser Support back in by use of separate file, solves #41

2.3.0 / 2018-04-18

  • Add use stdError option solves #31

2.2.2 / 2018-02-27

  • Fix issue #39
  • Add real teamcity tests

2.2.1 / 2018-02-06

  • Merge pull request #38 reporterOptions are optional from chge/master
  • Add a test to catch above PR

2.2.0 / 2018-02-05

  • Maintenance Update, Merged forked branch back into master branch by Travis
  • Breaking change, supports node 4+ only
  • Support flowId's
    • Why use flowIds? Flow tracking is necessary, for example, to distinguish separate processes running in parallel
    • This defaults to process.pid, so it works with concurrent task runners (Gulp/Grunt etc)
  • Other small bug fixes
  • Functional tests

2.1.0 / 2017-10-19

  • add top level suite option (@davidmfoley)

2.0.1 / 2017-08-22

  • Bug Fixes

2.0.0 / 2017-08-20

  • Add FlowId
  • Minimum nodejs engine of 4+
  • Add some eslint magic

1.1.0 / 2016-08-24

  • Support running in the browser with Mocha without require.js

1.0.1 / 2016-07-18

  • Remove escape sequences from reporting
  • Fix NaN duration

1.0.0 / 2015-09-30

  • Merge pull request #14 from debitoor/master
  • add err.stack to details of error
  • Merge pull request #8 from bdefore/master
  • Update teamcity.js
  • Merge pull request #6 from pandell/mocha-phantomjs