jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.21k stars 6.46k forks source link

`Object.create` problem, Jest can not test mongoose models (mocha works perfectly). #1204

Closed nodkz closed 8 years ago

nodkz commented 8 years ago

Yesterday I whole day try to test mongoose models with jest. Firstly, think that problem with me and my hands. And I tried tons of variants jest's configs, re-read tons of issues. And at last I take mocha and it started up with the first kick.

It seems, that Object.create some-how not fixed properly (https://github.com/facebook/jest/issues/933)
Mongoose internally use Object.create for creating instances.

Repo with test: https://github.com/nodkz/jest_mocha_mongoose Broken jest test: npm run test Same success mocha test: npm run test-mocha

npm debug file npm@3.8.9, node@v6.2.0 jest-cli@12.1.1

jest output file:

jest › it should create mongoose model
  - TypeError: Object prototype may only be an Object or null: undefined
        at Object.<anonymous> (node_modules/mongoose/lib/browserDocument.js:96:29)
        at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
        at Object.<anonymous> (node_modules/mongoose/lib/document_provider.js:9:23)
        at Object.<anonymous> (node_modules/mongoose/lib/types/embedded.js:7:16)
        at Object.<anonymous> (node_modules/mongoose/lib/types/array.js:5:24)
        at Object.<anonymous> (node_modules/mongoose/lib/types/index.js:6:17)
        at Object.<anonymous> (node_modules/mongoose/lib/schema/buffer.js:8:22)
        at Object.<anonymous> (node_modules/mongoose/lib/schema/array.js:13:11)
        at Object.<anonymous> (node_modules/mongoose/lib/schema/documentarray.js:7:17)
        at Object.<anonymous> (node_modules/mongoose/lib/schema/index.js:12:25)
        at Object.<anonymous> (node_modules/mongoose/lib/schema.js:1504:32)
        at Object.<anonymous> (node_modules/mongoose/lib/document.js:8:14)
        at exports.isMongooseObject (node_modules/mongoose/lib/utils.js:508:27)
        at Object.clone (node_modules/mongoose/lib/utils.js:248:7)
        at Schema.path (node_modules/mongoose/lib/schema.js:507:24)
        at Schema.add (node_modules/mongoose/lib/schema.js:393:12)
        at new Schema (node_modules/mongoose/lib/schema.js:96:10)
        at Object.eval (jest-test.js:8:18)
        at Object.<anonymous> (node_modules/jest-jasmine2/src/jasmine-pit.js:35:32)
        at jasmine2 (node_modules/jest-jasmine2/src/index.js:253:7)
danielstern commented 8 years ago

When I was having a simliar problem, the issue was the Mongoose Connection. Try to save a reference to your connection and use it specifically to create all Models and operations.

nodkz commented 8 years ago

@danielstern many thanks for the temporary hack!

But I hope that this problem will be resolved in proper way.

cpojer commented 8 years ago

I suspect this is fixed in Jest 15 because of the new defaults. See facebook.github.io/jest/blog/2016/09/01/jest-15.html

Let me know if this still persists and we can reopen and troubleshoot.

sibelius commented 7 years ago

@danielstern do u have an example of how to save a mongoose connection?

couturecraigj commented 7 years ago

I am having issues with this. I was having a hard time trying to see what was causing it but I am pretty sure it is Jest as it is working fine in production and on Mocha.

Negan1911 commented 7 years ago

Currently having the same issue, using jest ^20.0.4 & mongoose ^4.11.5

couturecraigj commented 7 years ago

@Negan1911 I for some reason was able to solve it when I tried to replicate the issue for the purposes of giving a example repo. I am not sure what happened. But maybe that is worth a try. Jest caches quite aggressively because of doing things on workers and it could be a worker is stuck. They suggest uninstalling and re-installing as well but that did not work either. I just found moving to another repo and trying to do something apparently frees up the cache. Might be worth a try.

Negan1911 commented 7 years ago

Tried with the example repo of above and failed, i don't think that is because of jest cache because i ran the tests inside a Docker container, and equally fails, i'd make sure that it started clean and i got the same result screen shot 2017-07-31 at 11 52 50 am

Could you told me what version of jest&mongoose are using? maybe it is a regression issue

nodkz commented 7 years ago

For me Jest is working perfectly. You may see these examples:

Negan1911 commented 7 years ago

After digging some hours with this issue, i found that the problem indeed is having this property resetModules: true on the jest.config.js which make sense because you'll need to mantain the module since it has the connection. So even if seems the same problem it isn't. thanks!

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.