ccampbell / luna-testing

Simple, modern, opinionated JavaScript unit testing
https://craig.is/testing/code
MIT License
151 stars 6 forks source link

Luna reports syntax error in file that loads fine in directly-run browser #14

Closed gwhitney closed 4 years ago

gwhitney commented 4 years ago

I have a file externals.js in my project (externals.txt) that is responsible for loading third-party modules (and falling back to my cached copies if the CDN is not accessible). If I open node_modules/puppeteer/.local-chromium/linux-706915/chrome-linux/chrome and load a page that imports externals.js, it works fine. However, when I run luna on this file, I get the following output

site/externals.js

⚠️  SyntaxError: Unexpected token (9:22)
5 : export var three;
6 : export var threeLoaded = new Promise(async function(resolve, reject) {
7 :     var success = false;
8 :     try {
9 :         three = await import('https://cdn.jsdelivr.net/npm/three@0.111.0/build/three.module.js');
                          ^

❓  Perhaps you meant to run your tests in node using the --node flag

Is this a problem with Luna, or is there something I am doing wrong? I can't make any sense of why "import" is an unexpected token here since clearly the version of chromium being used supports dynamic import. Thanks for any suggestions/fixes to Luna/etc.

ccampbell commented 4 years ago

Thanks for the ticket. I suspect this could be a rollup or buble issue where it is expecting all imports at the top of the file without detecting that it is an import function call. I can try to debug a little later. Just to confirm, are you using the latest version of Luna (1.5.0)? I recently updated all the dependencies to their latest versions as well as Chromium so not sure if it could be fixed there. Or maybe it broke as a result of that change.

I use dynamic script loading a bunch on my projects with Luna, but using my own method vs. the import method so it could def just be a bug.

ccampbell commented 4 years ago

Okay I am able to reproduce in 1.5.0. Seems to be a Luna issue. Will keep you updated on a fix

ccampbell commented 4 years ago

Turned out to be an issue with buble after all. The dynamic import function was not added until Chrome 63. I will publish a new version of luna to npm with the fix in a few.

One thing to note is that I am not sure if the ./ path is not going to work for a resolution in a luna context. You can try using --node to have it run in node mode, but if you need it to run in a browser you may have to change the path to /node_modules/three..., but it may work. I didn’t have a build of three there to test that