jsdom

How to test JavaScript with DOM elements using jasmine?

百般思念 提交于 2019-12-09 19:30:43
问题 I am trying to write a test in jasmine for JavaScript code including DOM elements which doesn't work. It works when I test only for JavaScript code (just simple functions but not for DOM elements). I have been researching for so long and can't really find the answer. I also tried to use jsdom as it's supposed to be used when testing for DOM elements, however I keep getting a bunch of errors. Is there any 'easy' way to test DOM elements only using jasmine? JavaScript: var result; var adding =

Does Jest reset the JSDOM document after every suite or test?

可紊 提交于 2019-12-09 09:01:34
问题 I'm testing a couple of components that reach outside of their DOM structure when mounting and unmounting to provide specific interaction capability that wouldn't be possible otherwise. I'm using Jest and the default JSDOM initialization to create a browser-like environment within node. I couldn't find anything in the documentation to suggest that Jest reset JSDOM after every test execution, and there's no explicit documentation on how to do that manually if that is not the case. My question

jsdom: dispatchEvent/addEventListener doesn't seem to work

被刻印的时光 ゝ 提交于 2019-12-09 03:03:46
问题 Summary: I am attempting to test a React component that listens to native DOM events in its componentWillMount . I'm finding that jsdom ( @8.4.0 ) doesn't work as expected when it comes to dispatching events and adding event listeners. The simplest bit of code I can extract: window.addEventListener('click', () => { throw new Error("success") }) const event = new Event('click') document.dispatchEvent(event) throw new Error('failure') This throws "failure". Context: At risk of the above being

How to use jsdom.jQueryify with jasmine-node?

人盡茶涼 提交于 2019-12-07 22:58:34
问题 Is it possible to user jasmine-node with the jQueryify feature of jsdom? What I'm trying to do is use NodeJS to test some JavaScript that depends on the presence of the DOM. Here is a reduced case of what I tried. When I run the script, jasmine-node recognizes the spec, but doesn't run the expect() : var fs = require('fs'), jsdom = require('jsdom'), window = jsdom.createWindow(), jasmine = require('jasmine-node') describe("jQueryify", function() { it('should run the test!', function () {

Access to DOM using node.js

自作多情 提交于 2019-12-07 05:56:58
问题 i want to access to html file and get an element by id using node.js, this is my html file : <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Diagram </title> <script> function generatePNG (oViewer) { // some other code reader.onloadend = function() { base64data = reader.result; var image = document.createElement('img'); image.setAttribute("id", "GraphImage"); image.src = base64data; document.body.appendChild(image); } }, "image/png", oImageOptions); return sResult; var sResult =

Set window width in jsDom?

倖福魔咒の 提交于 2019-12-07 01:46:53
问题 Should be a simple question. How do I set the width in a jsDom object? jsdom.env({ url:'http://testdatalocation', scripts: ['http://code.jquery.com/jquery.js'], done: function(errors, tstWindow) { console.log(tstWindow.innerWidth); }; } }); I can't figure out how to get the "innerWidth" to be anything but 1024 回答1: There isn't currently a formal option or API for doing so. The values of innerWidth and similar properties are simply set to literal values: DOMWindow.prototype = createFrom(dom ||

npm install <module> persistent error ? (node-gyp build ?)

我的梦境 提交于 2019-12-07 01:01:09
问题 I try to install jsdom such: $ sudo npm install -g jsdom # OR $ sudo npm install jsdom After some successfull command, the install quickly fails, with the first error message being after the [....] : $ sudo npm install jsdom npm http GET https://registry.npmjs.org/jsdom npm http 304 https://registry.npmjs.org/jsdom [....] > contextify@0.1.7 install /home/yug/Desktop/QGis/WikiAtlas/1_shaded_relief/test/node_modules/jsdom/node_modules/contextify > node-gyp rebuild gyp: /home/yug/.node-gyp/0.10

How can I configure the jsdom instance used by jest?

自闭症网瘾萝莉.ら 提交于 2019-12-07 00:06:09
问题 I've come up against this issue Invalid URL is thrown when requiring systemjs in jest test cases One of the last comments suggests "manipulate the jsdom instance to have a valid location / baseURI by setting the referrer config in jsdom." I'm wondering is there way for me to do that? Can I access the jsdom instance somehow from the jest object? 回答1: I had a similar issue when using a project requiring a url (location.href). You can configure jest with a testURL in your configuration. Here is

Script onload happens after window onload in JSDOM

偶尔善良 提交于 2019-12-06 17:41:28
问题 This is a new question arising from what I learnt in: Is order of script onload and window.onload well defined when the script is a DOM node created dynamically from a loaded script? In the previous question we learnt that when a window is loading scripts, any scripts (the one directly loaded as well as the ones dynamically being loaded by the script) would finish loading first and only after that the window.onload will fire. But JSDOM seems to be behaving differently. Here is the loader.js

Loading existing HTML file with JSDOM for frontend unit testing

Deadly 提交于 2019-12-06 09:09:45
问题 I'm new to unit testing, and I'm aware my tests may not be valuable or following a specific best practice, but I'm focused on getting this working, which will allow me to test my frontend code using JSDOM. const { JSDOM } = require('jsdom'); const { describe, it, beforeEach } = require('mocha'); const { expect } = require('chai'); let checkboxes; const options = { contentType: 'text/html', }; describe('component.js', () => { beforeEach(() => { JSDOM.fromFile('/Users/johnsoct/Dropbox