如何解决:“错误:无法解析'dns'”?

时间:2018-10-25 14:34:37

标签: javascript mocha fetch-api cypress

  

我正在对Cypress进行调用提取的测试。 JSON服务器正在运行
  然后我收到错误消息:   ./node_modules/fetch/lib/fetch.js   找不到模块:错误:无法在>'/ Users / stein / Development / TimeLogging / TimeLogging / node_modules / fetch / lib'中解析'dns'   在>'/ Users / stein / Development / TimeLogging / TimeLogging / node_modules / fetch / lib'中解析'dns'   解析的请求是一个模块    使用描述文件:> /Users/stein/Development/TimeLogging/TimeLogging/node_modules/fetch/package.json(相对路径:./ lib)      字段“浏览器”不包含有效的别名配置       解析为模块

     

测试如下:

import {insertTimer, loadTimer} from '../../src/lib/service/service.jsx'
describe( 'Integration tests of services that all the server', () => {
   beforeEach(()=> {

       cy.request('GET','timers')
          .its('body')
          .each(timer => cy.request('DELETE',`timers/${timer.id }` ))

     })

     it('Insert a timer to the database', () => { 

         const timer = { id: "77d3e1a5-18d1-4341-a4d2-6756427f511f" ,
                   title: "integrasjons test av kule titler"
                 }

        insertTimer(timer). then(response => {  
           expect(response.status).to.eq(201)
         })
        loadTimer.then(response => {
         expect(response.json()).to.equal(timer)
        })
    })

}

)
  

我正在测试的代码如下:

// @flow
import fetch from 'fetch'
import TimerData from '../../components/TimerData.jsx'

/**
 * Services that communicate with the servere
 */

  export const loadTimers = () => {
        return fetch('http://localhost/3030/timers')
  }

   export const insertTimer = ( timer: typeof TimerData) => {
        return fetch('http://localhost/3030/timers', {
                 method: "POST",
                 headers: {
                   "Content-Type": "application/json; charset=utf-8"
                  },
                body: JSON.stringify(timer)
               })
  }
  

package.json看起来像这样

{
  "name": "TimeLogging",
  "version": "1.0.0",
  "description": "React Time Logging",
  "main": "index.js",
  "engines": {
    "node": "10.10.0",
    "npm": "6.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "nyc --reporter=text-lcov cypress run ",
    "coverage": "nyc report --reporter=text-lcov | coveralls",
    "eject": "react-scripts eject",
    "cypress:open": "cypress open",
    "cypress:run": "cypress run",
    "eslint": "eslint",
    "mocha": "mocha",
    "nyc": "nyc",
    "flow": "flow",
    "sinon": "sinon",
    "uuid": "uuid"
  },
  "author": "CodeMix",
  "license": "ISC",
  "dependencies": {
    "prop-types": "^15.6.2",
    "react-scripts": "^2.0.4",
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-script": "^2.0.5",
    "semantic-ui-react": "^0.82.5"
  },
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-flow": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "@cypress/webpack-preprocessor": "^3.0.0",
    "ajv": "^6.5.4",
    "babel-loader": "^8.0.4",
    "coveralls": "^3.0.2",
    "cypress": "^3.1.0",
    "enzyme": "^3.6.0",
    "enzyme-adapter-react-16": "^1.5.0",
    "eslint": "^5.6.1",
    "eslint-config-standard": "^12.0.0",
    "eslint-plugin-chai-friendly": "^0.4.1",
    "eslint-plugin-cypress": "^2.0.1",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-node": "^7.0.1",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-react": "^7.11.1",
    "eslint-plugin-standard": "^4.0.0",
    "fetch": "^1.1.0",
    "flow-bin": "^0.83.0",
    "istanbul-lib-instrument": "^3.0.0",
    "nyc": "^13.0.1",
    "sinon": "^7.0.0",
    "webpack": "^4.20.2"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "node": {
    "dns": "empty",
    "net": "empty"
 }
}
  

一个json服务器正在运行

     

加载db.json完成

     

资源http://localhost:3030/timers

     

首页http://localhost:3030

     

数据库已记录

{
  "timers": [
    {
      "id": "15228f90-6a34-4672-954b-357012f74ed0",
      "title": "stored obejct"
    }
  ]
}

>

  

该如何解决此错误?

0 个答案:

没有答案