Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:尝试将已存根的滚动条换行到_Javascript_Node.js_Mocha.js_Sinon_Chai - Fatal编程技术网

Javascript TypeError:尝试将已存根的滚动条换行到

Javascript TypeError:尝试将已存根的滚动条换行到,javascript,node.js,mocha.js,sinon,chai,Javascript,Node.js,Mocha.js,Sinon,Chai,我最近为react组件编写了一个测试,它在单击按钮时将窗口滚动到顶部 我编写的测试文件在NodeJS 6.9上没有错误,不幸的是,当我升级到NodeJS 7.4时,我开始收到标题中提到的错误 在过去的两天里,我一直在阅读并尝试各种方法/调试这个问题,但我对此束手无策。非常感谢您的帮助 该问题的所有其他相关但非必要信息可在其回购协议中查看: test.js import { jsdom } from 'jsdom'; var exposedProperties = ['window', 'na

我最近为react组件编写了一个测试,它在单击按钮时将窗口滚动到顶部

我编写的测试文件在NodeJS 6.9上没有错误,不幸的是,当我升级到NodeJS 7.4时,我开始收到标题中提到的错误

在过去的两天里,我一直在阅读并尝试各种方法/调试这个问题,但我对此束手无策。非常感谢您的帮助

该问题的所有其他相关但非必要信息可在其回购协议中查看:

test.js

import { jsdom } from 'jsdom';

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('<!doctype html><html><body style="height:2000px"></body></html>');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    global[property] = document.defaultView[property];
  }
});
global.navigator = {
  userAgent: 'node.js'
};

require('raf').polyfill()
import React from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';
import TestUtils from 'react-addons-test-utils';
import ScrollUpButton from '../react-scroll-up-button';
import { expect } from 'chai';
//Testing group
describe('Testing <ScrollUpButton/> Action scroll to default top:', ()=>{
  let Component, ScrollTo_Stub;
  before(()=>{
    window.pageYOffset = 0
    //Render component
    Component = mount(<ScrollUpButton />);
    //Settup stub and replace scrollTo function with ours.
    // TypeError: Attempted to wrap scrollTo which is already stubbed
    ScrollTo_Stub = sinon.stub(window, 'scrollTo').callsFake((x,y)=>{
      window.pageXOffset = x;
      window.pageYOffset = y;
    Component.instance().HandleScroll(); // <-- call HandleScroll so the test can simulate the button being toggled
    });
  })
  after(()=>{
    ScrollTo_Stub.restore(); // <-- Restore the objects method
    //window.scrollTo.restore(); // <-- Restore the objects method
  })
  //did it scroll the page up
  it('did scroll the page to 0', (done) => {
    expect(Component.state().ToggleScrollUp).to.equal(''); // <-- Is the button hidden
    window.pageYOffset = 300 // <-- scroll window down to prepare for smulation
    Component.instance().HandleScroll(); // <-- call handleScroll since we scrolled the window down.
    expect(Component.state().ToggleScrollUp).to.equal(true); // <-- Is the button visible
    Component.instance().HandleClick(); // <-- call HandleClick to start the scroll up simulation.
    //Well wait a little bit to let the simulation complete
    setTimeout(()=>{
      expect(ScrollTo_Stub.lastCall.args[1]).to.within(-10,10); // <-- is pageYOffset between -10 and 10
      expect(Component.state().ToggleScrollUp).to.equal(''); // <-- Button should be hidden again
      done() // <-- since were asynchronous with setTimeout instruct chai that were done with the test.
    }, 500);
  });
});
我在这一行之前和之后都有consoled.log window.scrollTo。结果完全一样。所以我不知道为什么这会让代码工作

window.scrollTo = null;