UNPKG

7.8 kBJavaScriptView Raw
1/**
2 * Gulp file.
3 */
4var fs = require("fs"),
5 _ = require("lodash"),
6 gulp = require("gulp"),
7 jshint = require("gulp-jshint"),
8 // TODO: Switch back to https://github.com/lazd/gulp-karma when
9 // https://github.com/lazd/gulp-karma/pull/22 is merged.
10 karma = require("gulp-karma"),
11 mocha = require("gulp-mocha"),
12 jade = require("gulp-jade"),
13 rename = require("gulp-rename"),
14 mdox = require("gulp-mdox"),
15 rimraf = require("gulp-rimraf");
16
17// ----------------------------------------------------------------------------
18// Globals
19// ----------------------------------------------------------------------------
20// Sauce labs environments.
21var SAUCE_ENVS = {
22 /*jshint camelcase:false */
23 // Already tested in Travis.
24 // sl_firefox: {
25 // base: "SauceLabs",
26 // browserName: "firefox"
27 // },
28 sl_chrome: {
29 base: "SauceLabs",
30 browserName: "chrome"
31 },
32 sl_safari: {
33 base: "SauceLabs",
34 browserName: "safari",
35 platform: "OS X 10.9"
36 },
37 sl_ie_9: {
38 base: "SauceLabs",
39 browserName: "internet explorer",
40 platform: "Windows 7",
41 version: "9"
42 },
43 sl_ie_10: {
44 base: "SauceLabs",
45 browserName: "internet explorer",
46 platform: "Windows 7",
47 version: "10"
48 },
49 sl_ie_11: {
50 base: "SauceLabs",
51 browserName: "internet explorer",
52 platform: "Windows 7",
53 version: "11"
54 }
55 /*jshint camelcase:true */
56};
57
58// SauceLabs tag.
59var SAUCE_BRANCH = process.env.TRAVIS_BRANCH || "local";
60var SAUCE_TAG = process.env.SAUCE_USERNAME + "@" + SAUCE_BRANCH;
61
62// Karma coverage.
63var KARMA_COV = {
64 reporters: ["mocha", "coverage"],
65 preprocessors: {
66 "chai-jq.js": ["coverage"]
67 },
68 coverageReporter: {
69 reporters: [
70 { type: "json", file: "coverage.json" },
71 { type: "lcov" }
72 ],
73 dir: "coverage/"
74 }
75};
76
77// ----------------------------------------------------------------------------
78// Helpers
79// ----------------------------------------------------------------------------
80// Strip comments from JsHint JSON files (naive).
81var _jshintCfg = function (name) {
82 var raw = fs.readFileSync(name).toString();
83 return JSON.parse(raw.replace(/\/\/.*\n/g, ""));
84};
85
86// ----------------------------------------------------------------------------
87// JsHint
88// ----------------------------------------------------------------------------
89gulp.task("jshint:frontend", function () {
90 gulp
91 .src([
92 "test/js/adapters/karma.js",
93 "*.js",
94 "!gulpfile.js"
95 ])
96 .pipe(jshint(_jshintCfg(".jshintrc-frontend.json")))
97 .pipe(jshint.reporter("default"))
98 .pipe(jshint.reporter("fail"));
99});
100
101gulp.task("jshint:test", function () {
102 gulp
103 .src([
104 "test/js/spec/**/*.js"
105 ])
106 .pipe(jshint(_.merge(_jshintCfg(".jshintrc-frontend.json"), {
107 expr: true
108 })))
109 .pipe(jshint.reporter("default"))
110 .pipe(jshint.reporter("fail"));
111});
112
113gulp.task("jshint:backend", function () {
114 gulp
115 .src([
116 "*.js",
117 "tasks/**/*.js",
118 "test/js/adapters/node.js"
119 ])
120 .pipe(jshint(_jshintCfg(".jshintrc-backend.json")))
121 .pipe(jshint.reporter("default"))
122 .pipe(jshint.reporter("fail"));
123});
124
125gulp.task("jshint", ["jshint:frontend", "jshint:test", "jshint:backend"]);
126
127// ----------------------------------------------------------------------------
128// Test - Frontend
129// ----------------------------------------------------------------------------
130// Use `node_modules` Phantom
131process.env.PHANTOMJS_BIN = "./node_modules/.bin/phantomjs";
132
133// Test wrapper.
134var testFrontend = function () {
135 var files = [
136 // Libraries
137 "test/js/lib/sinon.js",
138 "test/js/lib/jquery.js",
139 "test/js/lib/chai.js",
140 "chai-jq.js",
141
142 // Test setup,
143 "test/adapters/karma.js",
144
145 // Tests
146 "test/js/spec/chai-jq.spec.js"
147 ];
148
149 var opts = _.extend.apply(_, [{
150 frameworks: ["mocha"],
151 port: 9999,
152 reporters: ["mocha"],
153 client: {
154 mocha: {
155 ui: "bdd"
156 }
157 }
158 }].concat(_.toArray(arguments)));
159
160 return function () {
161 return gulp
162 .src(files)
163 .pipe(karma(opts))
164 .on("error", function (err) {
165 throw err;
166 });
167 };
168};
169
170gulp.task("test:frontend:dev", testFrontend({
171 singleRun: true,
172 browsers: ["PhantomJS"]
173}, KARMA_COV));
174
175gulp.task("test:frontend:ci", testFrontend({
176 singleRun: true,
177 browsers: ["PhantomJS", "Firefox"]
178}, KARMA_COV, {
179 reporters: ["mocha", "coverage", "coveralls"]
180}));
181
182gulp.task("test:frontend:sauce", testFrontend({
183 singleRun: true,
184 reporters: ["mocha", "saucelabs"],
185 sauceLabs: {
186 testName: "chai-jq - Frontend Unit Tests",
187 tags: [SAUCE_TAG],
188 public: "public"
189 },
190 // Timeouts: Allow "n" minutes before saying "good enough". See also:
191 // https://github.com/angular/angular.js/blob/master/karma-shared.conf.js
192 captureTimeout: 0, // Pass through to SL.
193 customLaunchers: SAUCE_ENVS,
194 browsers: Object.keys(SAUCE_ENVS)
195}));
196
197gulp.task("test:frontend:all", testFrontend({
198 port: 9998,
199 browsers: ["PhantomJS", "Firefox", "Chrome", "Safari"]
200}));
201
202// ----------------------------------------------------------------------------
203// Test - Backend
204// ----------------------------------------------------------------------------
205gulp.task("test:backend", function () {
206 gulp
207 .src("test/adapters/node.js")
208 .pipe(mocha({
209 ui: "bdd",
210 reporter: "spec"
211 }))
212 .on("error", function (err) {
213 throw err;
214 });
215});
216
217// ----------------------------------------------------------------------------
218// Docs
219// ----------------------------------------------------------------------------
220gulp.task("docs:api", function () {
221 gulp
222 .src("chai-jq.js")
223 .pipe(mdox({
224 src: "README.md",
225 name: "README.md",
226 start: "## Plugin API",
227 end: "## Contributions"
228 }))
229 .pipe(gulp.dest("./"));
230});
231
232gulp.task("templates", function () {
233 gulp
234 .src("_templates/**/*.jade")
235 .pipe(jade({
236 pretty: true
237 }))
238 .pipe(gulp.dest("./"));
239});
240
241// ----------------------------------------------------------------------------
242// Copy
243// ----------------------------------------------------------------------------
244gulp.task("copy", function () {
245 gulp
246 .src([
247 "bower_components/mocha/mocha.js",
248 "bower_components/mocha/mocha.css",
249 "bower_components/chai/chai.js",
250 "bower_components/jquery/dist/jquery.js",
251 "bower_components/requirejs/require.js",
252 "bower_components/pure/pure-min.css"
253 ])
254 .pipe(gulp.dest("./test/js/lib"));
255
256 gulp
257 .src([
258 "bower_components/sinon/index.js"
259 ])
260 .pipe(rename({
261 basename: "sinon"
262 }))
263 .pipe(gulp.dest("./test/js/lib"));
264});
265
266// ----------------------------------------------------------------------------
267// Watch
268// ----------------------------------------------------------------------------
269gulp.task("watch", function () {
270 gulp.watch([
271 "_templates/**/*.jade",
272 "*.md",
273 "chai-jq.js"
274 ], ["docs:api", "templates"]);
275});
276
277// ----------------------------------------------------------------------------
278// Clean
279// ----------------------------------------------------------------------------
280gulp.task("clean", function () {
281 gulp
282 .src([
283 "coverage"
284 ], { read: false })
285 .pipe(rimraf());
286});
287
288// ----------------------------------------------------------------------------
289// Aggregated Tasks
290// ----------------------------------------------------------------------------
291gulp.task("check:dev", ["jshint", "test:backend", "test:frontend:dev"]);
292gulp.task("check:ci", ["jshint", "test:backend", "test:frontend:ci"]);
293gulp.task("check:all", ["jshint", "test:backend", "test:frontend:all"]);
294gulp.task("check", ["check:dev"]);
295
296gulp.task("build", ["copy", "docs:api", "templates"]);
297
298gulp.task("default", ["clean", "check", "build"]);