Blog

Eric Peterson

December 23, 2017

Spread the word


Share your thoughts

Today's featured module is a TestBox module that introduces snapshot testing capabilities to TestBox. If you've avoided testing your legacy applications, come see how [TestBox Snapshots](https://www.forgebox.io/view/testbox-snapshots) can get you started.

TestBox Snapshots

What is snapshot testing?

Snapshot testing is the latest craze. Popularized by Facebook’s Jest testing framework, snapshot testing is a simple way to add regression testing to your application.

Snapshots are useful for making sure the data passed in does not change unexpectedly. Snapshots aren't meant to replace other types of testing and assertions, but rather are another tool for helping you sleep at night knowing your application is performing as it should.

Setup

To get started with snapshot testing in TestBox, install the testbox-snapshots module from ForgeBox. Then add the matchers in your beforeAll method.

function beforeAll() {
    super.beforeAll();
    addMatchers( "testbox-snapshots.SnapshotMatchers" );
}

You can then assert that snapshots match using the toMatchSnapshot method.

it( "should display the home page", function() {
	var event = execute( event = "Main.index", renderResults = true );
	expect( event.getRenderedContent() ).toMatchSnapshot();
} );

Note: there is no notToMatchSnapshot method.

Here's the flow for how the matcher works:

  1. It checks if a snapshot has been generated for this spec. If not, it fails the test and suggests you generate a snapshot.

  2. If the updateSnapshots url flag is passed, the snapshot will be created or updated with the new value and the test will pass.

  3. If a snapshot exists and the updateSnapshots url flag is not passed, it passes only if the value passed in matches the snapshot.

Better Diffing

For more precise changes, make sure to include the optional java library in your test's load paths (this.javaSettings). It produces a diff of the files instead of just outputting the contents in their entirety. You can enable this by including the following in your tests/Application.cfc:

this.javaSettings = {
    loadPaths = [ "testbox-snapshots/lib" ],
    reloadOnChange = false
};

Wrap Up

Want more information on snapshot testing with TestBox? You can check out my own blog post about the subject, or Jason Steinshouer's two-part series on the topic (here and here).

Add Your Comment

Recent Entries

Is Cloud the Answer for Your ColdFusion Dilemma?

Is Cloud the Answer for Your ColdFusion Dilemma?

Feeling the limits of an on-premise ColdFusion setup?

Many businesses face high costs, limited scalability, and performance bottlenecks, leaving them wondering if the cloud could be the answer.

In our FREE whitepaper, "Is Cloud the Answer for Your ColdFusion Dilemma?", we explore:

  • Benefits of Migrating to the Cloud: From cost savings to increased flexibility, find out what a cloud-based ColdFusion setup can do.
Cristobal Escobar
Cristobal Escobar
November 13, 2024
Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024
10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

ColdFusion remains a powerful and versatile platform for building dynamic web applications. However, keeping your ColdFusion environment optimized, secure, and scalable requires specialized expertise. Whether managing a long-standing ColdFusion application or planning new development projects, hiring a dedicated ColdFusion consulting and support team can be a game-changer for CTOs, CIOs, and developers. Here's why:

1. Expert Guidance on ColdFusion Web Development

...

Cristobal Escobar
Cristobal Escobar
November 08, 2024