Mongoose unit test documentation & Atom Editor tsconfig.json
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Request 1: It would be great if there was an example available that shows how to write/use a unit test for modules that rely on @nestjs/mongoose. I would be especially interested in knowing how the mongoose connection dependencies should be injected into Test.createTestingModule()
(best practices).
There is a unit test already available in the 01-cats-app example, but it doesn’t make use of mongodb or mongoose.
Request 2 (Less important): Looking again at unit tests but now looking at 01-cats-app. The unit test is shown in the file cats.controller.spec.ts
. The tsconfig.json
however excludes all *.spec.ts files from the definitions. I believe that cats.controller.spec.ts
is not “bound” to any tsconfig.json
context because of that.
Opening the cats.controller.spec.ts
in the well-known Atom editor will therefore throw errors at the keywords describe
, beforeEach
, it
, etc. If one removes "**/*.spec.ts"
from tsconfig.json
, the errors are gone because Atom now interprets the spec.ts file as being part of the project.
Removing that line will obviously solve the issue, but all test files will also be compiled on a productive system which leaves a bad taste.
I could think of other editors that might have similar issues. It would be great if a solution was documented. One solution could be by using a supplementary tsconfig.json
file in a test directory, that is only read when using the npm test command.
Expected behavior
Both documentation requests documented.
Environment
Nest version: 4.6.4
For Tooling issues:
- Node version: 9.5.0
- Platform: Linux (Ubuntu 16.04.3 LTS)
Others:
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
@kamilmysliwiec At the end of the document, you indicate an example (14-mongoose-base) but in this example the test described in the document is missing, I would like to understand how to mock a result (Cats) that extends document (mongoose) into a unit test, thanks.
Here is a short path or a hacky-way about how i uses
@nestjs/mongoose
package in unit testing. first we will need to install aMockoose
but lately it has a problem with
promise
idk, but Kamil mentioned to usemockoose-fix
it’s the same lib but with this hot fix. anyway, enough talking. let’s code.create a new Provider in your database folder, oh, you are free to create it anywhere 😄
next in your unit testing.
I should mention that in this way we don’t use the full power of
MongooseModule
in our tests but it will work.I will make a PR soon to the
MongooseModule
to be auto-configured when your application in Testing Mode, i mean when theNODE_ENV === 'test'
it will pickMockoose
instead of making a real connection to database.