Closed kalwalt closed 2 years ago
Additional informations: You can browse my PR https://github.com/webarkit/jsartoolkitNFT/pull/126
Hi @kalwalt,
This is not an issue with QUnit or karma-qunit, but part of how Karma works in general. As such, this is not something we control. I have expressed my preference to change this in Karma at https://github.com/karma-runner/karma/issues/1607.
Nonetheless, I will try to help. Karma creates its own HTML page instead of using one that you manually create yourself. From the PR, I understand you know this already. This automatic HTML page loads the included files
as scripts, with the URLs automatically created relative to the basePath
, which is generally the root directory of the repository, the same place as where karma.conf.js
and package.json
are.
What you perhaps have not yet noticed is that this automatic HTML page is served to the browser as the virtual file /index.html
. This is different from the real file you had before, which was at /test/index.html
. As such, a relative URL like ./foo.dat
in your code will be relative to /
instead of /test/
. This is not something that can be changed. Consider the HTML page as existing virtually at /index.html
, so you will need to consider test URLs as relative to /
now.
On top of that, Karma also serves other files from its own web server. The files from your repository are served from /base/
by default, so /test/foo.dat
is available at /base/test/foo.dat
. For most requests this is automatically translated for you.
In some cases, a project needs to fetch additional files during the test. I know of a few different options to make this easy:
proxies
(docs). For example, tell Karma that /test/
should go to /base/test/
. (This is fine so long as the directory name doesn't conflict with Karma's own server.) Example at https://github.com/karma-runner/karma/issues/2917#issuecomment-496473358.proxies: { "/": "/base/" }
. In this case you will likely need to set urlRoot:
as well. For example, urlRoot: "/_karma/"
. If that doesn't work, the proxy may need to map /
to /_karma/base/
. That would make requests to /test/foo.dat
work transparently without needing to change the JavaScript code.window.__karma__
in a conditional statememt. For example, in your test code you can define base = (window.__karma__ ? '/base/' : '/')
and use that as prefix in your requests./base/test/
as the actual URL.Thank you @Krinkle for the long answer! Your comments were very helpful to me, at the end i left the path as you described in the 4. point, but maybe i will test also the other solutions.
@Krinkle I want also say that helped me a lot your https://github.com/Krinkle/example-node-and-browser-qunit-ci it is a good reference. I had an issue because i had setup singleRun: false
, and the action github runner was hanging waiting forever, i din't realize it until i looked into your karma.conf.js again and i solved.
Thank you again!
I am the jsartoolkitNFT maintainer and creator, i added in the past qunit test with Grunt but i would use instead Karma with karma-qunit. JsartoolkitNFT is a small lib for WebAR and it's necessary to load some config parameter files for the set up. I developed some testing to load the
camera_para.dat
file but i add to 'force' the path to an unusual"./base/tests/camera_para.dat";
normally it's only necessary this path"./camera_para.dat"
. Anyway with"./base/tests/camera_para.dat";
the test goes well instead if replace with"./camera_para.dat"
, tests are failing. This is my structure in tests folder:karma.conf.js
file:Does it is correct? I have the feeling that i missed something, but searching on the docs can't find anything else. Thank you, Walter.