fedeghe
fedeghe

Reputation: 1317

test html page with jest

I'm trying to setup a test for prototype.js. I would like to avoid starting a local server to use cypress / puppeteer / etc. My goal here is just use jest.

My idea was to have for each test a minimal index.html like the following:

<!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>

and I tried first to run jest as

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()
    });
});

but $ as all the prototype.js global stuff is not available.

Upvotes: 5

Views: 4113

Answers (1)

fedeghe
fedeghe

Reputation: 1317

ouhhh .... looks like wrapping the expect into a window.onload is enough, which makes also sense.

Upvotes: 1

Related Questions