问题
Is there a place where my tests can live without being run by Meteor?
I just started my first Meteor project, and began by writing unit tests with Mocha and should.js. Though mocha runs without a problem, the tests prevent Meteor from starting up since it has issues using node's require
instead of __meteor_bootstrap__.require
(full error message).
That being said, Meteor should not be running my tests! According to the Meteor documentation, code can only be placed on the client, server, or both. Unit test suites do no belong in these categories, and I am not the only person confused by Meteor's lack of a well-defined location for placing automated tests.
Right now, my tests are kept in server/test/
, with the contents of each file wrapped in the block:
if (typeof(Meteor) === 'undefined') { ... }
While this works, it does not feel elegant. Do you have any other suggestions for structuring your tests with your Meteor app?
Update: in lieu of explicit instructions in the Meteor docs, I followed the Rails folder conventions (4th paragraph), using a folder called test
for storing my testing assets. I later moved this to server/test
since I did not want it loaded on the client.
回答1:
Place your tests in the tests/
folder. Unlike Rails, which uses a folder named test
for this purpose, Meteor uses the plural tests
for this folder name.
Assets stored in a folder named "tests" will be completely ignored by Meteor; these assets will not be loaded on the client or server.
Ironically, I was tipped off by someone having the opposite issue who wants their tests loaded by the Meteor client.
回答2:
As of 0.6.0, an interesting approach is to exclusively use local smart packages for your app, which can be easily tested using Meteor's tinytest. You would have the bare minimum code residing outside of smart packages to bootstrap your app.
EDIT: I've done this approach, and I don't even need bare minimum code residing outside of smart packages. The whole app is packages.
回答3:
Place your test files in tests
folder and you should be fine. If your application is such that you have multiple files nested in server
or client
folder, you can replicate similar folder structure within your tests/client
or tests/server
directory. Not a rule but I think it helps for maintenance and I have had experience of spending hours debugging missing class error which was simply resolved by organising file structure in test folder.
回答4:
Since This is question from 2012, and there is no single comprehensive answer here, I would like to attempt one here.
This is a good starting point for starting to think about testing your meteor project.
In a nutshell Step1: meteor add sanjo:jasmine Step2: meteor add velocity:html-reporter
The moment you do this, and if your application is running ( or whenever you start your application back running) , on the right upper corner of browser- where you open your application is opened - you shall start seeing a blinking dot. That's it. You have now your testing framework in place.
Now you can actually produce a directory structure with the help of this framework also. The moment you click on that blinking dot, you shall see a popup like below.
This has convenience links for generating your test directory structure.
Alternatively, you can do it manually as below. Next thing is to decide on directory structure. Meteor Documentation says enough about it. You need to have tests folder in your project root.
Taking cue from here, you can go like this.
<projectRoot>
|---jasmine
|---client
|--- integration
|---- my.first.integration.spec.js
|---client
|--- unit
|---- my.first.unit.spec.js
|---jasmine
|---server
|--- integration
|---- my.first.integration.spec.js
|---server
|--- unit
|---- my.first.unit.spec.js
And Viola, start writing your jasmine tests. These posts can further help you.
- Post1
- Jasmine Documentation
- This is another good tutorial
来源:https://stackoverflow.com/questions/11785917/where-should-unit-tests-be-placed-in-meteor