Unit Testing Sisense ComposeSDK with Jest
Unit Testing Sisense ComposeSDK with Jest
Introduction
Unit testing is a software development practice where individual components or functions of a library or application are tested independently in isolation. This article outlines how to configure Jest, a JavaScript unit testing library, to effectively test components or functions built using the Sisense ComposeSDK (@sisense/sdk-data).
Terminology
- Jest: Jest is a popular JavaScript testing library used to run automated unit tests, providing assertions, mocks, and comprehensive test coverage reports.
- Babel: Babel is a JavaScript compiler that converts modern ES6 code into syntax compatible with older JavaScript environments, such as older browser versions or Node.js versions.
- Lodash: Lodash is a JavaScript utility library that provides convenient functions for common programming tasks such as manipulating arrays, objects, and strings. ComposeSDK uses Lodash internally.
- ES Modules (ESM): ES Modules, or ESM, refer to JavaScript’s official standardized system for importing and exporting code between separate files and packages, allowing modular and maintainable code structures.
- ES6+ (ES2015+): ES6+ (also known as ES2015+) refers to modern JavaScript syntax and features introduced since the 2015 ECMAScript specification, widely used in current JavaScript development.
What is Unit Testing?
Unit testing involves verifying that individual units of code, such as functions or methods, behave as expected in isolation. Unit tests are automated, repeatable, and provide immediate feedback on code quality and reliability.
Step-by-Step Guide
Step 1: Setting up Jest for ComposeSDK
Jest, by default, does not process ES Module (ESM) syntax (import and export) found in external dependencies. Since ComposeSDK and its internal dependencies (such as lodash-es) use ESM syntax, additional configuration is required to successfully execute unit tests using the ComposeSDK libraries.
Create a Jest configuration file (jest.config.cjs) at the root of the project with the following content:
// jest.config.cjs
module.exports = {
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
transformIgnorePatterns: [
'node_modules/(?!@sisense/sdk-common|@sisense/sdk-data|lodash-es)',
],
moduleFileExtensions: ['js', 'jsx', 'json', 'node'],
};
This configuration explicitly instructs Jest to process ComposeSDK and its dependency lodash-es with Babel, while ignoring other third-party packages, ensuring tests can execute correctly.
Step 2: Configuring Babel for Jest
To support Jest’s transformation of modern JavaScript syntax, a Babel configuration file (babel.config.cjs) is required at the root of the project:
// babel.config.cjs
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
],
};
This configuration ensures that modern JavaScript code is compiled down to syntax supported by the current Node.js environment.
Step 3: Writing Unit Tests for ComposeSDK
The following is an example function using ComposeSDK’s filterFactory:
// src/makeFilter.js
import { filterFactory } from '@sisense/sdk-data';
export function makeFilter(column, value) {
return filterFactory.greaterThan(column, value);
}
The following Jest test demonstrates typical assertions. Note that some tests may contain more detailed checks than commonly needed:
// src/makeFilter.test.js
import { makeFilter } from './makeFilter';
describe('makeFilter', () => {
it('returns the correct NumericFilter for greaterThan [Test.Dim] > 1000', () => {
const filter = makeFilter('[Test.Dim]', 1000);
// Core properties
expect(filter._name).toBe('filter');
expect(filter.type).toBe('filter');
expect(filter.filterType).toBe('numeric');
expect(filter.isScope).toBe(true);
// Attribute & comparison
expect(filter.attribute).toBe('[Test.Dim]');
expect(filter.valueA).toBe(1000);
expect(filter.operatorA).toBe('fromNotEqual');
// Defaults
expect(filter.description).toBe('');
expect(filter.composeCode).toBeUndefined();
// Config object shape
expect(filter.config).toMatchObject({
guid: expect.any(String),
disabled: false,
locked: false,
});
});
});
Step 4: Configuring the Test Script in package.json
To enable running Jest tests via a single command (npm test), add the following script to the project's package.json file:
{
"scripts": {
"test": "jest"
}
}
With this configuration, executing unit tests is straightforward:
npm test
This command runs Jest, which executes all unit tests in the project and reports the results.
Jest will execute the unit test and report results indicating success or failure based on the assertions in the output.
[ALT Text: Alt text: "Terminal screenshot showing successful Jest test result for makeFilter function. All test suites and tests are passed, with one test taking 3 ms."]
Step 5: Extending Jest Configuration for Additional Dependencies
If other packages produce similar ESM-related issues, they can be added to the existing Jest configuration under transformIgnorePatterns:transformIgnorePatterns: [
'node_modules/(?!@sisense/sdk-common|@sisense/sdk-data|lodash-es|another-esm-package)',
],
This flexibility enables straightforward testing of any ES module dependencies, including any other potential Compose SDK dependencies.
Conclusion
Unit testing Sisense ComposeSDK components with Jest requires explicitly configuring Babel to compile the ComposeSDK libraries. By adjusting Jest and Babel configurations appropriately, effective unit tests can be implemented. This approach allows comprehensive and full unit test coverage.