在JSDOM v11中使用jQuery
从v10开始,jsdom提供了更灵活的API。
https://swashata.me/blog/use-jquery-jsdom-v11/
const testHTML = `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" id="fiptest">
</body>
</html>
`;
const { JSDOM } = require( 'jsdom' );
const jsdom = new JSDOM( testHTML );
// Set window and document from jsdom
const { window } = jsdom;
const { document } = window;
// Also set global window and document before requiring jQuery
global.window = window;
global.document = document;
const $ = global.jQuery = require( 'jquery' );
console.log( `jQuery ${jQuery.fn.jquery} working! Yay!!!` );
const inputElement = $( '#fiptest' );
console.log( inputElement.length );