main icon indicating copy to clipboard operation
main copied to clipboard

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

Open jacobdo2 opened this issue 2 years ago • 1 comments

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

jacobdo2 avatar Sep 28 '22 07:09 jacobdo2

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.

javierbrea avatar Sep 28 '22 16:09 javierbrea