Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing makeMockModels yields error that .sequelizerc can not be found #91

Closed
dschuessler opened this issue May 10, 2019 · 8 comments
Closed
Assignees
Labels
bug Something isn't working investigate Look into this

Comments

@dschuessler
Copy link

I am importing makeMockModels as follows:

import { makeMockModels } from 'sequelize-test-helpers';

Running my Mocha test yields the following error originating from src/mockModels.js, line 11: https://gist.github.com/dschuessler/0e1c075837c4d7207151753313950d42

When I copy the exact path from the error and output it with cat /api/backend/.sequelizerc the file is present.

The error disappears when I change line 11 from:

 sequelizerc = require(path.join(projectRoot, '.sequelizerc'))

to

  sequelizerc = require.main.require('./.sequelizerc')

I am not entirely sure about the nature of the problem, but I guess it has something to do with Node having problems to require from absolute paths. I would greatly appreciate it, if you could have a closer look. Thanks in advance!

@davesag davesag self-assigned this May 12, 2019
@davesag davesag added bug Something isn't working investigate Look into this labels May 12, 2019
davesag added a commit that referenced this issue May 12, 2019
…found-handling-and-update-deps

[Optimisation, #91] improve sequelize not found handling and update deps
@davesag
Copy link
Owner

davesag commented May 12, 2019

Hi @dschuessler — a few questions to help me debug this: I've never known Node to have issues with a require from an absolute path, but it might be something to do with whatever pre-processor you are using to convert typescript to javascript.

  1. What version of Node are you using? (node -v)
  2. what is your project root? (I am guessing it's /api/backend going on your logs but can you please confirm this)
  3. what is in /api/backend/.sequelizerc?
  4. Does sequelize itself find your .sequelizerc file?

I have made some minor updates to how it handles the error if it can't find the .sequelizerc file so please try your code against the latest develop branch instead of the version in npm and let me know how that goes.

@davesag davesag added the question Further information is requested label May 12, 2019
@dschuessler
Copy link
Author

Your commit has the same effect on my test as my workaround. The test now runs through and fails with the following error.

     Error: ENOENT: no such file or directory, scandir '/api/backend/src/models'
      at Object.readdirSync (fs.js:785:3)
      at listModels (node_modules/sequelize-test-helpers/src/mockModels.js:32:6)
      at finder (node_modules/sequelize-test-helpers/src/mockModels.js:36:26)
      at Object.makeMockModels (node_modules/sequelize-test-helpers/src/mockModels.js:39:6)
      at Object.<anonymous> (src/controller/user/user.service.spec.ts:26:26)
      at Generator.next (<anonymous>)
      at /api/backend/src/controller/user/user.service.spec.ts:7:71
      at new Promise (<anonymous>)
      at __awaiter (src/controller/user/user.service.spec.ts:3:12)
      at Context.it.only (src/controller/user/user.service.spec.ts:24:72)

The content of .sequelizerc is the following:

const path = require('path');

module.exports = {
  'config': path.resolve('config', 'sequelize-cli.js')
  'models-path': path.resolve('models')
}

So makeMockModels does not read the model path from .sequelizerc, but searches in the standard location.

Regarding your other questions:

  1. Node 10.15.3
  2. Yes, the root directory is /api/backend
  3. I am pretty new to this tech stack so I actually don't know how to find out if Sequelize can find .sequelizerc. If you tell me how I can find out, I will gladly have a look. I can assure that Sequelize does find its models.

Also Typescript is transpiled by means of ts-node 8.1.0.

@davesag
Copy link
Owner

davesag commented May 13, 2019

@dschuessler Okay so your models are in /api/backend/models/ and not in /api/backend/src/models. I presume that's where the .ts files are though and not where the resulting .js files end up, or do you compile your .js into the same path as the .ts files?

My guess is that this is come sort of typescript weirdness as this library works just fine if you just use javascript. Are you able to share your repo, or make a small demo repo that exhibits this bug that I could take a look at? I'm not a typescript person but I don't see any reason why basic things like require would fail. Also what platform are you running on? Linux, Mac, or Windows. That might be a factor.

@davesag
Copy link
Owner

davesag commented May 16, 2019

@dschuessler I have pushed version 1.1.1 to npm — it probably doesn't fix your issue but it does include the improvement to how I detect the missing .sequelizerc file.

@dschuessler
Copy link
Author

I quickly assembled a small demo repo reproducing the error:

https://github.com/dschuessler/sequelize-test-helpers-bug-example

Do npm install && npm test to get the error I get.

@davesag
Copy link
Owner

davesag commented May 24, 2019

Thanks I'll aim to take a look over the weekend.

@davesag
Copy link
Owner

davesag commented May 24, 2019

Hey @dschuessler please see dschuessler/sequelize-test-helpers-bug-example#1 where I have fixed your tests and example code.

@davesag
Copy link
Owner

davesag commented May 24, 2019

I've released version 1.1.2 which improves the documentation of custom models folder path and adds the ability to supply custom file suffixes as well

@davesag davesag closed this as completed Jun 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigate Look into this
Projects
None yet
Development

No branches or pull requests

2 participants