我正在尝试为prototype.js 设置测试。我想避免启动本地服务器来使用 cypress / puppeteer / 等。我的目标只是使用 jest。
我的想法是为每个测试设置一个最小值,index.html如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
</head>
<body>
<span id="el">foo foo</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我首先尝试将 jest 运行为
const fs = require('fs');
const path = require('path');
const html = fs.readFileSync(path.resolve(__dirname, './index.html'), 'utf8');
jest.dontMock('fs');
describe('$', function () {
beforeEach(() => {
document.documentElement.innerHTML = html.toString();
});
afterEach(jest.resetModules);
it('finds a node', function () {
expect($('el')).toBeInTheDocument()
});
});
Run Code Online (Sandbox Code Playgroud)
但$由于所有prototype.js全局的东西都不可用。