{"id":1669,"date":"2017-05-30T10:00:00","date_gmt":"2017-05-30T08:00:00","guid":{"rendered":"https:\/\/demo.sidekick.be\/itenium\/javascript-testing-jasmine-syntax\/"},"modified":"2025-11-28T13:31:46","modified_gmt":"2025-11-28T12:31:46","slug":"javascript-testing-jasmine-syntax","status":"publish","type":"post","link":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/","title":{"rendered":"JavaScript Testing: Jasmine syntax"},"content":{"rendered":"<h1 id=\"basic-example\">Basic example<\/h1>\n<p>Time to start writing some real tests with Jasmine!<\/p>\n<pre><code class=\"language-js\">describe('basic example', () =&gt; {\n  beforeEach(() =&gt; {\n    this.result = 1;\n  });\n\n  it('can hardly fail', () =&gt; {\n    \/\/ toBe() uses ===\n    expect(this.result).toBe(1);\n\n    \/\/ For primitive types, toEqual\n    \/\/ behaves the same way as toBe\n    \/\/ toEqual goes further however\n    \/\/ by deeply comparing objects.\n    expect({a: {}}).toEqual({a: {}});\n    expect([0, {a: 1}]).toEqual([0, {a: 1}]);\n  });\n\n  afterAll(() =&gt; {});\n});\n<\/code><\/pre>\n<p>A <code>describe<\/code> groups one or more related tests together in a suite. Describes can be nested indefinitely and contain one <code>it<\/code> for each test in the suite.<\/p>\n<p><code>beforeAll<\/code> and <code>afterAll<\/code> runs once before\/after all tests in the containing describe are executed. <code>beforeEach<\/code> and <code>afterEach<\/code> runs once before\/after each test is executed.<\/p>\n<p>The <code>this<\/code> context is shared in the beforeX\/afterX functions and the <code>it<\/code>s, allowing you to setup\/teardown stuff and access them in your tests.<\/p>\n<h1 id=\"matchers\">Matchers<\/h1>\n<p>Set expectations with matchers. Usage and differences of <code>toBe<\/code> and <code>toEqual<\/code> are already covered in the basic example above. These are the other matchers:<\/p>\n<pre><code class=\"language-js\">it('can negate any matcher with .not', () =&gt; {\n  expect(1).not.toBe(0);\n});\n\nit('knows about regexes', () =&gt; {\n  expect('some bar').toMatch(\/bar$\/);\n  expect('some bar').toMatch('\\sbar');\n});\n\nit('can check numbers', () =&gt; {\n  expect(5).toBeLessThan(10); \/\/ .toBeLessThanOrEqual(5);\n  expect(5).toBeGreaterThan(1); \/\/ .toBeGreaterThanOrEqual(5);\n\n  const precision = 3;\n  expect(Math.PI).toBeCloseTo(3.142, precision);\n});\n\nit('has some special matchers', () =&gt; {\n  expect(1).toBeTruthy();\n  expect('').toBeFalsy();\n  expect().toBeUndefined();\n  \/\/ .toBeNull(); .toBeDefined(); .toBeNaN();\n  \/\/ .toBeNegativeInfinity(); .toBePositiveInfinity();\n  \/\/ .toHaveClass(domEl): contains the class\n  \/\/ .withContext('show this on failure');\n  \n\nit('gets funky with partial matchers', () =&gt; {\n  expect(['a', {b: {}}]).toContain({b: {}});\n  expect('some string').toContain('some');\n\n  expect({bar: 'baz', ack: 'yaye'}).toEqual(jasmine.objectContaining({bar: 'baz'}));\n  expect([1, 2, 3]).not.toEqual(jasmine.arrayContaining([1, 6]));\n  expect({foo: 'foobarbaz'}).toEqual({foo: jasmine.stringMatching('baz$')});\n\n  \/\/ jasmine.arrayWithExactContents()\n  \/\/ .empty(); .notEmpty();\n  \/\/ .truthy(); .falsy();\n});\n\nit('can check for errors', () =&gt; {\n  const foo = () =&gt; {\n    throw new TypeError('foo bar baz');\n  };\n\n  expect(foo).toThrow();\n\n  \/\/ Error message must be exact match in order to pass\n  expect(foo).toThrowError('foo bar baz');\n  expect(foo).toThrowError(\/bar\/);\n  expect(foo).toThrowError(TypeError);\n  expect(foo).toThrowError(TypeError, 'foo bar baz');\n\n  expect(foo).toThrowMatching(err =&gt; err.message.includes('baz'));\n});\n\nit('jasmine.any', () =&gt; {\n  expect({}).toEqual(jasmine.any(Object));\n  \/\/ Number, Boolean, Date, String, Array\n  \/\/ Or 'YourType'\n});\n\nit('jasmine.anything', () =&gt; {\n  expect(1).toEqual(jasmine.anything());\n});\n<\/code><\/pre>\n<p>The <a href=\"https:\/\/github.com\/jasmine\/jasmine\/blob\/main\/src\/core\/matchers\">matchers source<\/a> for if you\u2019d like to take a peek at the implementations.<\/p>\n<h1 id=\"workflow\">Workflow<\/h1>\n<p>If you are iterating over the same test(s) you can temporarily run these tests by prefixing one or more <code>describe<\/code> and\/or <code>it<\/code>s with an f. You can also execute a single file from the CLI <code>jasmine src\/someSpec.js<\/code>.<\/p>\n<p>Obviously you\u2019ll never want to do the opposite: exclude a test from the suite. But.. If you must, it is possible to temporarily disable a describe\/it by prefixing it with an x.<\/p>\n<pre><code class=\"language-js\">\/\/ f = focused\nfdescribe('only my its will run', () =&gt; {\n  it('which is me', () =&gt; {});\n  it('and me', () =&gt; {});\n  xit('but not me', () =&gt; {});\n});\n\nxit('I will not run, even if the fdescribe above is removed', () =&gt; {});\n<\/code><\/pre>\n<p>Other helpers available in an <code>it<\/code>:<\/p>\n<ul>\n<li><code>pending(string)<\/code>. An <code>it<\/code> where the second parameter is omitted is automatically pending.<\/li>\n<li><code>fail(string | Error)<\/code><\/li>\n<\/ul>\n<h1>Also in this series<\/h1>\n<ul>\n<li>Part 1: <a href=\"\/javascript\/javascript-testing-jasmine-getting-started\/\">JavaScript Testing: Getting started with Jasmine<\/a><\/li>\n<li>This Part: JavaScript Testing: Jasmine syntax<\/li>\n<li>Part 3: <a href=\"\/javascript\/javascript-testing-jasmine-spies\/\">JavaScript Testing: Jasmine Spies<\/a><\/li>\n<li>Part 4: <a href=\"\/javascript\/javascript-testing-jasmine-async\/\">JavaScript Testing: Jasmine async syntax<\/a><\/li>\n<li>Part 5: <a href=\"\/javascript\/javascript-testing-jasmine-customization\/\">JavaScript Testing: Jasmine customization<\/a><\/li>\n<li>Part 6: <a href=\"\/javascript\/javascript-testing-jasmine-plugins\/\">JavaScript Testing: Jasmine plugins<\/a><\/li>\n<li>Part 7: <a href=\"\/javascript\/javascript-testing-jasmine-to-jest\/\">JavaScript Testing: Jasmine to Jest<\/a><\/li>\n<\/ul>\n<h1>Extras<\/h1>\n<p><b>Stuff that came into being during the making of this post<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/itenium-be\/jasmine-tut\">The accompanying source on GitHub<\/a><\/li>\n<\/ul>\n<h1>Updates<\/h1>\n<ul>\n<li> 28 March 2023: Updated to Jasmine 4.3.0.<\/li>\n<\/ul>\n<h1> Package Versions <i class=\"fa fa-info-circle\" style=\"float: right; color: cornflowerblue; font-size: 28px\" title=\"Package versions used for this post and in the accompanying code on Github\"><\/i><\/h1>\n<div><a href=\"https:\/\/github.com\/jasmine\/jasmine\" class=\"github\"><img decoding=\"async\" src=\"https:\/\/img.shields.io\/github\/stars\/jasmine\/jasmine.svg?style=social&#038;label=Star\" \/><\/a><a href=\"https:\/\/github.com\/jasmine\/jasmine\">jasmine\/jasmine<\/a>: v4.3.0 <\/div>\n","protected":false},"excerpt":{"rendered":"<p>The basic example contains the general test suite structure and the two most used matchers <code>toBe()<\/code> (===) and <code>toEqual()<\/code> (deep compare), followed by all the other matchers that come out of the box.<\/p>\n<p>To finish some helpers for your workflow: how to only have certain tests run and how to exclude tests.<\/p>\n","protected":false},"author":1,"featured_media":2110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[21,6],"class_list":["post-1669","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-testing","tag-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Testing: Jasmine syntax - Itenium<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Testing: Jasmine syntax - Itenium\" \/>\n<meta property=\"og:description\" content=\"The basic example contains the general test suite structure and the two most used matchers toBe() (===) and toEqual() (deep compare), followed by all the other matchers that come out of the box.To finish some helpers for your workflow: how to only have certain tests run and how to exclude tests.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/\" \/>\n<meta property=\"og:site_name\" content=\"Itenium\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-30T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-28T12:31:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"360\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Usdevelop\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Usdevelop\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/\",\"name\":\"JavaScript Testing: Jasmine syntax - Itenium\",\"isPartOf\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg\",\"datePublished\":\"2017-05-30T08:00:00+00:00\",\"dateModified\":\"2025-11-28T12:31:46+00:00\",\"author\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\"},\"breadcrumb\":{\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg\",\"contentUrl\":\"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg\",\"width\":360,\"height\":300},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/demo.sidekick.be\/itenium\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Testing: Jasmine syntax\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#website\",\"url\":\"https:\/\/demo.sidekick.be\/itenium\/\",\"name\":\"Itenium\",\"description\":\"We build software. We build careers.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022\",\"name\":\"Usdevelop\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g\",\"caption\":\"Usdevelop\"},\"sameAs\":[\"https:\/\/demo.sidekick.be\/itenium\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Testing: Jasmine syntax - Itenium","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"JavaScript Testing: Jasmine syntax - Itenium","og_description":"The basic example contains the general test suite structure and the two most used matchers toBe() (===) and toEqual() (deep compare), followed by all the other matchers that come out of the box.To finish some helpers for your workflow: how to only have certain tests run and how to exclude tests.","og_url":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/","og_site_name":"Itenium","article_published_time":"2017-05-30T08:00:00+00:00","article_modified_time":"2025-11-28T12:31:46+00:00","og_image":[{"width":360,"height":300,"url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg","type":"image\/jpeg"}],"author":"Usdevelop","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Usdevelop","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/","url":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/","name":"JavaScript Testing: Jasmine syntax - Itenium","isPartOf":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#website"},"primaryImageOfPage":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage"},"image":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage"},"thumbnailUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg","datePublished":"2017-05-30T08:00:00+00:00","dateModified":"2025-11-28T12:31:46+00:00","author":{"@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022"},"breadcrumb":{"@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#primaryimage","url":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg","contentUrl":"https:\/\/demo.sidekick.be\/itenium\/wp-content\/uploads\/2025\/11\/jasmine-tea.jpg","width":360,"height":300},{"@type":"BreadcrumbList","@id":"https:\/\/demo.sidekick.be\/itenium\/blog\/javascript\/javascript-testing-jasmine-syntax\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/demo.sidekick.be\/itenium\/"},{"@type":"ListItem","position":2,"name":"JavaScript Testing: Jasmine syntax"}]},{"@type":"WebSite","@id":"https:\/\/demo.sidekick.be\/itenium\/#website","url":"https:\/\/demo.sidekick.be\/itenium\/","name":"Itenium","description":"We build software. We build careers.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/demo.sidekick.be\/itenium\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/6ab401ce01e858184ee4a8667fe0f022","name":"Usdevelop","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/demo.sidekick.be\/itenium\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cc8cf9b9e9d5bbe7e8d247907cc9ac8457bbfa2854b52eaddf6529adfe88ceab?s=96&d=mm&r=g","caption":"Usdevelop"},"sameAs":["https:\/\/demo.sidekick.be\/itenium"]}]}},"_links":{"self":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/comments?post=1669"}],"version-history":[{"count":2,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1669\/revisions"}],"predecessor-version":[{"id":2229,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/posts\/1669\/revisions\/2229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media\/2110"}],"wp:attachment":[{"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/media?parent=1669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/categories?post=1669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.sidekick.be\/itenium\/wp-json\/wp\/v2\/tags?post=1669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}