我是学生,所以我对Node.js和异步编程都很陌生。我正在开发一个带有测试模块的API项目。我的一个模块(seed.js)连接到mongodb数据库并将数据播种到数据库中。一旦数据被播种,我就会在(test.js)中运行测试。
除了我正在处理的test.js文件中的错误之外,我有一个我想要解决的问题,部分是为了美观,但我也担心我的测试可能会在播种完成之前开始运行。我怀疑可能是因为我的控制台输出如下:
```
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
Successfully initialized mongoose-seed
Connected to: mongodb://localhost:27017/CourseRateAPITest and seeding files
[ 'models/user.js', 'models/review.js', 'models/course.js' ]
Reviews collection cleared
Users collection cleared
Courses collection cleared
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 Course model
Successfully created document [0] of User model
1) should create a user with complete information
2) should reject a user with incomplete information
3) should reject a user with a duplicate emailAddress
GET /api/users
4) should return a user with credentialed information
5) should reject a user that is unauthorized
0 passing (10s)
5 failing
1) [---USER routes---]
POST /api/users
should create a user with complete 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---]
POST /api/users
should reject a user with incomplete information:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
3) [---USER routes---]
POST /api/users
should reject a user with a duplicate emailAddress:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
4) [---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.
5) [---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.
npm ERR! Test failed. See above for more details.
```
正如您所看到的,播种开始(连接消息),然后我从测试模块获得3行输出,然后是其余的播种输出,最后用测试输出完成。
我想要做的是在任何测试开始之前看到种子功能完成。我已经包含了两个要点的链接,seed.js和test.js.
seed.js https://gist.github.com/dhawkinson/2e83c2bdbe9af8c9f26ab5b675e8d714
test.js https://gist.github.com/dhawkinson/728fd190b9a0730a3c1e6e75a802b201
我知道test.js目前无法正常工作,但我现在仍在努力解决这个问题。
我关心的是种子和测试之间的执行顺序。
感谢。
答案 0 :(得分:1)
由于seed.js
似乎导出将在播种完成后解析的承诺,因此应该可以将describe
块包含在test.js
中,如下所示:
seed.then(() => {
// Describe, etc...
});
我认为更典型的Mocha方式是使用调用before
的异步seed
hook。