Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lessons Learned with Grunt and Jasmine

Lessons Learned with Grunt and Jasmine

My deck from Tamedia:TX

Ben Green

June 25, 2015
Tweet

More Decks by Ben Green

Other Decks in Programming

Transcript

  1. top-tips learned the hard way with grunt and jasmine (and

    not selenium) Automating the client-side
  2. Gruntfile.js module.exports = function(grunt) { 'use strict'; require('./configs/local_options.js').addLocalOptions(grunt); var build_files

    = grunt.file.readJSON('configs/build_file_list.json'); grunt.config.set('build_files_list', build_files.files); grunt.loadTasks('grunt-tasks'); };
  3. module.exports.addLocalOptions = function (grunt){ grunt.config.init({ options: { workingDirectory: 'build', sourceDirectory:

    'src', serverDirectory: '/Library/WebServer/Documents/webapp' } }); } local_options.js
  4. module.exports = function(grunt) { grunt.config('react', { files: { expand: true,

    cwd: '<%= options.workingDirectory %>/jsx', src: ['components.jsx'], dest: '<%= options.workingDirectory %>/js', ext: '.js' } }); grunt.loadNpmTasks('grunt-react'); }; grunt-configs/react.js
  5. module.exports = function(grunt) { grunt.registerTask('tc', 'testComponent: grunt tc --cx=myComponent', function()

    { var compo = grunt.option('cx'); if(compo) { grunt.config.set('component', compo); } else { grunt.log.writeln("no component specified. This might not work as you were hoping"); grunt.log.writeln("try again like this: grunt tc --cx=myComponent"); return false; } grunt.task.run('testComponent'); }); }; grunt-configs/alias_tasks.js
  6. module.exports = function(grunt) { grunt.registerTask('copyComponentFeed','get the right feed', function() {

    grunt.config.requires('options.workingDirectory'); var fs = require('fs'), sourcefile = 'src/components/' + grunt.config('component') + '/stub.json', wd = grunt.config.get('options.serverDirectory'), destination = wd + '/feed/component.json'; fs.writeFileSync(destination, fs.readFileSync(sourcefile)); }); }; grunt-configs/alias_tasks.js
  7. 1. describe("Twenty ", function() { 2. it("exists as a global

    object", function() { 3. expect(Twenty).toEqual(jasmine.any(Object)); 4. }); 5. describe("Twenty.core", function() { 6. it("exists", function() { 7. expect(Twenty.core).toEqual(jasmine.any(Object)); 8. }); 9. )}; 10.}); jasmine’s three main functions
  8. matchers. collect ‘em all .toBe() .toEqual() .toMatch() .toBeDefined() .toBeUndefined() .toBeNull()

    .toBeTruthy() .toBeFalsy() .toContain() .toBeLessThan() .toBeGreaterThan() .toBeNaN() .toBeCloseTo() .toThrow() x2 .not
  9. best-ish practices • don’t comment out tests - delete them

    • check them in to git • test the interface. Don’t get hung up on every path through the application. • try to make your code better through testing
  10. but