misskey icon indicating copy to clipboard operation
misskey copied to clipboard

Use IMemoryDb from pg-mem for unit testing

Open ycanardeau opened this issue 2 years ago • 2 comments

What

Closes #9881.

This pull request

  1. Replaces NestJS DI container with Yohira DI container,
  2. Uses pg-mem instead of a real postgres database for unit testing, and
  3. Replaces Jest with Vitest.

Why

By using pg-mem, an in-memory postgres DB instance for unit testing, I managed to make unit tests much faster and stable in development. (I'm not sure what happens in GitHub Actions.) One of drawbacks of using pg-mem however is that as the official documentation mentions, this library will not be a perfect representation of a real postgres database.

Additional info (optional)

To run the unit tests, you can just type pnpm test or pnpm test-and-coverage as usual.

Note that this PR is still a work-in-progress, and I had to temporarily remove chart.ts, because it doesn't work well for some reason.

If you think this PR is too large, I can split this into smaller chunks.

TODOs:

  • [x] Restore and fix chart.ts (87b740b, bbc0c62, 13b7158).
  • [x] Fix Minimum Note in activitypub.ts (2af817d).
  • [ ] Dispose ServiceProvider on application shutdown.
  • [x] Replace Jest with Vitest in .github/workflows/test.yml (8e32936).

ycanardeau avatar Feb 19 '23 02:02 ycanardeau

Codecov Report

Merging #9988 (96a9209) into develop (57cac0a) will decrease coverage by 1.38%. The diff coverage is 86.18%.

@@             Coverage Diff             @@
##           develop    #9988      +/-   ##
===========================================
- Coverage    74.14%   72.77%   -1.38%     
===========================================
  Files          809      784      -25     
  Lines        77546    76401    -1145     
  Branches      5482     4092    -1390     
===========================================
- Hits         57500    55602    -1898     
- Misses       20046    20799     +753     
Impacted Files Coverage Δ
...ges/backend/src/core/activitypub/ApInboxService.ts 15.41% <2.38%> (-3.06%) :arrow_down:
...ages/backend/src/queue/DbQueueProcessorsService.ts 30.68% <3.57%> (-13.59%) :arrow_down:
...kend/src/queue/processors/InboxProcessorService.ts 17.19% <3.84%> (-1.69%) :arrow_down:
...rc/queue/processors/CleanChartsProcessorService.ts 31.18% <4.00%> (-9.40%) :arrow_down:
...src/queue/processors/TickChartsProcessorService.ts 31.18% <4.00%> (-9.40%) :arrow_down:
...c/queue/processors/ResyncChartsProcessorService.ts 34.14% <4.34%> (-10.86%) :arrow_down:
...ackages/backend/src/queue/QueueProcessorService.ts 14.45% <5.55%> (-1.58%) :arrow_down:
...nd/src/queue/processors/DeliverProcessorService.ts 21.62% <5.55%> (-2.05%) :arrow_down:
.../backend/src/queue/SystemQueueProcessorsService.ts 39.58% <8.33%> (-11.77%) :arrow_down:
...ueue/processors/ImportUserListsProcessorService.ts 20.80% <8.33%> (-1.13%) :arrow_down:
... and 276 more

... and 287 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Feb 23 '23 12:02 codecov[bot]

All tests have passed. I think this PR is ready for review now.

As you can see in https://github.com/misskey-dev/misskey/actions/runs/4274185423/jobs/7440613310, unit tests with pg-mem only took about 30 seconds, which can be compared to https://github.com/misskey-dev/misskey/actions/runs/4274064254/jobs/7440398498 from the latest commit on the develop branch that took over one minute.

Having said that, sometimes there are some issues related to pg-mem, so it's really up to the Misskey dev team whether to merge this PR. (Actually, I started making these changes for experimental purposes.)

The Codecov report above says that merging this PR into develop will increase coverage by 33.86, but I haven't added any additional unit tests. There might be differences between Jest and Vitest.

ycanardeau avatar Feb 26 '23 09:02 ycanardeau

非常に興味深いPRですが、コンフリクトしてしまっている

tamaina avatar Mar 09 '23 13:03 tamaina

@tamaina I'll try to resolve conflicts when I have some time. I also need to fix unit tests in the e2e folder, which were restored in #10163.

ycanardeau avatar Mar 09 '23 13:03 ycanardeau

@tamaina Thanks for your interest, but I started thinking that it's not so a good idea to use a different implementation for unit testing due to lacking features at least for now, and unfortunately, I don't have enough time to work on this at the moment. Closing for now.

ycanardeau avatar May 16 '23 13:05 ycanardeau