Testing Bryntum Grid with Jest
Jest is an extensive JavaScript testing library that seamlessly integrates with frameworks like Node.js, React, and Angular, with minimal configurations, making it a popular choice with JavaScript developers. While Bryntum components ship with the Siesta testing tool, you may wish to extend your existing Jest testing setup to cover the Bryntum components in your application, too.
In this guide, we will explore:
- Why Jest does not support testing Bryntum modules by default.
- How to configure Babel to transpile Bryntum components from ECMAScript to CommonJS to allow testing with Jest.
- How to write a basic Jest test for a Bryntum Grid component and make the necessary project configuration changes.
If you would like to follow along, clone the Bryntum Grid starter repository, which contains a basic React app with a Bryntum Grid component. The complete-config branch on the starter repo contains the complete configuration setup for testing the Bryntum Grid component with Jest.
Prerequisites
You can try out Bryntum components for free using our public Bryntum trial packages. If you have a Bryntum license, please refer to our Npm Repository Guide to access the private Bryntum repository.
Install the Bryntum Grid component by following the guide here.
Why Jest does not support testing Bryntum modules by default: ECMAScript vs. CommonJS
Bryntum components are packaged as ECMAScript Modules. Jest is usually used with CommonJS modules and currently has only experimental support for ECMAScript modules. This experimental support is disabled by default, and we won't use it in this guide. Instead, we'll configure Jest's existing functionality to work with ECMAScript modules.
ECMAScript differs from CommonJS in key ways, including asynchronous module loading, support for dynamic imports, and syntax variation. Using Jest to test ECMAScript modules will return the error SyntaxError: Unexpected token 'export', as ECMAScript uses the import and export keywords where CommonJS uses require() and module.exports. Since Jest expects CommonJS, it can't handle the ECMAScript syntax.
We can overcome these differences by transpiling the Bryntum modules from ECMAScript to CommonJS using Babel, an open-source JavaScript transcompiler. This will allow Jest to integrate with Bryntum and test Bryntum components like other CommonJS modules.
Exploring the starter app
If you have cloned the starter repository, you should have a basic Bryntum Grid React app ready to go. This app is built off Create React App with minor changes to configure the project to use a Bryntum Grid. We use Create React App instead of Vite as the former has better support for Jest, while Vite has some limitations.
To start the React app in development mode, install the project dependencies:
npm install
Now run:
npm start
The development app will start at http://localhost:3000.
Writing a basic test
Let's write a basic test using Jest. This test will simply ensure that there is a Bryntum Grid component in the React app.
In the src directory, create a SimpleGrid.test.tsx file and add the following code to it:
import '@testing-library/jest-dom';
import React from 'react';
import { render, screen } from '@testing-library/react';
import SimpleGrid from './SimpleGrid';
test('Renders Simple Grid', () => {
render(<SimpleGrid />);
expect(screen.getByText(/Grid Title/i)).toBeInTheDocument();
});
To execute this test, run:
npm test
You will get the following error:
SyntaxError: Unexpected token 'export'
1 | import React from 'react';
> 2 | import {BryntumGrid} from "@bryntum/grid-react";
| ^
3 |
4 | const SimpleGrid = () => {
5 |
at Runtime.createScriptFromCode (node_modules/react-scripts/node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/SimpleGrid.tsx:2:1)
at Object.<anonymous> (src/SimpleGrid.test.tsx:4:1)
This error occurs because we have not yet transpiled the Bryntum components to CommonJS, and Jest does not know what to do with the import and export keywords, which are not used in CommonJS.
Configuring Babel
We will use Babel to transpile the Bryntum component from ECMAScript to CommonJS.
To customize the Babel configurations to override the defaults, add the babel.config.json file to the project's root directory:
{
"presets": [
[
"@babel/preset-typescript",
{
"allowDeclareFields": true
}
],
"@babel/preset-react",
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
Configuring Jest to use Babel
To configure Jest to use Babel, add a jest.config.ts file containing the following code to the project root directory:
import type { JestConfigWithTsJest } from 'ts-jest';
const jestConfig: JestConfigWithTsJest = {
extensionsToTreatAsEsm : ['.ts'],
moduleNameMapper : {
'^(\\.{1,2}/.*)\\.js