英文:
Jasmine on Node.js fails with ERR_UNSUPPORTED_DIR_IMPORT
问题
I'm writing an Express v4.18.2 app on Node.js 18.12.1 on Windows. I'm testing a controller with Jasmine 4.5.0. When I run jasmine spec
, it fails with an error message about resolving ES modules:
> Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import
> '...\SimpleServiceJasmine\spec' is not supported resolving ES modules
> imported from
> ...\SimpleServiceJasmine\node_modules\jasmine\lib\loader.js
> ...
> code: 'ERR_UNSUPPORTED_DIR_IMPORT',
> url: 'file:///D:/.../SimpleServiceJasmine/spec'
I use require
, not import
, everywhere in the code being tested or the spec.
Note that jasmine runs fine if I specify the spec file explicitly, even with wildcards, as long as the wildcard path resolves to a single file:
jasmine spec/service/contact-api.spec.js # ok
jasmine spec/*/c* # ok
I tried downgrading jasmine to 3.0.0 and 2.0.1 but got the same error. The behavior is the same on Windows 11 and Windows Server 2019.
Any suggestions for how can I run all the specs in this project?
Here's my package.json
:
{
"name": "simpleservice",
"version": "1.0.0",
"description": "A simple CRUD API for contacts",
"main": "service/contact-api.js",
"scripts": {
"test": "jasmine spec/service/contact-api.spec.js",
"start": "node src/service/contact-api.js"
},
"author": "Puzzled Dev",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"jasmine": "^4.5.0"
}
}
Here's the spec/support/jasmine.json
:
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.?(m)js"
],
"helpers": [
"helpers/**/*.?(m)js"
],
"env": {
"stopSpecOnExpectationFailure": false,
"random": true
}
}
英文:
I'm writing an Express v4.18.2 app on Node.js 18.12.1 on Windows. I'm testing a controller with Jasmine 4.5.0. When I run jasmine spec
, it fails with an error message about resolving ES modules:
> Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import
> '...\SimpleServiceJasmine\spec' is not supported resolving ES modules
> imported from
> ...\SimpleServiceJasmine\node_modules\jasmine\lib\loader.js
> ...
> code: 'ERR_UNSUPPORTED_DIR_IMPORT',
> url: 'file:///D:/.../SimpleServiceJasmine/spec'
I use require
, not import
, everywhere in the code being tested or the spec.
Note that jasmine runs fine if I specify the spec file explicitly, even with wildcards, as long as the wildcard path resolves to a single file:
jasmine spec/service/contact-api.spec.js # ok
jasmine spec/*/c* # ok
I tried downgrading jasmine to 3.0.0 and 2.0.1 but got the same error. The behavior is the same on Windows 11 and Windows Server 2019.
Any suggestions for how can I run all the specs in this project?
Here's my package.json
:
{
"name": "simpleservice",
"version": "1.0.0",
"description": "A simple CRUD API for contacts",
"main": "service/contact-api.js",
"scripts": {
"test": "jasmine spec/service/contact-api.spec.js",
"start": "node src/service/contact-api.js"
},
"author": "Puzzled Dev",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"jasmine": "^4.5.0"
}
}
Here's the spec/support/jasmine.json
:
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.?(m)js"
],
"helpers": [
"helpers/**/*.?(m)js"
],
"env": {
"stopSpecOnExpectationFailure": false,
"random": true
}
}
答案1
得分: 1
Jasmine使用glob进行模式匹配,这意味着每当你将spec
作为模式传递时,glob会找到这个特定目录,然后将其返回给jasmine。不幸的是,jasmine中的路径验证似乎不够智能,它继续使用给定的路径执行,然后由Node.js引发错误。
它等同于这样:
import spec from './spec'
你可以查看jasmine-runner的代码,以及贡献者放置注释的那一行:
> ES模块规范要求导入路径必须是有效的URL。自从v14开始,
Node在Windows上强制执行此规定,但在其他操作系统上不执行。在OS X上,作为URL的导入路径不能包含父目录引用。
因此,一旦你在jasmine.json
中定义了spec_files
,就不需要提供额外的模式来执行所有测试。
jasmine
将使用jasmine.json
中的spec_dir
和spec_files
字段来执行所有测试。你只需简单地运行jasmine
或npx jasmine
。
英文:
Jasmine uses glob for pattern matching, which means whenever you pass spec
as a pattern, glob finds this particular directory and then returns it to jasmine. Unfortunately, path validation in jasmine does not seem in a very smart way, it continues executing with the given path, and then it throws an error by Node.js.
It is equivalent to this:
import spec from './spec'
You can take a look at the jasmine-runner code, and the line in which contributor put a comment:
> The ES module spec requires import paths to be valid URLs. As of v14,
Node enforces this on Windows but not on other OSes. On OS X, import
paths that are URLs must not contain parent directory references.
So, once you have defined spec_files
in jasmine.json
, there is no need to give an extra pattern to execute all tests.
jasmine
will execute all tests with the help of spec_dir
, and spec_files
fields in jasmine.json
. All you need to do is just simply run jasmine
or npx jasmine
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论