UNPKG

6.2 kBJavaScriptView Raw
1'use strict';
2
3/*global __dirname, process, require, describe, it, beforeEach, afterEach */
4var A = require('chai').assert,
5 mockery = require('mockery'),
6 sinon = require('sinon'),
7 path = require('path'),
8 shellescape = require('shell-escape'),
9 mocks = {
10 mkdir: sinon.stub(),
11 exec: sinon.stub(),
12 exit: sinon.stub(),
13 config: {},
14 env: {}
15 },
16 node_modules = path.join(__dirname, '..', 'node_modules'),
17 nycPath = escape(path.join(node_modules, '.bin', 'nyc')),
18 specXunitPath = escape(require.resolve('spec-xunit-file')),
19 mochaPath = escape(path.join(node_modules, '.bin', 'mocha')),
20 _mochaPath = escape(path.join(node_modules, '.bin', '_mocha')),
21 coverage = escape(path.join(process.cwd(), 'artifacts', 'coverage'));
22
23
24A.equalObject = function (a, b, message) {
25 A.equal(JSON.stringify(a, null, 4), JSON.stringify(b, null, 4), message);
26};
27
28function escape(directory){
29 return shellescape([directory]);
30}
31
32describe('Jenkins Mocha Test Case', function () {
33 // Bump timeout to 5 seconds
34 this.timeout(5000);
35
36 beforeEach(function () {
37 Object.keys(mocks).forEach(function (key) {
38 if (typeof mocks[key] === 'function') {
39 mocks[key] = sinon.stub();
40 } else {
41 mocks[key] = {};
42 }
43 });
44
45 mockery.enable({
46 warnOnUnregistered: false,
47 useCleanCache: true
48 });
49 mockery.registerMock('shelljs', mocks);
50 });
51
52 afterEach(function () {
53 mockery.deregisterAll();
54 mockery.disable();
55 });
56
57 function assertMkDirCallsAreReceived(){
58 // Check mkdirs
59 A.equalObject(mocks.mkdir.args[0], ['-p', path.join(process.cwd(), 'artifacts')], 'artifact dir was created');
60 A.equalObject(mocks.mkdir.args[1], ['-p', path.join(process.cwd(), 'artifacts', 'coverage')], 'coverage dir was created');
61 A.equalObject(mocks.mkdir.args[2], ['-p', path.join(process.cwd(), 'artifacts', 'test')], 'tests dir was created');
62 }
63
64 describe('jenkins', function () {
65 it('should run the right functions', function () {
66 mocks.exec.returns({
67 code: 0
68 });
69
70 // Run
71 require('../lib/jenkins')(['--foo', 'tests/*']);
72
73 assertMkDirCallsAreReceived();
74
75 // Check environment
76 A.equalObject(mocks.env, {
77 XUNIT_FILE: path.join(process.cwd(), 'artifacts', 'test', 'xunit.xml')
78 }, 'xunit file was set');
79
80 // Check exec
81 A.equalObject(mocks.exec.args[0], [
82 'node ' + nycPath + ' --reporter lcov --report-dir ' +
83 coverage +
84 ' -- node ' + _mochaPath + ' --reporter ' + specXunitPath + ' --colors --foo \'tests/*\''
85 ], 'mocha was called correctly');
86 });
87
88 it('should support a --no-colors options', function () {
89 mocks.exec.returns({
90 code: 0
91 });
92
93 // Run
94 require('../lib/jenkins')(['--foo', 'tests/*', '--no-colors']);
95
96 assertMkDirCallsAreReceived();
97
98 // Check environment
99 A.equalObject(mocks.env, {
100 XUNIT_FILE: path.join(process.cwd(), 'artifacts', 'test', 'xunit.xml')
101 }, 'xunit file was set');
102
103 // Check exec
104 A.equalObject(mocks.exec.args[0], [
105 'node ' + nycPath + ' --reporter lcov --report-dir ' +
106 coverage +
107 ' -- node ' + _mochaPath + ' --reporter ' + specXunitPath + ' --foo \'tests/*\' --no-colors'
108 ], 'mocha was called correctly');
109 });
110
111 it('should support a --no-coverage option', function () {
112 mocks.exec.returns({
113 code: 0
114 });
115
116 // Run
117 require('../lib/jenkins')(['--foo', 'tests/*', '--no-coverage']);
118
119 assertMkDirCallsAreReceived();
120
121 // Check environment
122 A.equalObject(mocks.env, {
123 XUNIT_FILE: path.join(process.cwd(), 'artifacts', 'test', 'xunit.xml')
124 }, 'xunit file was set');
125
126 // Check exec
127 A.equalObject(mocks.exec.args[0], [
128 'node ' + mochaPath + ' --reporter ' + specXunitPath + ' --colors --foo \'tests/*\''
129 ], 'mocha was called correctly');
130 });
131
132 it('should support a --cobertura option', function () {
133 mocks.exec.returns({
134 code: 0
135 });
136
137 // Run
138 require('../lib/jenkins')(['--foo', 'tests/*', '--cobertura']);
139
140 assertMkDirCallsAreReceived();
141
142 // Check environment
143 A.equalObject(mocks.env, {
144 XUNIT_FILE: path.join(process.cwd(), 'artifacts', 'test', 'xunit.xml')
145 }, 'xunit file was set');
146
147 // Check exec
148 A.equalObject(mocks.exec.args[0], [
149 'node ' + nycPath + ' --reporter cobertura --report-dir ' +
150 coverage +
151 ' -- node ' + _mochaPath + ' --reporter ' + specXunitPath + ' --colors --foo \'tests/*\''
152 ], 'mocha was called correctly');
153 });
154
155 it('should support passing Node args', function () {
156 mocks.exec.returns({
157 code: 0
158 });
159 mocks.env.NODE_ARGS = '--flop=blop';
160
161 // Run
162 require('../lib/jenkins')(['--foo', 'tests/*', '--no-colors']);
163
164 assertMkDirCallsAreReceived();
165
166 // Check environment
167 A.equalObject(mocks.env, {
168 NODE_ARGS: '--flop=blop',
169 XUNIT_FILE: path.join(process.cwd(), 'artifacts', 'test', 'xunit.xml')
170 }, 'xunit file was set');
171
172 // Check exec
173 A.equalObject(mocks.exec.args[0], [
174 'node --flop=blop ' + nycPath + ' --reporter lcov --report-dir ' +
175 coverage +
176 ' -- node --flop=blop ' + _mochaPath + ' --reporter ' + specXunitPath + ' --foo \'tests/*\' --no-colors'
177 ], 'mocha was called correctly');
178 });
179 });
180});