Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Impossible to return strings #28

Closed
Owlbertz opened this issue Feb 9, 2018 · 4 comments
Closed

Impossible to return strings #28

Owlbertz opened this issue Feb 9, 2018 · 4 comments

Comments

@Owlbertz
Copy link

Owlbertz commented Feb 9, 2018

Hi,
I am currently having the issue where I want to mock a HTML-file. Unfortunately, I cannot do this because if the second argument of mock() is a string, it assumes that it is a path to look for a module to return. But I simply want mock('../templates/template.html', 'dummytemplate') to return 'dummytemplate'.
Maybe this is already possible somehow...?

@boblauer
Copy link
Owner

boblauer commented Feb 9, 2018

This is not possible currently, as you are correct that it assumes a string is a file path.

I would suggest making a dummy-template.html file instead.

@Owlbertz
Copy link
Author

Owlbertz commented Feb 9, 2018

Having a dummy template is what I am currently doing, but as my project has a rather dynamic structure, this leads to issues during building and testing (using gulp), as files get copied to different locations.

If you think is is a valid feature request, I will try to do a PR.

@boblauer
Copy link
Owner

boblauer commented Feb 9, 2018

Unfortunately I don't think this would be a good feature to add, as it would hide errors when referencing an incorrect path/module. For example, mock('./file.js', './otherr-file.js') would "return" the string ./otherr-file.js, rather than alerting the user that ./otherr-file.js does not exist.

Depending on how your code is setup, you might be able to get away with doing something like:

const html = {
  toString: () => '<html />'
};

mock('./template.html', html);

require('./template.html') will now return an object, but if that object is coerced into a string later on, then it will work just as if it were a string. It's a bit of a hack, but it may work for you depending on how this html string is being used later on in the code.

@Owlbertz
Copy link
Author

Owlbertz commented Feb 9, 2018

I actually tried that, but it failed as toString() was not invoked in my case.
I guess I will just find a way to copy the templates to where they are searched as well.

@boblauer boblauer closed this as completed Feb 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants