xolvio / jasmine-unit

A jasmine-based unit-testing framework designed for the Velocity test runner
9 stars 4 forks source link

Error with SimpleSchema #33

Open bitomule opened 10 years ago

bitomule commented 10 years ago

I've installed jasmine-unit on my Meteor app and it crashes with this error:

Error loading app files SimpleSchema is not defined ReferenceError: SimpleSchema is not defined

And the last error on terminal is:

[SyntaxError: Unexpected identifier]

Any idea on how can I fix this?

ghost commented 10 years ago

You you please run your app with DEBUG=1 VELOCITY_DEBUG=1 JASMINE_DEBUG=1 mrt and post the full log as Gist.

bitomule commented 10 years ago

Yes, here it is:

https://gist.github.com/bitomule/2fe7a1abfeb164e40ccc

Hope you can help me. Thanks Sanjo.

ghost commented 10 years ago

There is a problem with auto-stubbing simple-schema. I reported it here https://github.com/alanning/meteor-package-stubber/issues/6. I will try to fix it in the next days.

In the meantime you can create a custom-stubs.js file in the tests directory and add you own stub.

SimpleSchema = function () {};
bitomule commented 10 years ago

It stills fires error, now about attachSchema and I see also something about iron router. Here's the gist:

https://gist.github.com/bitomule/f3ce0970cbae4f04e394

ghost commented 10 years ago

You just need to add the method to your stub like this:

SimpleSchema = function () {
  attachSchema = function () {}
};

Yup, I mentioned the iron-router issue already here and it will be fixed the next days also.

bitomule commented 10 years ago

Still get the same error. I'll wait till this is fixed to start working with velocity and jasmine. Thanks for your work Sanjo ;)

alanning commented 10 years ago

Jonas fixed one issue that package-stubber had with simple-schema, recognizing exports when the package uses the array syntax.

There's another issue where package-stubber can't handle the case where the top level function returns undefined, which is what SimpleSchema() does. Working on a fix now.

alanning commented 10 years ago

Released package-stubber v0.0.15 which will gracefully handle top-level functions returning funky stuff. You can pull it down by running mrt update

Since SimpleSchema doesn't return anything useful when the constructor is called without args, you'll probably still need to supply your own custom stub for 'SimpleSchema' as @Sanjo mentioned. For example, 'tests/simple-schema-stub.js'. Once you have a working stub, please consider contributing it back to the community stubs in the package-stubber repo. https://github.com/alanning/meteor-package-stubber/tree/master/package-stubber/community-stubs

bitomule commented 10 years ago

Thanks alining, great work.

I still get the error and I think it's not really on simple schema stub now, it's about collections 2 that has a method on each collection called attachSchema. That method is called on collections like:

MyCollection.attachSchema(Schemas.Item);

So Jasmine throws this error:

Error loading app files Object #<Object> has no method 'attachSchema' TypeError: Object #<Object> has no method 'attachSchema'
alanning commented 10 years ago

Unit-testing in general is more challenging than in-context tests because you need to isolate the individual tests. Usually this requires a lot of manual stubbing. Jasmine-unit tries to make stubbing easier for you by auto-stubbing where it can but in some cases that's not good enough.

Basically any package that provides functions that modify another prototype will need a custom stub. This came up recently with the collection-hooks package since it does something similar to collections2 where it modifies the Collection prototype: https://github.com/xolvio/velocity/issues/57#issuecomment-49490509

So now its a question of who writes the custom stub. If you can do it that would be a big help. You can put the stub in a file like tests/collections2-stub.js and it will be automatically applied for you. Or you can wait for someone else to create the stub.

If you do write it, please contribute it back to the package-stubber community stubs

ghost commented 10 years ago

The stub for collection2 is:

Meteor.Collection.prototype.attachSchema = function () {};

(Haven't tested it)

bitomule commented 10 years ago

Simple Schema stub seems to be more complicated, now I get an undefined property id on simple schema in this line:

SimpleSchema.RegEx.Id

This is the error:

Error loading app files Cannot read property 'Id' of undefined TypeError: Cannot read property 'Id' of undefined
ghost commented 10 years ago

Yeah, mocking simple-schema completely is a bit more work. The API is in the documentation. I don't have time to do it.

You can use my jasmine package for the client tests. Then you don't have to mock simple-schema at all.

aldeed commented 10 years ago

@Sanjo @alanning, just skimming through trying to figure out what should be done in general regarding ss/c2. It sounds like you would like someone to stub the package APIs in full and do a PR to package-stubber/community-stubs? I have no idea how to write a stub or which parts of the API would need to be included.

Also, you make reference to weird things ss/c2 are doing regarding return values, subclassing, etc. Are there any changes that could be made to the packages to make them play more nicely with testing frameworks? Do you have a list of general tips in that regard?

alanning commented 10 years ago

Hi @aldeed, thanks for following up with this. A stub of the full API would be ideal but just stubbing enough to prevent the errors is better than what we have now.

The packages themselves aren't doing anything incorrect, they just have functionality that is difficult to stub automatically. So creating a manual stub for these cases would be appropriate.

@Sanjo is writing up a tutorial on how to stub packages; we'll be sure to link that here once its ready. In the meantime, you can take a look at the meteor-stubs repo and the community-stubs in package-stubber.

ghost commented 10 years ago

I think the component-mocker can mock simple-schema and collections2 automatically without problems. I did a quick test last week. We can integrate the component-mocker package into the package-stubber next week.

@aldeed I will ping you if manual work is still required after we updated the mocker package.

aldeed commented 10 years ago

Awesome, thanks guys!

bitomule commented 10 years ago

Great! Thanks guys for the great work :)