babel-jest

Does Jest support ES6 import/export?

ぃ、小莉子 提交于 2019-11-27 12:45:31
问题 If I use import/export from ES6 then all my jest tests fail with error: Unexpected reserved word I convert my object under test to use old school IIFY syntax and suddenly my tests pass. Or, take an even simpler test case: var Validation = require('../src/components/validation/validation');//PASS //import * as Validation from '../src/components/validation/validation'//FAIL Same error. Obviously there's a problem with import/export here. It's not practical for me to rewrite my code using ES5

Mocking globals in Jest

a 夏天 提交于 2019-11-27 09:00:27
Is there any way in Jest to mock global objects, such as navigator , or Image *? I've pretty much given up on this, and left it up to a series of mockable utility methods. For example: // Utils.js export isOnline() { return navigator.onLine; } Testing this tiny function is simple, but crufty and not deterministic at all. I can get 75% of the way there, but this is about as far as I can go: // Utils.test.js it('knows if it is online', () => { const { isOnline } = require('path/to/Utils'); expect(() => isOnline()).not.toThrow(); expect(typeof isOnline()).toBe('boolean'); }); On the other hand,

Mocking globals in Jest

末鹿安然 提交于 2019-11-26 17:47:43
问题 Is there any way in Jest to mock global objects, such as navigator , or Image *? I've pretty much given up on this, and left it up to a series of mockable utility methods. For example: // Utils.js export isOnline() { return navigator.onLine; } Testing this tiny function is simple, but crufty and not deterministic at all. I can get 75% of the way there, but this is about as far as I can go: // Utils.test.js it('knows if it is online', () => { const { isOnline } = require('path/to/Utils');

Mocking `document` in jest

寵の児 提交于 2019-11-26 16:13:32
问题 I'm trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I'm facing an issue while loading the js file. I have followed a piece of code where document object has a currentScript object. But in test context it is null . So I was thinking of mocking same. But jest.fn() is not really help in same. How can I handle this issue? Piece of code where jest is failing. var currentScriptElement = document._currentScript || document.currentScript; var