Front-End

Running Jest when you are importing a file which Jest cannot parse

When you are running jest and encounter the error below, the solution is simple. You need to add transformIgnorePatterns in the jest config. For example, you can simply add this in your package.json. This is usually caused by using a third party library that uses ES module. 12345"jest": {   …

Front-End

Using act() in Jest Unit Tests with React-Dom

react-dom/test-utils provides a helper, act() to make sure all the tasks like rendering, user events, and data fetching (these are considered as units of interaction with a user interface) to be processed and applied to the DOM before making an assertion. We use act() when we are using jest with …

Front-End

Unit Testing Event Listeners on Form Input Fields with Karma

Karma is a JavaScript test runner where you can run your unit tests on headless chrome or real browsers. Because it runs on the real browser, it supports the browser web APIs and makes it easy to test front-end JavaScript. With Karma, the environment where unit tests run is truer …

Sitecore

Creating DbItem with Item ID – Sitecore FakeDb

With FakeDb, we can mock content items. Each tree node can be created by the DbItem class. DbItem class has three constructors as below. public DbItem(string name); public DbItem(string name, ID id); public DbItem(string name, ID id, ID templateId); When your code is calling GetItem with the Sitecore.Data.ID object, you …

Sitecore

How to Troubleshoot FakeDb Errors – Sitecore 8.2

A NuGet package, Sitecore.FakeDb helps us to test functions that use Sitecore API. It is a mock memory database where you can remove the dependencies and isolate the code. When you create a brand-new test project, setting up FakeDb can be tricky. Installing FakeDb and adding the usual two Sitecore …

Sitecore

How to Mock Rendering Parameters in Unit Test – Sitecore

Now that we created a cool components with rendering parameters (see Using Rendering Parameters to Inject Data into Hidden Input), we can write a unit test. Sitecore’s Rendering class has a publicly accessible string field called Parameters. This is the rendering parameter string get populated on page rendering. Mocking is …

Front-End

Setting up Unit Tests for React with Mocha, JSDOM and Enzyme

Before start coding your awesome React app, it is always a good idea to set up unit test infrastructure. Once test is set up, you can write tests whenever you feel necessary before waiting to complete the entire app development. Mocha is a JavaScript test framework that runs on Node.js. …

Front-End

React Unit Test Code Snippets with Jest and Enzyme

Sometimes all I need is to see a snippet of code without much explanation when I try to find a solution online. This is because I am familiar enough with the language and framework and need a quick reminder. So, here it is. I created a post with a few …

Front-End

Unit Testing React Form with Jest and Enzyme

Jest is a JavaScript unit testing framework created by Facebook. I usually use mocha for unit testing Node.js code, but when it comes to testing React, Jest combined with Enzyme is pretty much the best. Enzyme is a JavaScript utility library created by Airbnb. It lets you simulate DOM rendering …

.NET

Unit Testing Entity Framework Database Update logic with NSubstitute

In the previous post (UnitTesting ASP.NET Web API by Mocking DbContext with NSubstitute), we explored how to create a unit test for getting data from database with Entity Framework by mocking DbContext and DbSet.  We are going to build upon the API we created previously and add PUT logic where …