Replies: 2 comments
-
I see this as more of an issue with the Node.js require cache. The first import The only solution I can think of is to not mix import styles. However, I am open to suggestions, if this can be improved |
Beta Was this translation helpful? Give feedback.
-
I ran some tests on the issue repository and wasn't able to reproduce the behaviour you've mentioned:
import ControllerA from "/home/aboud/adonis-jest-mock-issue/app/Controller";
import ControllerB from "../app/Controller";
import ControllerC from "App/Controller";
console.log(ControllerA); // [class Controller]
console.log(ControllerB); // [class Controller]
console.log(ControllerC); // [class Controller]
console.log(ControllerA == ControllerB); // true
console.log(ControllerB == ControllerC); // false
console.log(ControllerA == ControllerC); // false From your description I was expecting the first comparison to also fail. console.log(new ControllerA().service.constructor); // [class Service]
console.log(new ControllerB().service.constructor); // [class Service]
console.log(new ControllerC().service.constructor); // [class Service]
console.log(new ControllerA().service.constructor == new ControllerB().service.constructor); // true
console.log(new ControllerB().service.constructor == new ControllerC().service.constructor); // false
console.log(new ControllerA().service.constructor == new ControllerC().service.constructor); // false Apparently this means that adonis aliases have their own scope, and everything imported through the alias, including nested imports, belongs to this scope. I have found a workaround, which is forcing the ...
"aliases": {
...
"CRUD": "node_modules/crud-lib"
}
... // WithAlias.test.ts
import Controller from "App/Controller";
import Service from "CRUD/Service"; It still breaks with relative/absolute imports, but they are a bad practice anyway. I can't evaluate how far this issue reaches beyond mocking. I've been using Adonis for a lot of stuff and this was never a concern. The workaround above has minimal impact on our workflow and apparently works, however I'd like to make sure my reasoning is valid. |
Beta Was this translation helpful? Give feedback.
-
I'm implementing tests on my application using adonis-jest, which handles the Transformer logic used by
Adonis
along withjest
.It has been working so far. The issue below relates specifically to mocking.
The way the transformer works apparently breaks deep references from the modules, so mocked modules don't share the scope of modules imported using aliases.
Apparently, when importing through the alias, the module no longe shares a reference to the original module:
If you mix both syntaxes (with and without aliases) through the application, mocked tests might break without an apparent cause. This is a pitfall, but could be avoided by double-checking imports.
The real problem appears when trying to mock a method from within a node dependency. As described on the issue repository:
crud-lib
, added as dependency to my adonis applicationapp
crud-lib
exports aService
class, with arequest
methodapp
has aController
, which imports and usescrud-lib/Service
Controller
:a. I mock the implementation of
Service.prototype.request
, also imported ascrud-lib/Service
b. The
Controller
prototype returned byApp/Controller
apparently doesn't share the mockedService
.c. The
Controller
prototype returned by../app/Controller
does, so it works.This means I'm not able to use aliases if I intend to use mocks, which feels like a bad decision.
The one thing I don't get is why the
Service
prototype is different for theController
imported with and without alias. Does the Adonis Transformer creates a "deep cache" of the required modules?Any thoughts would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions