edbentley
edbentley

Reputation: 331

Static files in Jest tests

In my code that's being tested I'm creating an image like:

const image = new Image();
image.addEventListener("load", () => {});
image.addEventListener("error", () => {});
image.src = "hello.png"

When I run in the browser I have webpack copy that image to the dist directory and it works fine. In jest however I get the error Error: Could not load img: "http://localhost/hello.png".

Any guidance on how to "serve" a static image like this in Jest? Where should the image be located in relation to my test file?

(note I've added testEnvironmentOptions: { resources: 'usable' } in the jest config)

Upvotes: 1

Views: 2285

Answers (1)

edbentley
edbentley

Reputation: 331

Found a solution: set the testURL to a local port and then serve the files with http-server / express or something else similar on that port.

Upvotes: 1

Related Questions