本文介绍了TypeError:无法解析模块说明符"@ open-wc/testing".相对引用必须以"/"、“./"或"../"开头.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find some good way to unit test m y vanilla webcomponents and I am trying this approach open-wc and I am getting error mentioned above

my-component-test.js

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的相关部分

relevant part from 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/[email protected]

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

*** edited in 2019 Jully 22

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

I change the import by writing the whole to path (import { html, fixture, expect } from "C:/_d/WSs/basic-vanilla-webcomponent/node_modules/@open-wc/testing";) and then I got this error that doesn't help much:

推荐答案

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

I was facing the same issue but pointing tests inside the src folder works without any error. So for example

这会产生与您相同的错误

This gives the same error like yours

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

但这很好用

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

这篇关于TypeError:无法解析模块说明符"@ open-wc/testing".相对引用必须以"/"、“./"或"../"开头.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:24