Rxjs: Testing with jsdom crashes on root.Object.assign

Created on 9 Jun 2017  路  3Comments  路  Source: ReactiveX/rxjs

RxJS version: 5.4

node_modules/rxjs/util/assign.js:22
    return root.Object.assign || assignImpl;
                      ^
TypeError: Cannot read property 'assign' of undefined

Code to reproduce: import { Observable } from 'rxjs/Rx'; with

setup.js

require('babel-register')();
import chai from 'chai';
chai.config.includeStack = true;

/* In case we want to use JSDOM */

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

function copyProps(src, target) {
    const props = Object.getOwnPropertyNames(src)
        .filter(prop => typeof target[prop] === 'undefined')
            .map(prop => Object.getOwnPropertyDescriptor(src, prop));
              Object.defineProperties(target, props);
}

global.window = window;
global.document = window.document;
global.navigator = {
    userAgent: 'node.js'
};
copyProps(window, global);

Command line:

cross-env BABEL_ENV=test mocha --harmony --compilers js:babel-register --require tests/helpers/nullCompiler.js --require intl tests/helpers/setup.js $(find app -name '*.test.js')

Similar to http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/

Expected behavior: Object should exist, because we fill it in.

Actual behavior: Object is undefined.

Additional information:

Most helpful comment

@haf just run into the same problem, I simply extended the global window with Object and Math
hope it helps

import { JSDOM } from 'jsdom';
import canvas from 'canvas-prebuilt';

const DOM = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = DOM;

window.Object = Object;
window.Math = Math;

global.canvas = canvas;
global.window = window;
global.document = window.document;
global.navigator = {
    userAgent: 'node.js',
};

function copyProps(src, target) {
    const props = Object.getOwnPropertyNames(src)
        .filter(prop => typeof target[prop] === 'undefined')
        .map(prop => Object.getOwnPropertyDescriptor(src, prop));
    Object.defineProperties(target, props);
}
copyProps(window, global);

All 3 comments

I don't think this can be amended by Rx.

What Rx's root does is trying to find global context, with simple fallbacks of __window || __global || __self - looking for window context first, then Node.JS's global, then self.

If you assert status of global, you can observe below -
image

global still has Object, but your custom window object doesn't have it and since you overrided root to emulate browser environment, Rx can't fall backs into global but looking for window object which doesn't have it.

Your fixture setup should correctly provide if you intend to override environment.

@haf just run into the same problem, I simply extended the global window with Object and Math
hope it helps

import { JSDOM } from 'jsdom';
import canvas from 'canvas-prebuilt';

const DOM = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = DOM;

window.Object = Object;
window.Math = Math;

global.canvas = canvas;
global.window = window;
global.document = window.document;
global.navigator = {
    userAgent: 'node.js',
};

function copyProps(src, target) {
    const props = Object.getOwnPropertyNames(src)
        .filter(prop => typeof target[prop] === 'undefined')
        .map(prop => Object.getOwnPropertyDescriptor(src, prop));
    Object.defineProperties(target, props);
}
copyProps(window, global);

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings