From 29ca556c7a8ee9d735445c3f7c0d239af0da8c0a Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Mon, 7 Mar 2016 22:22:03 +0100 Subject: [PATCH] Fix for test name to contain parent suite(s) names. This fixes issue reported in #62 where test names doesn't include parent test suites in the generated report. Also created minimal unit test setup. --- .travis.yml | 4 +-- gruntfile.js | 17 +++++++-- index.js | 12 +++++-- package.json | 10 +++++- test/.eslintrc | 5 +++ test/reporter.spec.js | 83 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 test/.eslintrc create mode 100644 test/reporter.spec.js diff --git a/.travis.yml b/.travis.yml index b83af3c..20c52a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,10 @@ node_js: - "0.12" - "0.10" + # Make sure we have new NPM. before_install: - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start - npm install -g npm - npm config set loglevel warn - -before_script: - - npm install -g grunt-cli diff --git a/gruntfile.js b/gruntfile.js index 71ff3d7..9290c5d 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -43,14 +43,27 @@ module.exports = function (grunt) { eslint: { target: [ 'index.js', - 'gruntfile.js' + 'gruntfile.js', + 'test/*.js' ] + }, + simplemocha: { + options: { + ui: 'bdd', + reporter: 'dot' + }, + unit: { + src: [ + 'test/*.spec.js' + ] + } } }) require('load-grunt-tasks')(grunt) - grunt.registerTask('default', ['eslint']) + grunt.registerTask('test', ['simplemocha']) + grunt.registerTask('default', ['eslint', 'test']) grunt.registerTask('release', 'Bump the version and publish to NPM.', function (type) { grunt.task.run([ diff --git a/index.js b/index.js index 767646d..bd65d7e 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,12 @@ var os = require('os') var path = require('path') var fs = require('fs') var builder = require('xmlbuilder') +var pathIsAbsolute = require('path-is-absolute') + +// concatenate test suite(s) and test description by default +function defaultNameFormatter (browser, result) { + return result.suite.join(' ') + ' ' + result.description +} var JUnitReporter = function (baseReporterDecorator, config, logger, helper, formatError) { var log = logger.create('reporter.junit') @@ -10,7 +16,7 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for var outputDir = reporterConfig.outputDir var outputFile = reporterConfig.outputFile var useBrowserName = reporterConfig.useBrowserName - var nameFormatter = reporterConfig.nameFormatter + var nameFormatter = reporterConfig.nameFormatter || defaultNameFormatter var classNameFormatter = reporterConfig.classNameFormatter var suites @@ -52,7 +58,7 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for var writeXmlForBrowser = function (browser) { var safeBrowserName = browser.name.replace(/ /g, '_') var newOutputFile - if (path.isAbsolute(outputFile)) { + if (pathIsAbsolute(outputFile)) { newOutputFile = outputFile } else if (outputFile != null) { var dir = useBrowserName ? path.join(outputDir, safeBrowserName) @@ -127,7 +133,7 @@ var JUnitReporter = function (baseReporterDecorator, config, logger, helper, for this.specSuccess = this.specSkipped = this.specFailure = function (browser, result) { var spec = suites[browser.id].ele('testcase', { - name: typeof nameFormatter === 'function' ? nameFormatter(browser, result) : result.description, + name: nameFormatter(browser, result), time: ((result.time || 0) / 1000), classname: (typeof classNameFormatter === 'function' ? classNameFormatter : getClassName)(browser, result) }) diff --git a/package.json b/package.json index 385a791..d47c594 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ ], "author": "Vojta Jina ", "dependencies": { + "path-is-absolute": "^1.0.0", "xmlbuilder": "3.1.0" }, "peerDependencies": { @@ -27,16 +28,23 @@ }, "license": "MIT", "devDependencies": { + "chai": "^3.5.0", "eslint": "^1.2.1", "eslint-config-standard": "^4.1.0", "eslint-plugin-standard": "^1.3.1", "grunt": "^0.4.1", "grunt-bump": "^0.5.0", + "grunt-cli": "^0.1.13", "grunt-conventional-changelog": "^4.1.0", "grunt-conventional-github-releaser": "^0.4.0", "grunt-eslint": "^17.1.0", "grunt-npm": "^0.0.2", - "load-grunt-tasks": "^3.2.0" + "grunt-simple-mocha": "^0.4.1", + "load-grunt-tasks": "^3.2.0", + "mocha": "^2.4.5", + "proxyquire": "^1.7.4", + "sinon": "^1.17.3", + "sinon-chai": "^2.8.0" }, "contributors": [ "dignifiedquire ", diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 0000000..7eeefc3 --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,5 @@ +{ + "env": { + "mocha": true + } +} diff --git a/test/reporter.spec.js b/test/reporter.spec.js new file mode 100644 index 0000000..be8c26a --- /dev/null +++ b/test/reporter.spec.js @@ -0,0 +1,83 @@ +'use strict' + +var chai = require('chai') +var expect = require('chai').expect +var sinon = require('sinon') +var proxyquire = require('proxyquire') + +chai.use(require('sinon-chai')) + +function noop () {} + +var fakeLogger = { + create: noop +} + +var fakeHelper = { + normalizeWinPath: noop, + mkdirIfNotExists: sinon.stub().yields() +} + +var fakeConfig = { + basePath: __dirname, + junitReporter: { + outputFile: '' + } +} + +var fakeBaseReporterDecorator = noop + +describe('JUnit reporter', function () { + var reporterModule + var reporter + + var fakeFs + + beforeEach(function () { + fakeFs = { + writeFile: sinon.spy() + } + + reporterModule = proxyquire('..', { + fs: fakeFs + }) + }) + + beforeEach(function () { + reporter = new reporterModule['reporter:junit'][1](fakeBaseReporterDecorator, fakeConfig, fakeLogger, fakeHelper) + }) + + it('should include parent suite names in generated test names', function () { + var fakeBrowser = { + id: 'Android_4_1_2', + name: 'Android', + fullName: 'Android 4.1.2', + lastResult: { + error: false, + total: 1, + failed: 0, + netTime: 10 * 1000 + } + } + + var fakeResult = { + suite: [ + 'Sender', + 'using it', + 'get request' + ], + description: 'should not fail', + log: [] + } + + reporter.onRunStart([ fakeBrowser ]) + reporter.specSuccess(fakeBrowser, fakeResult) + reporter.onBrowserComplete(fakeBrowser) + reporter.onRunComplete() + + expect(fakeFs.writeFile).to.have.been.called + + var writtenXml = fakeFs.writeFile.firstCall.args[1] + expect(writtenXml).to.have.string('testcase name="Sender using it get request should not fail"') + }) +})