Skip to content

Commit

Permalink
Merge pull request #54 from martinblech/master
Browse files Browse the repository at this point in the history
unit tests for rjs+almond
  • Loading branch information
mklabs committed Aug 1, 2012
2 parents d1b3f8f + d0cf594 commit 5fd335b
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 38 deletions.
3 changes: 3 additions & 0 deletions tasks/rjs.js
Expand Up @@ -22,6 +22,9 @@ module.exports = function(grunt) {
if (options.almond === true) {
grunt.log.ok('Including almond.js');
// resolve almond path
if (typeof options.paths === 'undefined') {
options.paths = {};
}
options.paths.almond = require.resolve('almond').replace('.js', '');
// include almond to each module
options.modules.forEach(function (module, idx) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/rjs-almond/expected/alpha.js
@@ -0,0 +1 @@
define(["require","exports","module"],function(a,b){b.name="alpha"})
1 change: 1 addition & 0 deletions test/fixtures/rjs-almond/expected/beta.js
@@ -0,0 +1 @@
define(["./sub/betaSub"],function(a){return{name:"beta",subName:a.name}})
1 change: 1 addition & 0 deletions test/fixtures/rjs-almond/expected/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/rjs-almond/expected/plugins.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/rjs-almond/expected/sub/betaSub.js
@@ -0,0 +1 @@
define({name:"betaSubName"})
95 changes: 95 additions & 0 deletions test/fixtures/rjs-almond/grunt.js
@@ -0,0 +1,95 @@
/*global module:false*/
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
// the staging directory used during the process
staging: 'intermediate',
// final build output
output: 'publish',
// filter any files matching one of the below pattern during mkdirs task
// the pattern in the .gitignore file should work too.
exclude: '.git* build/** node_modules/** grunt.js package.json *.md'.split(' '),
mkdirs: {
staging: '<config:exclude>'
},
// concat css/**/*.css files, inline @import, output a single minified css
css: {
'css/style.css': ['css/**/*.css']
},
// Renames JS/CSS to prepend a hash of their contents for easier
// versioning
rev: {
js: 'js/**/*.js',
css: 'css/**/*.css',
img: 'img/**'
},
// update references in html to revved files
usemin: {
files: ['**/*.html']
},
// html minification
html: '<config:usemin>',
// Optimizes JPGs and PNGs (with jpegtran & optipng)
img: {
dist: '<config:rev.img>'
},
watch: {
files: '<config:lint.files>',
tasks: 'lint qunit'
},
lint: {
files: ['grunt.js', 'js/**/*.js', 'test/**/*.js']
},
qunit: {
files: ['test/**/*.html']
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true
},
globals: {
jQuery: true
}
},
rjs: {
almond: true,
modules: [{
name: 'main',
}],
dir: 'js',
appDir: 'js',
baseUrl: './',
pragmas: {
doExclude: true
},
skipModuleInsertion: false,
optimizeAllPluginResources: true,
findNestedDependencies: true
}
});


// in rjs setup, the concat and min task are overriden to use rjs optimizr
grunt.renameTask('concat', '_concat').registerTask('concat', 'rjs (noop)', function() {
grunt.log.writeln('the concat in rjs setup is a noop, rjs optimizer somewhat replace js concatenation');
});
grunt.renameTask('min', '_min').registerTask('min', 'rjs');


// uncomment this line if you're using the build script as a grunt plugin
// it should be installed locally, even better if put in your package.json's
// dependency
//
grunt.loadNpmTasks(require('path').join(__dirname, '../'));
};
3 changes: 3 additions & 0 deletions test/fixtures/rjs-almond/sample/alpha.js
@@ -0,0 +1,3 @@
define(function(require, exports) {
exports.name = 'alpha';
});
6 changes: 6 additions & 0 deletions test/fixtures/rjs-almond/sample/beta.js
@@ -0,0 +1,6 @@
define(['./sub/betaSub'], function (betaSub) {
return {
name: 'beta',
subName: betaSub.name
};
});
3 changes: 3 additions & 0 deletions test/fixtures/rjs-almond/sample/main.js
@@ -0,0 +1,3 @@
require(['alpha', 'beta'], function (alpha, beta) {
});

3 changes: 3 additions & 0 deletions test/fixtures/rjs-almond/sample/sub/betaSub.js
@@ -0,0 +1,3 @@
define({
name: 'betaSubName'
});
7 changes: 0 additions & 7 deletions test/fixtures/rjs/expected/build.txt

This file was deleted.

8 changes: 8 additions & 0 deletions test/helpers/index.js
Expand Up @@ -247,3 +247,11 @@ helpers.copyFile = function(src, dst, cb) {
fs.createReadStream(src).pipe(fs.createWriteStream(dst)).on('close', cb);
});
};

// a simple file remove helper
helpers.rmFile = function(path, cb) {
if (!cb) {
return fs.unlinkSync(path);
}
fs.unlink(path, cb);
}
71 changes: 40 additions & 31 deletions test/tasks/test-rjs.js
Expand Up @@ -4,53 +4,62 @@

var helpers = require('../helpers');

describe("RJS task", function() {
var describeTask = function(taskName, fixturePath) {

before(function(done) {
helpers.before(function(err) {
if(err) return done(err);
describe(taskName, function() {

helpers.copyFile('test/fixtures/rjs/grunt.js', '.test/grunt.js', function(err) {
before(function(done) {
helpers.before(function(err) {
if(err) return done(err);
var files = [
'rjs/sample/alpha.js',
'rjs/sample/beta.js',
'rjs/sample/main.js'
];

helpers.copy(files, '.test/js', function(err) {
helpers.copyFile(fixturePath + 'grunt.js', '.test/grunt.js', function(err) {
if(err) return done(err);
helpers.copyFile('test/fixtures/rjs/sample/sub/betaSub.js', '.test/js/sub/betaSub.js', done);
var files = [
'rjs/sample/alpha.js',
'rjs/sample/beta.js',
'rjs/sample/main.js'
];

helpers.copy(files, '.test/js', function(err) {
if(err) return done(err);
helpers.copyFile(fixturePath + 'sample/sub/betaSub.js', '.test/js/sub/betaSub.js', done);
});
});
});
});
});

describe("As a build script user I want to be able to run the rjs task So that I can see the rjs task in action", function() {
describe("As a build script user I want to be able to run the rjs task So that I can see the rjs task in action", function() {

describe("rjs task", function() {
describe("rjs task", function() {

// XXX the rjs task takes up to 8s on my machine with almost nothing to
// process (sample files are really basic...)
// figure out what's going wrong
it("Given I run the 'rjs' task", function(done) {
// runt the rjs task
helpers.run('rjs', done);
});
// XXX the rjs task takes up to 8s on my machine with almost nothing to
// process (sample files are really basic...)
// figure out what's going wrong
it("Given I run the 'rjs' task", function(done) {
// runt the rjs task
helpers.run('rjs', done);
});

it("When the script ends", function(done) {
// not doing anything particularly usefull in this step but the hook is here
// if we need to
done();
});
it("When the script ends", function(done) {
// remove build.txt as it contains system-dependent almond path
helpers.rmFile('.test/js/build.txt', function(err) {
if (err) return done(err);
return done();
});
});

it("Then '.test/js' should be the same as " + fixturePath + "expected/'", function(done) {
helpers.assertDir('.test/js', fixturePath + 'expected/');
done();
});

it("Then '.test/js' should be the same as 'test/fixtures/rjs/expected/'", function(done) {
helpers.assertDir('.test/js', 'test/fixtures/rjs/expected/');
done();
});

});

});

});
};

describeTask("RJS task", 'test/fixtures/rjs/');
describeTask("RJS task (almond)", 'test/fixtures/rjs-almond/');

0 comments on commit 5fd335b

Please sign in to comment.