Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDoug Hawkinson
Full Stack JavaScript Techdegree Student 25,073 PointsHow to control the execution sequence of modules in a Node project
This has become very problematic. This paragraph will make more sense as you read on. I am adding additional information to my original post. I have become quite concerned that my seeding functionality is NOT completing before my testing functionality fires off. Where as all of my testing was failing when I originally posted this problem, I have corrected my testing code and now the first three tests are succeeding. However, if the truth be told, I am setting the values for testing in the first three test cases so I have control over them. The final two cases rely on the test database being seeded with data.
This is a Tech Degree Program exercise. I am fairly new to Node.js and asynchronous programming. I am working on an API project with a testing module in it. One of my modules (seed.js) connects to a mongodb database and seeds data into the database. Once the data is seeded I have tests that run from (test.js).
I know the seeding function works as it should. I have verified that numerous times. The db is created and seeded with data, as expected.
When looking at this, ignore the fact that the final two tests are not working. I know that, and am working on it. My concern is the execution timing of the modules. The snippet below shows the console output when I execute npm test.
The first thing that is displayed is the two describe statements and the first test case from my test.js file.
Following that, I am getting the display output from the seeding function interspersed with the display output of my test cases, until we get to the final describe block and the final two test cases.
In my research I came across the following:
Nothing runs before describe (except prior describes and code outside of callbacks). Mocha first runs all the describes to find out what all your tests and hooks are, then runs all your hooks and tests after it is done with all the describes and has found all of the tests and hooks. While it seems irrelevant in trivial cases, this is in fact important for several reasons, including variable lifetime/scope and the fact that you cannot add tests or hooks in a test or hook and reliably have Mocha do what you expect.
It would, however, be accurate to say that before runs before the first it in a given describe, as opposed to beforeEach (which in contrast runs again before every it). (In the case of before/beforeEach outside any describe, you have to imagine that all the test files together are implicitly inside one big invisible describe -- Mocha actually handles stuff like hooks outside of describe blocks using something like that.)
which only lends credence to the fact that the timing issue cannot be solved with a hook, as I have discovered by trying that. The output is the same.
My question is this. How do I get the mongoose-seed output to display, thereby signalling the completion of the seeding process, before ANY of the describe() output from the tests.
I want to be sure that the seeding process has finished before the testing process starts. Without that assurance, I cannot tell if my last two test cases are failing because of syntax or some other programming error, or because of a failure of the seeding process to have completed.
I have tried solving the problem with a new version of seed.js (see playSeed.js below) that adds a Timeout function that I thought would run the seeding then delay for 3 seconds before moving on. But all that did was prevent the seeding from happening AT ALL. I'm bet there is something wrong with playSeed.js, but I am enough of a newbie that I don't know what it is.
dougs-mbp:fsjsProject11 doug5solas$ npm test
> fsjsProject11@0.0.0 test /Users/doug5solas/training/treehouse/fsjsProjects/fsjsProject11
> export NODE_ENV=test || SET "NODE_ENV=test" && mocha test/test.js
[---USER routes---]
POST /api/users
✓ should create a user with complete information
Successfully initialized mongoose-seed
Connected to: mongodb://localhost:27017/CourseRateAPITest and seeding files
[ 'models/user.js', 'models/review.js', 'models/course.js' ]
✓ should reject a user with incomplete information
Reviews collection cleared
Users collection cleared
✓ should reject a user with a duplicate emailAddress
Courses collection cleared
GET /api/users
Successfully created document [0] of Review model
Successfully created document [2] of Review model
Successfully created document [1] of Review model
Successfully created document [1] of User model
Successfully created document [2] of User model
Successfully created document [1] of Course model
Successfully created document [0] of User model
Successfully created document [0] of Course model
1) should return a user with credentialed information
2) should reject a user that is unauthorized
3 passing (4s)
2 failing
1) [---USER routes---]
GET /api/users
should return a user with credentialed information:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
2) [---USER routes---]
GET /api/users
should reject a user that is unauthorized:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I have included links to three gists, seed.js, seedPlay.js and test.js.
seed.js https://gist.github.com/dhawkinson/2e83c2bdbe9af8c9f26ab5b675e8d714
seedPlay.js https://gist.github.com/dhawkinson/3a90bba255351a24cc4d8f8d357de5cb
test.js https://gist.github.com/dhawkinson/728fd190b9a0730a3c1e6e75a802b201
Thanks.