TypeError:无法解析模块说明符“ @ open-wc / testing”。相对引用必须以“ /”、“./”或“ ../”开头

时间:2019-07-16 22:12:07

标签: npm karma-jasmine web-component

我正在尝试找到一些对香草Web组件进行单元测试的好方法,并且正在尝试这种方法open-wc,并且遇到上述错误

my-component-test.js

import {
  html,
  fixture,
  expect,
} from '@open-wc/testing';
import { MyComponent } from "../src/my-component.js";

describe("simple test", () => {
  it("test", async () => {
    const el = await fixture("<my-component></my-component>");
    expect(el.success).to.be.false;
  });
});

package.json的相关部分

  "scripts": {
    "test": "karma start",
...
  },
  "devDependencies": {
    "@open-wc/testing": "^1.0.14",
    "@webcomponents/webcomponentsjs": "^2.2.0",
    "babel-eslint": "^10.0.2",
    "es-dev-server": "^1.6.3",
    "eslint": "^6.0.1",
    "eslint-config-google": "^0.13.0",
    "karma": "^4.2.0",
    "karma-chrome-launcher": "^3.0.0",
    "karma-jasmine": "^2.0.1",
    "karma-script-launcher": "^1.0.0",
    "karma-spec-reporter": "0.0.32",
    "prettier": "1.18.2",
    "wct-browser-legacy": "^1.0.2",
    "web-component-tester": "^6.9.2"
  },

控制台

# npm test 



> basic-vanilla-webcomponent@ test C:\_d\WSs\basic-vanilla-webcomponent                                                                       
> karma start                                                                                                                                 

16 07 2019 18:57:51.712:INFO [karma-server]: Karma v4.2.0 server started at http://0.0.0.0:9876/                                              
16 07 2019 18:57:51.719:INFO [launcher]: Launching browsers Chrome with concurrency unlimited                                                 
16 07 2019 18:57:51.730:INFO [launcher]: Starting browser Chrome                                                                              
16 07 2019 18:57:54.850:INFO [Chrome 75.0.3770 (Windows 10.0.0)]: Connected on socket Hf1IzCLlm_IaBeZXAAAA with id 12297274                   
Chrome 75.0.3770 (Windows 10.0.0) ERROR                                                                                                       
  An error was thrown in afterAll                                                                                                             
  ReferenceError: require is not defined                                                                                                      
      at node_modules/find-port/test/findPort.test.js:1:16                                                                                    
  ReferenceError: require is not defined                                                                                                      
      at node_modules/registry-auth-token/test/auth-token.test.js:1:10                                                                        
  ReferenceError: require is not defined                                                                                                      
      at node_modules/registry-auth-token/test/registry-url.test.js:1:10                                                                      
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/00-file-stress.test.js:11:12                                                                    
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/01-file-maxsize.test.js:8:16                                                                    
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/console.test.js:11:14                                                                           
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/file-archive.test.js:10:16                                                                      
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/file-tailrolling.test.js:2:16                                                                   
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/file.test.js:3:14                                                                               
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/http.test.js:7:12                                                                               
  ReferenceError: require is not defined                                                                                                      
      at node_modules/winston/test/transports/stream.test.js:3:14                                                                             
  TypeError: Failed to resolve module specifier "@open-wc/testing". Relative references must start with either "/", "./", or "../".           

Chrome 75.0.3770 (Windows 10.0.0): Executed 0 of 0 ERROR (0.02 secs / 0 secs)                                                                 

npm ERR! Test failed.  See above for more details.                                                                                      

版本已检查

# npm list @open-wc/testing
basic-vanilla-webcomponent@ C:\_d\WSs\basic-vanilla-webcomponent
`-- @open-wc/testing@1.0.14

***于2019年7月22日编辑

我通过将整个内容写入路径来更改导入(从“ C:/ _ d / WSs / basic-vanilla-webcomponent / node_modules / @ open-wc / testing”中导入{html,fixture,expect}),然后我收到此错误并没有多大帮助:

enter image description here

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但是在tests文件夹中指向src可以正常工作。例如

这会产生与您相同的错误

{
   pattern: 'test/**/*.test.js',
   type: 'module'
}

但这很好用

{
   pattern: 'src/**/*.test.js',
   type: 'module'
}

答案 1 :(得分:0)

如果./位于主公用文件夹中,则应尝试添加@open-wc

import {
  html,
  fixture,
  expect,
} from './@open-wc/testing';

答案 2 :(得分:0)

我遇到了类似的问题,但是我的解决方案是在karma.conf.js中添加以下代码:

esm: {
  nodeResolve: true,
},