Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mithril v1 on the server. created for dev/testing and prod.
'use strict';
var MockBrowser = require('mock-browser').mocks.MockBrowser;
var jsdom = require('jsdom');
function propagateToGlobal (window) {
for (var key in window) {
if (!window.hasOwnProperty(key)) continue
if (global[key]) {
if (process.env.JSDOM_VERBOSE) {
console.warn("[jsdom] Warning: skipping cleanup of global['" + key + "']")
}
continue
}
global[key] = window[key]
}
}
/*
* re-throws jsdom errors
*/
function getError (errors) {
var data = errors[0].data
var err = data.error
err.message = err.message + ' [jsdom]'
// clean up stack trace
if (err.stack) {
err.stack = err.stack.split('\n')
.reduce(function (list, line) {
if (line.match(/node_modules.+(jsdom|mocha)/)) {
return list
}
line = line
.replace(/file:\/\/.*<script>/g, '<script>')
.replace(/:undefined:undefined/g, '')
list.push(line)
return list
}, []).join('\n')
}
return err
}
var ready = function(err) {
if (err) throw err;
require('./main.js');
console.log(global.document.documentElement.outerHTML);
};
jsdom.env({
url: 'http://localhost:/',
globalize: true,
console: true,
useEach: false,
skipWindowCheck: false,
html: "<!doctype html><html><head><meta charset='utf-8'></head>" +
'<body></body></html>',
done: function(errors, window) {
var mock = new MockBrowser({
window: window,
});
global.window = mock.getWindow();
global.document = mock.getDocument();
// global.window.location = mock.getLocation();
// global.window.navigator = mock.getNavigator();
// global.window.history = mock.getHistory();
// global.window.localStorage = mock.getLocalStorage();
// global.window.sessionStorage = mock.getSessionStorage();
if (errors) {
return ready(getError(errors))
}
ready(null)
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment