-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How To Mock Dependencies For Testing? #366
Comments
System.map["moduleB"] = "fake/moduleB"; |
sweet. so just do that before importing the module and its all good? also, does it keep these fake dependencies within the scope of one test file? anything i should know about "resetting"/"clearing out" the fakes? |
You can "swap out" dependencies with something like: System.set('custom/module', System.newModule({ custom: 'module' }));
var tmp = System.get('custom/module');
System.set('custom/module', ...); // etc |
Both of these means don't work for me: https://github.com/micahasmith/babel-test/blob/master/test/app.test.js#L15-L47 |
I take it you're swapping out dependencies of the module you're importing? If so note you need to System.delete the index.js module by exact name as well as both dependencies before re-importing to get the right effect. |
thanks! going to test now |
Doesn't work for me either: https://github.com/micahasmith/babel-test/blob/master/test/app.test.js#L50-L96 Is there some other way devs are going about this now? (Am i using an antiquated solution to code isolation via dep swapping?) |
It could be worth ensuring the delete operation is returning true to indicate its actually doing something. Note you need to be using SystemJS 0.15 with the fix from ModuleLoader/es-module-loader#313. |
its in there in both of the new tests, |
Yes - but check it is returning |
Actually |
Yep, returns |
And you're definitely using SystemJS 0.15? |
i npm installed jspm yesterday, it was version v0.14 it included systemjs v0.13.2 even if i upgrade and get this fixed though, it'll only lead into the problem that the systemjs loader is based on a global mutable singleton map of dependencies, which means i would have to enforce order/non-async execution of my tests in order to ensure that while i mutate the System.js config for one test its not trying to import/use that config in another test, correct? or does System.js have a means to mutate within a sandbox/context? |
Right, the SystemJS update for jspm that will make this work is coming tomorrow or Friday (working on it). If you want to create a custom version of SystemJS for testing, you can use |
Awesome. Well i'm super excited to try both after the release. I'll check back in on this next week then. Thanks! |
After some trial and error, I finally got this working. I had to call let functionToTest;
beforeEach((done) => {
System.delete(System.normalizeSync('foo/bar.js'));
System.set(System.normalizeSync('foo/bar.js'), System.newModule({...});
System.delete(System.normalizeSync('foo/functionToTest.js'));
System.import('foo/functionToTest.js').then((imported) => {
functionToTest = imported;
}).then(done, fail);
}); I think it's kind of weird that I have to do this, because |
@onlywei that is exactly correct. |
@guybedford should the |
I was wondering how to mock a module's dependency for a test?
For instance, let's say that I have a module A that references another module B, but for my unit test I want module B to be swapped out with a fake-- can I do that with systemjs? (esp with es6 modules)
Wasn't able to find any info online. Thanks!
The text was updated successfully, but these errors were encountered: