Integration testing

Learn how to write integration tests for your integration.

When writing integration tests, it's generally recommended to avoid relying on external services for faster and more reliable results.

The Next.js SDK allows you to set up a custom endpoint so you can mock responses and test your integration without relying on our servers.

Set a custom endpoint

You can set up a custom endpoint using an environment variable:

NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL=http://localhost:3000

And these are the endpoints you can mock:

PathDescription
/client/web/cidEndpoint for assigning a CID.
/client/web/evaluateEndpoint for query evaluation.
/client/web/trackEndpoint for tracking events.
/contentEndpoint for content retrieval.

Notice that if you enable the test mode, you do not need to mock the /client/web/track endpoint, as the SDK replaces the transport layer with a fake one that simulates successful calls.

Create a mock server

Mockoon is a free and open-source tool for quickly mocking APIs and services. You can define rules to mock different responses based on request parameters, which is perfect for testing different slots and query results.

To get started, install the Mockoon CLI:

npm install -g @mockoon/cli

Then, create a configuration file named croct.json with your mock rules. The easiest way to do this is to use the Mockoon desktop app, which generates a configuration file for you.

Here is an example of a configuration file that mocks the content of a slot named home-hero and the result of the query user is returning:

croct.json
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
{
"uuid": "4b9baabf-9953-4f2f-bc7f-ee45ef2aad8f",
"lastMigration": 22,
"name": "Croct",
"endpointPrefix": "",
"latency": 0,
"port": 3000,
"hostname": "0.0.0.0",
"routes": [
{
"uuid": "0e12ece9-2173-4a50-aeed-7f1ecfbe80f4",
"documentation": "Content for \"home-hero\" slot.",
"method": "post",
"endpoint": "content",
"responses": [
{
"uuid": "3b3d03fa-c92f-465b-a1b9-56fba1a53221",
"body": "{\n \"content\": {\n \"_component\": \"hero-banner@1\",\n \"title\": \"Banner title\",\n \"subtitle\": \"Banner subtitle\",\n \"cta\": {\n \"label\": \"Button\",\n \"link\": \"https://croct.com\"\n }\n }\n}",
"latency": 0,
"statusCode": 200,
"label": "",
"headers": [],
"filePath": "",
"sendFileAsBody": false,
"rules": [
{
"target": "body",
"modifier": "slotId",
"value": "home-hero",
"invert": false,
"operator": "equals"
}
],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": true
}
],
"enabled": true,
"responseMode": null
},
{
"uuid": "d41789d9-fe4d-42d4-93a8-f0ebab7bde01",
"documentation": "Evaluation result for \"user is returning\"",
"method": "get",
"endpoint": "client/web/evaluate",
"responses": [
{
"uuid": "4e6b250d-b601-4437-8341-52271f7a63c7",
"body": "false",
"latency": 0,
"statusCode": 200,
"label": "",
"headers": [],
"filePath": "",
"sendFileAsBody": false,
"rules": [
{
"target": "body",
"modifier": "query",
"value": "user is returning",
"invert": false,
"operator": "equals"
}
],
"rulesOperator": "OR",
"disableTemplating": false,
"fallbackTo404": false,
"default": true
}
],
"enabled": true,
"responseMode": null
}
],
"proxyMode": false,
"proxyHost": "",
"proxyRemovePrefix": false,
"tlsOptions": {
"enabled": false,
"type": "CERT",
"pfxPath": "",
"certPath": "",
"keyPath": "",
"caPath": "",
"passphrase": ""
},
"cors": true,
"headers": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"proxyReqHeaders": [
{
"key": "",
"value": ""
}
],
"proxyResHeaders": [
{
"key": "",
"value": ""
}
]
}

Lastly, run the following command to start the mock server:

mockoon start --config croct.json

After starting the mock server, accessing your application should return the mocked content and query results.