ZweiEuro
ZweiEuro7d ago

Test using png mocks

When using imports with *.png files it will crash denos testing system with a warning:
error: Expected a JavaScript or TypeScript module, but identified a Unknown module. Importing these types of modules is currently not supported.
Specifier: file:///home/zweieuro/Documents/uni/2023_SS/WUI/html_UI/vivaci_ui_deno/client/src/player_life.png
error: Expected a JavaScript or TypeScript module, but identified a Unknown module. Importing these types of modules is currently not supported.
Specifier: file:///home/zweieuro/Documents/uni/2023_SS/WUI/html_UI/vivaci_ui_deno/client/src/player_life.png
While I understand the problem, how can i mock away the png and replace it with an identity object or something similar? In jest i would transform it away (https://stackoverflow.com/questions/46898638/importing-images-breaks-jest-test) but i am not sure how deno would handle this
Stack Overflow
Importing images breaks jest test
In React components importing assets (ex, import logo from "../../../assets/img/logo.png) gives such error ({"Object.":function(module,exports,require,dirname,filename,global,jest){�PNG...
29 Replies
marvinh.
marvinh.7d ago
Not possible with Deno's built in test runner. Solution is to stick with jest or vitest
ZweiEuro
ZweiEuroOP7d ago
Hmm, guessing i'd have to write everything in jest then, right ?
marvinh.
marvinh.7d ago
yes, use jest or vitest
ZweiEuro
ZweiEuroOP7d ago
That's a shame, is there any support for this planned ? Importing 'css' files will cause a similar issue so i am assuming a lot of people are running into this issue
marvinh.
marvinh.7d ago
no, support planned any time soon I'm afraid. The team is pretty evenly split on whether we should add a module loader API to support loading non-js files so I doubt it would be added anytime soon
ZweiEuro
ZweiEuroOP7d ago
If i were to use something other than deno, i guess deno test, coverage and other things would also need to be done by jest ? Ah, that's a shame
marvinh.
marvinh.7d ago
yes, all these features are part of the test runner.
ZweiEuro
ZweiEuroOP7d ago
I am a bit confused as to how front-end development and testing should even be possible then
marvinh.
marvinh.7d ago
vite + vitest is the typical setup
ZweiEuro
ZweiEuroOP7d ago
yeah but i cannot use deno test and the coverage report then, right ?
marvinh.
marvinh.7d ago
yes, but you can generate pretty much the same coverage report with vitest
ZweiEuro
ZweiEuroOP7d ago
I find it a bit ironic that one of the main advertised features of deno seems to be "no longer needing to learn a new test system everytime" and then exactly that is the case fair enough Is there a template on how to cleanly do this with deno ?
marvinh.
marvinh.7d ago
I hear ya, which is why I'm the camp that Deno should support module loaders I'm not sure if we have a template for that. It should work the same way as you'd do in node
ZweiEuro
ZweiEuroOP7d ago
Yeah, but i am only familiar with jest (and all the weird edge cases it has) so i was hoping deno would offer a proper alternative. I am fine with trying vitest though, guessing there is not much difference if i essentially use react-testing-lib for rendering anyways
marvinh.
marvinh.7d ago
if you're familiar with jest, you'll love vitest. It's basically jest for the modern times
ZweiEuro
ZweiEuroOP7d ago
Practically impossible to tell what is modern and not nowadays ._. everything is so fast and new anyways. Just glad deno makes me forget about babel
marvinh.
marvinh.7d ago
The deno test runner is a bit barebones. Works well for simple stuff, but doesn't play well with complex frontend needs
ZweiEuro
ZweiEuroOP7d ago
hm, so it would be nice if there was a plugin for it i guess, but oh well, nohthing i can do about this now tho i did loose a day trying to get this to run xD thx a lot for the context ! would this work for starting vitest: "test": "deno test -A --node-modules-dir=auto npm:vitest" Kinda at a loss why it cant find any test files nad is generally not really doing anything and i am not sure if the how the root property interacts with deno and other options its not running a setujp file or anything else
marvinh.
marvinh.7d ago
The vitest documentation is pretty great. You can configure all of these things in their config file https://vitest.dev/config/#setupfiles
ZweiEuro
ZweiEuroOP7d ago
yeah i am reading through that rn but my vite config came pre-configured with stuff for deno from the template, which i have no idea about
marvinh.
marvinh.7d ago
the getting started section of vitest tells you all about that I'm afraid I don't have the bandwidth to guide you through that at the moment
ZweiEuro
ZweiEuroOP7d ago
NP, THX!
ZweiEuro
ZweiEuroOP6d ago
I've read through the docs but there is something blocking me from using this inside deno. I am getting this error with a library I wrote: link; It just says to "tell the maintainer", which is me, but I have no clue what vite expects here or if its some resolution problem with deno. I understand that might be just a vite issue, but maybe if you are familiar with it you can throw me some doc link of what this error is even referring to ?
vitejs
Troubleshooting
Next Generation Frontend Tooling
ZweiEuro
ZweiEuroOP6d ago
I am comparing my package.json and library to one from jsdom that isn't throwing the error, but i see no difference
marvinh.
marvinh.6d ago
The issue is that some portion of your code or a library that you're using is importing native node modules when vite is creating builds for the browser. This is not a deno or a vite/vitest issue, but an issue with whatever tries to import that in browser builds. Reason is that there is no node:fs module in the browser
ZweiEuro
ZweiEuroOP6d ago
Hm... my code doesn'T use any node specific things that polyfill would fill, evne if i tried it with polyfill it fails. I pretty much just wrote custom react hooks in that library. so i am guessing its a configuration problem ? ..or am i not allowed to touch globalThis at all in any module otherwise its considered not compatible ?
marvinh.
marvinh.6d ago
Hard to tell without seeing any code
ZweiEuro
ZweiEuroOP6d ago
I got all the code, but i really don't want to throw this at you sinec you already said you are tight on time. https://gitlab.zweieuro.at/wui/core/web-user-interface-node/-/releases/v2.5.1 It essentially just does some custom stuff with global.window.<func here> its part of a big project i work on but that is essentially the API "out". But its fine if it is undefined.
GitLab
v2.5.1 · wui / core / Node Module · GitLab
2.5.1 (2025-03-05) Bug Fixes package.json: fix missingt type...
ZweiEuro
ZweiEuroOP6d ago
if something is scanning all imports and looking for illegal statements i could work around em if they are listed somewhere as far as i can tell the only thing here is Object.defineProperties(window, ... but even so, that should not trip any predefined things Even if i delete everything inside the file i am trying to import it throw this error so i am suspecting its not even the content and something else is really wrong I also treid to find the error inside vitest repo to maybe reverse enginer it, but i cant find it

Did you find this page helpful?