- Using with MongoDB · Jest — jest-mongodb(more docs)
 
 (SO source) The conventions for Jest, in order of best to worst in my opinion:
src/file.test.jsmentioned first in the Getting Started docs, and is great for keeping tests (especially unit) easy to find next to source files
src/__tests__/file.jslets you have multiple__tests__directories so tests are still near original files without cluttering the same directories
__tests__/file.jsmore like older test frameworks that put all the tests in a separate directory; while Jest does support it, it's not as easy to keep tests organized and discoverable
Becareful that,
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed. When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as local time.
→ Sử dụng UTC time sẽ an toàn hơn (và ko bị ảnh hưởng bởi local time hay timezone)
👇 This SO.
If the tests do make changes to those conditions, then you would need to use 
beforeEach, which will run before every test, so it can reset the conditions for the next one.👉 Read more.
- They are all used for mocking a method.
 
jest.fn()→ returnundefinedif no implementation is provided. (use these methods to implement)
jest.spyOn()→ default it calls the original implementation, it stores the original implementation in memory. ← restore bymockRestore()
Put 
import 'dotenv/config'; at the beginning of the .spec file. It will use the current env file.If we wanna mock some variables?
--runInBand(or-i): source — Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests.
- If using 
spyOn()→restoreAllMocks()inafterEach() 
⭐ Good to read: Mocking functions and modules with Jest | pawelgrzybek.com
👇 Source.
Mocking a named import → official 
jest.mock()Mocking only the named import (and leaving other imports unmocked) → official 
jest.requireActualMocking a default import
Mocking default and named imports
Changing what the mock returns per test
→ Be careful: Calling 
mockReturnValue inside a test still changes the mock for all other tests after it. ← use mockReturnValueOnce instead!Clearing mocks between tests (make sure it’ll be called once) → 
clearAllMocksMocking multiple modules with chaining,
→ Use 
mockName() and getMockName()This is just an example of my very specific case. It may not good (or accurate) enough!
If a class is not a default export from a module (official doc),
Read this official doc.
👇 ⭐ Source (read the exmplanation there)
If we wanna import a constant?
How about a method?
(We don’t need 
import * from ... and also no need __esModule)(Optional) Mock a client of @google-cloud/dialogflow
got has itself got and other methods get, post,…Read this official doc.
There is also a case where in the main funtion, we 
throw new ClassName() and we want to catch this error in the test file!Bên trong hàm cần được test có 1 hàm (
updateEntityTypeSpy) và ta muốn mock hàm này throw an error để có thể test.Create a “util” function for all tests → create it in 
.stub.ts and then export it and import it in the .spec.ts file.❇️  
mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))(On Mac M1) → make sure the terminal is open under 
arm architecture (run arch too see) → then reinstall everything,❇️  
Jest did not exit one second after the test run has completed.(Not a perfect solution) ← just not to see but not solve the problem internally
✳️ Unexpected directive 'TinyBotSpinnerComponent' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
Still not know!!!!
✳️ RequestError: getaddrinfo ENOTFOUND us-undefined
→ Forget to 
.mockResolvedValue() It’s good to do something like this,