mocks-server / main

Node.js mock server running live, interactive mocks in place of real APIs
https://www.mocks-server.org
Apache License 2.0
281 stars 15 forks source link

Cannot find collections file in /mocks folder when files.options.only is used in mocks.config.js #448

Open jacobdo2 opened 1 year ago

jacobdo2 commented 1 year ago

Describe the bug When setting up typescript project and following the docs, the collections file inside of mocks folder stops working the moment I enable files.options.only in mocks.config.js

To Reproduce

  1. Clone this repo: https://github.com/jacobdo2/mocks-server-ts-example
  2. Run yarn to install deps
  3. Run yarn mocks to start

Expected behavior It should compile and run the project

Logs Error loading file /Users/myName/Projects/mocks-test/mocks/collections.ts: .only must be an array, or undefined Error: .only must be an array, or undefined

javierbrea commented 1 year ago

Hi @jacobdo2,

It seems to be related to your Babel version. As described in this issue, since a determined version Babel only supports an array as value for the only option. So, if you want to use a function you can define an array with a function as unique element:

{
  files: {
    babelRegister: {
      enabled: true,
      options: {
        only: [(filePath) => {
          return filePath.includes("/mocks/") || filePath.includes("/fixtures/");
        }],
      },
    },
  },
}

Note: I have tested the change in your repository and it works.

Thanks for the notice, even when it is not related to this project, it would be good to mention it in the guide about how to configure TypeScript. I'll open another issue for that.