Jasmine在Node.js上出现ERR_UNSUPPORTED_DIR_IMPORT错误。

huangapple go评论161阅读模式
英文:

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_dirspec_files字段来执行所有测试。你只需简单地运行jasminenpx 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.

huangapple
  • 本文由 发表于 2023年1月11日 03:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75075164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定