jsdom

Configuring jsdom in Jest across multiple tests without using modules

血红的双手。 提交于 2020-08-07 18:14:30
问题 I want to test scripts in an environment where we can not export modules. I have installed Jest version 23.1.0 and there aren't other packages in my package.json file. Using jsdom 'old' api I have come up with a solution that works as expected: script.js var exVar = "test"; script.test.js const jsdom = require('jsdom/lib/old-api.js'); test('old jsdom api config', function(done) { jsdom.env({ html: "<html><body></body></html>", scripts: [__dirname + "/script.js"], done: function (err, window)

JSDom 11.12.0 - how to mock localStorage?

↘锁芯ラ 提交于 2020-08-04 06:33:27
问题 Since the latest release of JSDom, I'm not able to mock localStorage anymore. I've tried the following methods: Object.defineProperty(window, 'localStorage', {value: LocalStorageMock}) window.localStorage = LocalStorageMock; jest.spyOn(window.localStorage, 'setItem') Any of those methods not worked for me, I get always the original localStorage . 回答1: I actually ran into this same issue when updating Jest, not sure if that's what happened to you but I found this fix here: https://github.com

How to test Web Component (lit-element) with jest

ⅰ亾dé卋堺 提交于 2020-07-05 03:09:31
问题 Anyone that has a good setup for testing custom elements with jest with jsdom or similar? I have been using Puppeteer ans Selenium, but they slows down the test runs too much. Any other alternatives to or fixes for jsdom that makes the below test runnable? import {LitElement} from 'lit-element'; import {html} from 'lit-html'; export class Counter extends LitElement { static get properties() { return Object.assign({}, super.properties, { count: {type: Number} }); } render() { return html`Count

How to setup jsdom when working with jest

你说的曾经没有我的故事 提交于 2020-07-04 07:51:28
问题 I'm trying to migrate from AVA to Jest. In AVA you can set ava.setup , in which you set the jsdom environment. For example, creating the DOM structure and doing necessary polyfills (localStorage). How do I accomplish that in Jest? Currently, I'm using beforeEach in each test suite, which doesn't feel like the best solution. Thanks in advance! 回答1: Great question. Jest actually ships with jsdom and the environment already configured. You can override it with the testEnvironment setting. If you

How to setup jsdom when working with jest

旧城冷巷雨未停 提交于 2020-07-04 07:50:45
问题 I'm trying to migrate from AVA to Jest. In AVA you can set ava.setup , in which you set the jsdom environment. For example, creating the DOM structure and doing necessary polyfills (localStorage). How do I accomplish that in Jest? Currently, I'm using beforeEach in each test suite, which doesn't feel like the best solution. Thanks in advance! 回答1: Great question. Jest actually ships with jsdom and the environment already configured. You can override it with the testEnvironment setting. If you

How do I point Jest testEnvironment to a node module in StencilJS Config?

若如初见. 提交于 2020-06-17 11:54:51
问题 I'm trying to set testEnvironment to "jsdom" in stencil.config.ts like this: testing: { testEnvironment: "jsdom" } However, running npm test gives me this error: [ ERROR ] runJest: ● Validation Error: Test environment C:/path/to/my/project/stencil.config.ts/jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module. Configuration Documentation: https://jestjs.io/docs/configuration.html How do I point Jest testEnvironment to a node module in

How do I point Jest testEnvironment to a node module in StencilJS Config?

风流意气都作罢 提交于 2020-06-17 11:54:31
问题 I'm trying to set testEnvironment to "jsdom" in stencil.config.ts like this: testing: { testEnvironment: "jsdom" } However, running npm test gives me this error: [ ERROR ] runJest: ● Validation Error: Test environment C:/path/to/my/project/stencil.config.ts/jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module. Configuration Documentation: https://jestjs.io/docs/configuration.html How do I point Jest testEnvironment to a node module in

How do I point Jest testEnvironment to a node module in StencilJS Config?

自闭症网瘾萝莉.ら 提交于 2020-06-17 11:53:32
问题 I'm trying to set testEnvironment to "jsdom" in stencil.config.ts like this: testing: { testEnvironment: "jsdom" } However, running npm test gives me this error: [ ERROR ] runJest: ● Validation Error: Test environment C:/path/to/my/project/stencil.config.ts/jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module. Configuration Documentation: https://jestjs.io/docs/configuration.html How do I point Jest testEnvironment to a node module in

Edit elements in the jsdom window and save the window as a new HTML file?

一个人想着一个人 提交于 2020-05-10 00:47:00
问题 I want to load an HTML file (using fs.read), load the DOM using jsdom, and then change the text of the nodes of the body (via jquery). Then I want to save the edited DOM window as an HTML file. Is there a way to do this? The code I am using is the following: fs.readFile(file, 'utf8', function(error, data) { jsdom.env(data, [], function (errors, window) { var $ = require('jquery')(window); $(document.body.getElementsByTagName("*")).each(function () { var content = $(this).text(); var word = "\

Edit elements in the jsdom window and save the window as a new HTML file?

筅森魡賤 提交于 2020-05-10 00:44:34
问题 I want to load an HTML file (using fs.read), load the DOM using jsdom, and then change the text of the nodes of the body (via jquery). Then I want to save the edited DOM window as an HTML file. Is there a way to do this? The code I am using is the following: fs.readFile(file, 'utf8', function(error, data) { jsdom.env(data, [], function (errors, window) { var $ = require('jquery')(window); $(document.body.getElementsByTagName("*")).each(function () { var content = $(this).text(); var word = "\