mocks-server/main

When loading variant from .json file, error is shown if body is not an array

Closed this issue · 2 comments

In our project, mocked response for GET /api/user would look like this:

{
  "users": [
    {
      "id": 1,
      "name" "John Smith"
    },
    {
      "id": 2,
      "name" "Jane Smith"
    }
  ]
}

I have added route variant that reads response body from file:

[
  {
    "id": "users-search",
    "url": "/api/user",
    "method": "GET",
    "variants": [
      {
        "id": "success",
        "type": "file",
        "options": {
          "status": 200,
          "path": "users-search/success.json",
          "options": {
            "root": "test/mocks/routes"
          }
        }
      },
      {
        "id": "dev",
        "type": "proxy",
        "options": {
          "host": "https://myserver.example.com"
        }
      }
    ]
  },
  {
    "id": "proxy-all",
    "url": "*",
    "method": "*",
    "variants": [
      {
        "id": "dev",
        "type": "proxy",
        "options": {
          "host": "https://myserver.example.com"
        }
      }
    ]
  }
]

Logs
The route variant is working as expected, but the following message is shown in the logs:

Warning: [files:loader:routes:file:C:/project/test/mocks/routes/users-search/success.json] Error loading routes from file C:/project/test/mocks/routes/users-search/success.json: File does not export an array

Hi @bagratina, the issue may be related to Mocks Server trying to load the users-search/success.json file as it was containing routes. You should move it to another folder outside the routes one, because all files there are considered route files.

For example, you could create a mocks/fixtures folder and place there the json files containing the responses data. Then, change the variant configuration to:

{
//... variant options
"options": {
      "status": 200,
      "path": "users-search/success.json",
      "options": {
        "root": "test/mocks/fixtures"
      }
  }
}

Thanks for quick reply, it helped. The case can be closed