Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loadFixtures method in jest.js

Tags:

jquery

jestjs

Is there an equivalent method to loadFixtures method in jest. I can't seem to find anything in the jest docs. I'd like to be able to load a html fixture into jest test file ?

Or is there another way of doing html stub in jest that I'm missing ?

Note i'm not using React as its an old project with Jquery.

So instead of writing something like.

window.$ = require('jquery');

beforeEach(() => {


    document.body.innerHTML =
        '<div>' +
        ' <input id="exMonth" value="02" />' +
        ' <input id="exYear" value="2017" />'
        '</div>';

});


test("exMonth should be 02", () =>{

    expect($('#exMonth').val()).toBe('01');

});

I'd like to abstract my html out to a html fixture file and require it to the

document.body.innerHTML = require(myHtmlFixture.html)
like image 991
me-me Avatar asked Sep 10 '25 10:09

me-me


1 Answers

You could use node's fs for serverside tests.

var fs = require('fs');

var htmlFixture = fs.readFileSync('spec/fixtures/myFixture.html')

document.body.innerHTML = htmlFixture;

see: https://nodejs.dev/learn/reading-files-with-nodejs

like image 119
Sleeves Avatar answered Sep 13 '25 00:09

Sleeves