Javascript 将jQuery作为依赖项的Mocha测试模块
也有类似的问题,但似乎没有一个明确的答案,而且大多数答案都是过时的。这就是我问这个问题的原因 所以我有一个这样的测试Javascript 将jQuery作为依赖项的Mocha测试模块,javascript,jquery,mocha.js,jsdom,Javascript,Jquery,Mocha.js,Jsdom,也有类似的问题,但似乎没有一个明确的答案,而且大多数答案都是过时的。这就是我问这个问题的原因 所以我有一个这样的测试 // B2CPopup_methods_test.js import './domTestsHelper' import { expect } from 'chai' import newB2CPopup from './popupFromDbBoilerplate' describe('B2CPopup class method - ', () => { const
// 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
})
})
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 { $ }
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, $)
我的domTestsHelper.js如下所示
// 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
})
})
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 { $ }
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, $)
当然,由于这个错误,我的测试失败了
Error: jQuery requires a window with a document
在我的domTestsHelper.js中,我添加了以下行
global.window = window
global.document = window.document
global.$ = $
我的最终文件如下所示
// 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
})
})
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 { $ }
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, $)