-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Error says I must use 'import' when I'm clearly using 'import'!! #2304
Comments
You're a little ahead of the curve here. By default AVA 2 still compiles ESM syntax to CJS. You could disable that but AVA won't know to #2293 has more background and our plan for supporting ESM properly. I've now pinned this issue. It'll take a bit of time for AVA (and I think the Node.js ecosystem at large) to adjust to the new ESM behavior in Node.js 13. (I'm closing this issue for housekeeping purposes, but let's keep the conversation going.) |
FYI:
I tried following the directions at
https://github.com/avajs/ava/blob/master/docs/recipes/es-modules.md
At first, it would not work. I had included "type": "module" in my
package.json, AVA recognized that it was there because it would tell me
that because of that, it was regarding it as an ES module. However, it
still insisted that I use "import" in my test file, even though I was,
leading me to believe it was still transpiling my test file.
So, I realized that what I really wanted was to not use babel at all - just
disable it, which is one of the suggestions on the above page.
Unfortunately, it then failed, saying that require() of ES modules is not
supported. So, apparently, though it wasn't transpiling my external ES
library, it was still transpiling the test file, even with "type": "module"
in my package.json
Then, on a lark, I decided to rename my library from testName.js to
testName.mjs, and it worked! As I understand it, using the "mjs" extension
and putting "type": "module" should both do the same thing, but AVA
apparently is using some weird criteria to determine whether to transpile
the test file.
Personally, I have no problem using the "mjs" extension. I come from the
world of Perl, where modules are clearly marked by using the "pm"
extension, while scripts use "pl".
An aside: Someone once said (don't remember his name) "There are only 2
hard things in computer science: cache management and naming things". Yes,
naming things is hard. When I run an AVA test, it shows me the exact test
where it failed - file, line number and all. Therefore, having to name each
test is not only a real impediment to developing unit tests, it's
completely unnecessary. I've seen some posts that lead me to believe that
in the past, AVA didn't require naming tests. I would really like to see
that functionality restored, even if I have to set something in my
package.json. In the Perl world, I already spent quite a bit of time
developing assertions that report the file name and line number where an
assertion failed so that I don't have to name each test (internally, it
generates names like "Fail in file X, line Y" to use for the functions that
require names.
One more thing, in my editor (TextPad), I have AVA configured to run as
"ava --no-color $File" where $File is replaced with the full path of the
currently open file. I'm still getting strange characters in the output
(FYI, this is a little annoying, but not a serious problem):
√ Test 1
√ Test 2
2 tests passed
In case you want to see my setup, which is currently working (keeping my
fingers crossed), I've attached 3 small files, including my package.json.
UPDATE: gmail wouldn't let me attach my main test file test.js, so I'm
pasting it in directly. FYI, I'm using node 13.2.0 and Ava 2.4.0.
import test from 'ava';
import tname from './testUtils.mjs';
test(tname(), t => {
t.deepEqual([1, 2], [1, 2]);
});
test(tname(), t => {
t.deepEqual([1, 2], [1, 2]);
});
…On Tue, Dec 3, 2019 at 3:54 AM Mark Wubben ***@***.***> wrote:
You're a little ahead of the curve here. By default AVA 2 still compiles
ESM syntax to CJS. You could disable that
<https://github.com/avajs/ava/blob/master/docs/recipes/babel.md#preserve-es-module-syntax>
but AVA won't know to import() your test file so it still wouldn't work.
#2293 <#2293> has more background and
our plan for supporting ESM properly. I've now pinned this issue.
It'll take a bit of time for AVA (and I think the Node.js ecosystem at
large) to adjust to the new ESM behavior in Node.js 13.
(I'm closing this issue for housekeeping purposes, but let's keep the
conversation going.)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2304?email_source=notifications&email_token=ABHEZ6KDMPAZCKXQ2QB3WSTQWYNGDA5CNFSM4JUHBWB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFYSYMA#issuecomment-561065008>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABHEZ6II3TKK6WRPYC5T56DQWYNGDANCNFSM4JUHBWBQ>
.
|
What madness is this? I'm clearly using import!
FYI, my package.json file includes "type": "module", so I should not need to use the .mjs file extension, though I've tried that and I get the error message "Cannot apply enhancement-only precompilation, possible bad usage".
Here is my test2.js:
The text was updated successfully, but these errors were encountered: