README
Expressions Unit Test Library
A small environment which allows to add unit tests to expressions.
Set up
Preconditions
This library is published as a NodeJS module. You can use npm
or yarn
to install it
In case you already have a package.json:
Install the module with npm install pim-integrations-expression-unit-test --save-dev
or yarn add pim-integrations-expression-unit-test --dev
and use it as described below.
In case you do not have a package.json:
It is recommended that you add a package.json
to your project, which lists and handles the JavaScript dependencies. You can use yarn init
or npm init
to create one and then proceed as described above. Don't forget to exclude the folder node_modules
from version control!
In case you do not want to have a package.json:
You can install the module with npm install pim-integrations-expression-unit-test
or yarn add pim-integrations-expression-unit-test
. In this case, you always have to (re-)install the module every time. Don't forget to exclude the folder node_modules
from version control!
Unit test inline expressions
- If you want to paste in your expressions directly from the Product Editor, you have to paste them into a
.js
file and put it into a named function:
Example
Your expression in PIM:
term("$red")
Create a new file called customJSFunctions.js
and wrap it into a named function, like this:
function myFunction(){
term("$red")
}
Then you can use the function in the test file as myFunction()
Writing tests
- This framework is based on jest.
- Use our example repository for in-depth examples and documentation.
- All functions which usually are available within an expression, like
term
andboilerplate
, are set up as jest-mocks. See the example repository for details. - On the top of your test file, place the following lines:
jest.unmock('pim-integrations-expression-unit-test');
const util = require('pim-integrations-expression-unit-test').initalize(options});
eval(util.unparsedExpressions);
- The first line makes sure that
jest
does not mock this library - the second line initializes this library.
options
is a JavaScript object, defined as follows:
{
filePath: "./customizationArea/integration/externalProduct/customJSFunctions.js" //points to the file with the expressions. Default: ./customizationArea/integration/externalProduct/customJSFunctions.js
}
- The third line makes the expressions available for test.
Notes
- PIM does not support ES6 features in expressions.
- This test suite does currently not support the
format
functions from PIM.
Compatibility
- This test suite works with PIM 8.10m1
Contributing
We are happy to accept pull requests.
Contributors
Uwe Stolz | |
---|---|
David Brien | |
Alexej Froebel | |
Philipp Flenker |