2017-09-27 12 views
0

이와 비슷한 질문이 있지만 확실한 답이없는 것 같고 대부분의 대답은 오래되고 더 이상 사용되지 않습니다. 그게 내가 왜 묻는 지.종속성으로 jQuery가있는 모카 테스트 모듈

그래서 나는이

// B2CPopup_methods_test.js 
import './domTestsHelper' 
import { expect } from 'chai' 
import newB2CPopup from './popupFromDbBoilerplate' 

describe('B2CPopup class method - ',() => { 
    const popup = newB2CPopup 

    it('prependHTMLToDOM prepends html to dom',() => { 
    popup.prependHTMLToDOM() 
    expect($('.b2cPopupOverlay')).to.exist 
    }) 
}) 

domTestsHelper.js 같은 시험이

import jquery from 'jquery' 
import jsdom from 'jsdom' 
import chai from 'chai' 
import chaiJquery from 'chai-jquery' 

// Set up testing environment to run like a browser in the command line 
const { JSDOM } = jsdom 
const { window } = new JSDOM('<!doctype html><html><body></body></html>') 
const $ = jquery(window) 

chaiJquery(chai, chai.util, $) 

export { $ } 

마지막으로 내가 테스트하고있는 파일이

// B2CPopup.js 
import $ from 'jquery' 

export default class B2CPopup { 
    constructor ({ _id, options }, html) { 
    this._id = _id 
    this.options = options 
    this.html = html 
    } 

    start =() => {} 
    createOptions =() => {} 
    prependHTMLToDOM =() => $(this.html).prependTo('body') 

    show =() => {} 
} 

처럼 보이는 그리고 물론이 내 이 오류로 인해 테스트가 실패했습니다

내 domTestsHelper.js에서
Error: jQuery requires a window with a document 

답변

0

나는이 라인을 추가 내 최종 파일이

import jquery from 'jquery' 
import jsdom from 'jsdom' 
import chai from 'chai' 
import chaiJquery from 'chai-jquery' 

// Set up testing environment to run like a browser in the command line 
const { JSDOM } = jsdom 
const { window } = new JSDOM('<!doctype html><html><body></body></html>') 
const $ = jquery(window) 

global.window = window 
global.document = window.document 
global.$ = $ 

chaiJquery(chai, chai.util, $) 
과 같은

global.window = window 
global.document = window.document 
global.$ = $