Jasmine specs for the frontend often need some DOM elements to work with. Because creating them is such a common task, we should have an efficient way to do it...

...a selector like "span.foo" and turn it into a element for you: up.element (from Unpoly) jasmine-fixture (requires jQuery) dom-create-element-query-selector Here is the example above using...

makandra dev

In a Jasmine spec you want to spy on a function that is imported by the code under test. This card explores various methods to achieve this. Example

...is to write a function that can swap out its internal implementation with a Jasmine spy later. This adds a little noise to your module, but allows you to keep...

...content('finished in') # wait for all tests to finish expect(page).not_to have_css('.jasmine-failed .jasmine-description') # better error messages expect(page).to have_css('.jasmine-bar.jasmine-passed') # safer...

end Then 'all specs should have passed' do expect(page).to have_css('.jasmine-version'), 'Failed to load Jasmine page' using_wait_time 60 do expect(page).to...

Jasmine has long standing support for writing asynchronous specs. In days gone by we used the done callback to achieve this, but these days it is possible to write much...

...expect(this.submitButton.disabled).toBe(true) submitPromise.then(() => { // form is done expect(this.submitButton.disabled).toBe(false) done() // notify Jasmine we're finished }) }) Newer versions of Jasmine allow our specs to simply return a Promise...

Jasmine is a great tool to unit test your JavaScript components without writing an expensive end-to-end test for every little thing. Jasmine is a purely client-side tool...

Jasmine: Creating DOM elements effectively Hennings talk Jasmine für Fortgeschrittene (2023, in our library) jasmine-dom matchers Exercises Integrate Jasmine Integrate Jasmine into your MovieDB app and perform the...

makandra dev
content.pivotal.io

...returns an object with a compare key. Usually it is registered with beforeEach: beforeEach(() => { jasmine.addMatchers({ // Example matcher toBeAnything() { return { compare(actualValue, ...matcherArguments) { // Do some computations here ... // Return whether the actualValue...

return {pass: true} } } } }) }) Usage expect(actualValue).toBeAnything(...matcherArguments) When a matcher is invoked, Jasmine will call its compare() function with the actualValue, followed by any arguments passed to the...

Jasmine specs that work with DOM elements often leave elements in the DOM after they're done. This will leak test-local DOM state to subsequent tests. For example, this...

...automatically after each test. By including the following helper hooks your tests can call jasmine.fixtures to access a container that is emptied once the test concludes: beforeAll(function() { jasmine.fixtures = document.createElement...

...happening? Shouldn't waitFor wait until the element has actually been found? Sadly no! Jasmine's assertions with their expect method do not throw errors and just continue with the...

Jasmine comes with two matchers that test for equality. The first is toBe: expect(first).toBe(second) toBe passes when first === second. Unfortunately this is useless for non-primitive values...

...because JavaScript is a horrible language. However, Jasmine comes with another matcher toEqual: expect(first).toEqual(second) This matcher behaves as a human would expect for types like the following...

Whenever you have to deal with randomness in a jasmine test there are some spy strategies to help you out! Let's say we have a method Random.shuffle(array) to...

...wrappingFunction() // no way to expect the promise state }) In this case you can use jasmine.spyOnGlobalErrorsAsync() to temporarily disable Jasmine's detection of unhandled errors: it('has a test', async function...

...await jasmine.spyOnGlobalErrorsAsync(async function() { wrappingFunction() }) }) You can even make assertions on the unhandled error by accepting a spy in the callback: it('has a test', async function() { await jasmine.spyOnGlobalErrorsAsync(async...

To check if a method has been called in Jasmine, you first need to spy on it: let spy = spyOn(window, 'alert') codeThatAlerts() expect(window.alert).toHaveBeenCalledWith('Important message')

...an object of a given type, pass the constructor function to jasmine.any(): expect(spy).toHaveBeenCalledWith(jasmine.any(Object)) expect(spy).toHaveBeenCalledWith(jasmine.any(String)) expect(spy).toHaveBeenCalledWith(jasmine.any(Number))

Due to the way we setup Jasmine tests in our projects, you may run into various errors when Jasmine boots. Setting jasmineRequire on undefined Jasmine 4 may fail with an...

...expose getJasmineRequireObj() to your specs. Fix for Webpack Add a file // app/webpack/spec/support/jasmine_provider.js import jasmineRequire from 'jasmine-core/lib/jasmine-core/jasmine.js' export default function getJasmineRequireObj() { return jasmineRequire; } Then expose the provider in your...

...Should be easily adaptable to a pure Webpack setup. Step 1: Install Jasmine yarn add jasmine-core Step 2: Add two separate packs Since we do not want to mix...

...pack, since this will confuse Jasmine and cause it to swallow all backtraces. // app/webpack/packs/jasmine.js import 'jasmine-core/lib/jasmine-core/jasmine.css' import 'jasmine-core/lib/jasmine-core/jasmine-html.js' import 'jasmine-core/lib/jasmine-core/boot0.js' import 'jasmine-core/lib/jasmine-core/boot1.js...

Jasmine has spyOnProperty(), but it only works if the property is implemented using getter and setter functions. This is a known limitation of Jasmine. If the mocked property is a...

...converts a value property to getters and setters so it can be mocked with Jasmine. You can use it like this: const x = { foo: 1 } console.log(x.foo) // 1 spyOnValueProperty(x...

When the order matters: expect(array1).toEqual(array2) Regardless of order: expect(array1).toEqual(jasmine.arrayWithExactContents(array2)) Ignoring extra elements: expect(array1).toEqual(jasmine.arrayContaining(array2...

... containers for the code to work on. We're using jasmine-fixture to quickly create such a container from a CSS selector by saying affix...

...users'). jasmine-fixture will append the container to the document body and clean it up after the test. We need to tell Jasmine to wait until the AJAX response was...

@$httpBackend.expectGET('/api/something').respond nested: example: 'json' # Run all queued requests @$httpBackend.flush() # Have a Jasmine expectation expect(something).toHaveHappened() Notes This example assumes you have already employed ngMock

...to disable uiRouter's initialization. Otherwise it would attempt to sync the current URL (Jasmine's!) with its routes, fall back to the default route and then fail to load...

jasmine.github.io

Jasmine has a jasmine.clock() helper that you can use to travel through time and trigger setTimeout and setInterval callbacks: beforeEach(function() { timerCallback = jasmine.createSpy("timerCallback"); jasmine.clock().install(); }); afterEach(function() { jasmine.clock().uninstall...

...causes a timeout to be called", function() { setTimeout(function() { timerCallback(); }, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.clock().tick(101); expect(timerCallback).toHaveBeenCalled(); }); If you actually want the test to wait out the...

...of our PostgreSQL cheat sheet for MySQL lamers, here is a cheat sheet for Jasmine when you're used to RSpec. Note that Jasmine syntax has changed with Jasmine...

...so if you're using Jasmine 1.x you might instead want to use an older cheat sheet. Expectations # RSpec expect(foo).to.eq("value") expect(foo).to_not eq("value...

To test that an object was constructed by a given constructor function, use jasmine.any(Klass): describe('plus()', function() { it ('returns a number', function() { let result = plus(1, 2) expect(result...

...toEqual(jasmine.any(Number)) }) }) Also see Expecting objects as method invocation arguments...

...will appear to navigate away from http://localhost:3000/specs (or wherever you run your Jasmine tests). This is inconvenient, since reloading the document will no longer re-run the test...

jasmine.github.io

The easiest way to freeze or travel through time in a Jasmine spec is to use the built-in jasmine.clock(). After jasmine.clock().install() you can use it to control setTimeout...

...and setInterval. Using jasmine.clock().mockDate() you can mock new Date() (which returns the current time in Javascript) While you can use SinonJS Fake timers, using the built-in Jasmine clock...

To delay your entire Jasmine test suite until the DOM is ready, add the following: beforeAll(function(done) {