Save 37% off PRO during our Black Friday Sale! »

Better JavaScript with Jasmine

Better JavaScript with Jasmine

Quick introduction into the Jasmine testing framework for JavaScript, geared towards the rubyist.

D28499b3a2778ce8a1075c51f826cf01?s=128

Todd Kaufman

July 27, 2012
Tweet

Transcript

  1. Better Javascript with Jasmine

  2. I don’t test my JavaScript because ______________

  3. “Semicolons are for losers”

  4. “It can’t be automated”

  5. “I already test it with Cucumber”

  6. “I don’t have any REAL BUSINESS LOGIC in my JavaScript”

  7. “It’s too hard”

  8. “It changes too often”

  9. “I’m lazy”

  10. “It’s not worth the effort”

  11. I have [ ] more [ ] less JavaScript in

    my code than I used to.
  12. I have [ X ] more [ ] less JavaScript

    in my code than I used to.
  13. None
  14. Rubyists need not fear

  15. the authors may have seen RSpec before

  16. describe RomanNumeral do it "should convert I to 1" do

    subject = RomanNumeral.new("I") subject.to_i.should eq(1) end end
  17. describe("RomanNumeral", function() { it("returns 1 for I", function() { var

    subject = new RomanNumeral("I"); return expect(subject.toI()).toBe(1); }); });
  18. basics

  19. RSpec Jasmine describe User do describe(“User”, function() it “saves stuff”

    do it (“saves stuff”, function() value.should expect(value)
  20. matchers

  21. RSpec Jasmine name.should eq(“Todd”) expect(name).toBe(“Todd”); (3*3).should_not == 8 expect(3*3).not.toBe(8); be,

    eql, equal, ==, === toEqual, toBe
  22. None
  23. None
  24. RSpec Jasmine 7.should be_true expect(7).toBeTruthy(); (7).should_not be(true) expect(7).not.toBe(true); nil.should be_false

    expect(null).toBeFalsy();
  25. RSpec Jasmine [‘a’,’b’].should include(‘a’) expect([3,4]).toContain(4); (1.05).should be_close(1, 0.06) expect(1.05).toBeCloseTo(1, 0)

    nil.should be_false expect(null).toBeFalsy();
  26. toBeDefined / toBeUndefined toMatch(/regex/) toThrow custom matchers as well

  27. beforeEach afterEach

  28. xdescribe xit

  29. None
  30. test doubles

  31. None
  32. spies

  33. None
  34. None
  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. stubs

  42. None
  43. jasmine-stealth

  44. None
  45. fakes

  46. None
  47. mocks

  48. ease of use

  49. None
  50. None
  51. None
  52. but, semicolons are still for losers

  53. describe("RomanNumeral", function() { it("returns 1 for I", function() { var

    subject = new RomanNumeral("I"); return expect(subject.toI()).toBe(1); }); });
  54. describe "RomanNumeral", -> it "returns 1 for I", -> subject

    = new RomanNumeral("I") expect(subject.toI()).toBe 1
  55. Great, so how do I run this stuff?

  56. www.tryjasmine.com

  57. None
  58. Jasmine standalone

  59. https://github.com/ pivotal/jasmine/ downloads

  60. None
  61. Jasmine within Rails

  62. gem install jasmine

  63. jasmine-headless-webkit

  64. None
  65. None
  66. gem install jasmine-rails

  67. Available for other enviroments as well

  68. what now?

  69. it’s not that hard

  70. it is important

  71. you have no excuses

  72. Thanks! @toddkaufman @testdouble