【问题标题】:Cannot assign to read only property '.js' of #<Object>无法分配给#<Object> 的只读属性“.js”
【发布时间】:2016-10-30 17:19:59
【问题描述】:

我正在构建一个应用程序,但在处理代理查询和测试时遇到了一些麻烦。

我有以下测试:

import proxyquire from 'proxyquire'
const fetchMock = () => new Promise((res) => {
  jobs: []
})
const JenkinsService = proxyquire('../lib/jenkins-service', {
  'node-fetch': fetchMock
})

describe('JenkinsService#getProject->goodConfs', () => {
      let service
      beforeEach(() => service = new JenkinsService({
        url: 'http://jenkins.io'
      }))

      it('should call fetch method', () => {
        service.getAll()
      })
    })

失败并抛出以下错误:

无法分配给#的只读属性“.js”

我试图简单地测试在我的 getAll 方法中调用了 fetch 模块:

'use babel'
import fetch from 'node-fetch'
import CONSTANTS from './constants'

export default class JenkinsService {

  /**
   * Create a new JenkinsService
   * @param  {Object} configs The configuration needed to access jenkins
   */
  constructor (configs) {
    this.configs = configs
  }

  /**
   * Get a remote list of projects
   * @return {Object[]} The project list
   */
  getAll () {
    if (!this.configs.url) throw new Error(CONSTANTS.NO_URL_IN_CONFS)
    return fetch(this.configs.url)
  }
}

知道出了什么问题吗?

【问题讨论】:

    标签: javascript node.js jasmine proxyquire


    【解决方案1】:

    可能的解决方案是在每个导入行的末尾添加分号

    import fetch from 'node-fetch';
    import CONSTANTS from './constants';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2019-04-27
      • 1970-01-01
      • 2019-08-30
      • 2023-03-13
      • 2021-09-26
      相关资源
      最近更新 更多