Socialify

Folder ..

Viewing sendToCoveralls.js
80 lines (67 loc) • 2.2 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';

const should = require('should');
const request = require('request');
const sinon = require('sinon');
const logDriver = require('log-driver');
const index = require('..');

logDriver({ level: false });

describe('sendToCoveralls', () => {
  let realCoverallsHost;
  beforeEach(() => {
    realCoverallsHost = process.env.COVERALLS_ENDPOINT;
  });

  afterEach(() => {
    sinon.restore();
    if (realCoverallsHost !== undefined) {
      process.env.COVERALLS_ENDPOINT = realCoverallsHost;
    } else {
      delete process.env.COVERALLS_ENDPOINT;
    }
  });

  it('passes on the correct params to request.post', done => {
    sinon.stub(request, 'post').callsFake((obj, cb) => {
      obj.url.should.equal('https://coveralls.io/api/v1/jobs');
      obj.form.should.eql({ json: '{"some":"obj"}' });
      cb('err', 'response', 'body');
    });

    const obj = { 'some': 'obj' };

    index.sendToCoveralls(obj, (err, response, body) => {
      err.should.equal('err');
      response.should.equal('response');
      body.should.equal('body');
      done();
    });
  });

  it('allows sending to enterprise url', done => {
    process.env.COVERALLS_ENDPOINT = 'https://coveralls-ubuntu.domain.com';
    sinon.stub(request, 'post').callsFake((obj, cb) => {
      obj.url.should.equal('https://coveralls-ubuntu.domain.com/api/v1/jobs');
      obj.form.should.eql({ json: '{"some":"obj"}' });
      cb('err', 'response', 'body');
    });

    const obj = { 'some': 'obj' };
    index.sendToCoveralls(obj, (err, response, body) => {
      err.should.equal('err');
      response.should.equal('response');
      body.should.equal('body');
      done();
    });
  });
  it('writes output to stdout when --stdout is passed', done => {
    const obj = { 'some': 'obj' };

    // set up mock process.stdout.write temporarily
    const origStdoutWrite = process.stdout.write;
    process.stdout.write = function(string, ...args) {
      if (string === JSON.stringify(obj)) {
        process.stdout.write = origStdoutWrite;
        return done();
      }

      origStdoutWrite.apply(this, args);
    };

    index.options.stdout = true;

    index.sendToCoveralls(obj, (err, response) => {
      should.not.exist(err);
      response.statusCode.should.equal(200);
    });
  });
});