Is it possible to make an asset http fetchable as part of a Jest test












1















Is it possible to make a test asset (such as a pdf) accessible via http during a test run using Jest for a webpack project?



To give context, I'm writing a test for pdf.js api where normally I'd provide a http url. But when I run a test via Jest the file I'm providing is in the local filing system. Use of the "file" protocol with a relative path would be a CORs vialoation. If I were manually testing I could make it statically available as part of the wepback build, but of course I'm running a Jest test so that doesn't apply.



Added note: I bypassed http url problem by passing a blob represented in base64 into pdf.js getDocument. This solution works pretty well as the string is pretty succinct. However I'm still interested in figuring out how to access http resources through Jest in case I run into something like this again.










share|improve this question

























  • Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

    – Ninh Pham
    Nov 19 '18 at 16:10
















1















Is it possible to make a test asset (such as a pdf) accessible via http during a test run using Jest for a webpack project?



To give context, I'm writing a test for pdf.js api where normally I'd provide a http url. But when I run a test via Jest the file I'm providing is in the local filing system. Use of the "file" protocol with a relative path would be a CORs vialoation. If I were manually testing I could make it statically available as part of the wepback build, but of course I'm running a Jest test so that doesn't apply.



Added note: I bypassed http url problem by passing a blob represented in base64 into pdf.js getDocument. This solution works pretty well as the string is pretty succinct. However I'm still interested in figuring out how to access http resources through Jest in case I run into something like this again.










share|improve this question

























  • Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

    – Ninh Pham
    Nov 19 '18 at 16:10














1












1








1


1






Is it possible to make a test asset (such as a pdf) accessible via http during a test run using Jest for a webpack project?



To give context, I'm writing a test for pdf.js api where normally I'd provide a http url. But when I run a test via Jest the file I'm providing is in the local filing system. Use of the "file" protocol with a relative path would be a CORs vialoation. If I were manually testing I could make it statically available as part of the wepback build, but of course I'm running a Jest test so that doesn't apply.



Added note: I bypassed http url problem by passing a blob represented in base64 into pdf.js getDocument. This solution works pretty well as the string is pretty succinct. However I'm still interested in figuring out how to access http resources through Jest in case I run into something like this again.










share|improve this question
















Is it possible to make a test asset (such as a pdf) accessible via http during a test run using Jest for a webpack project?



To give context, I'm writing a test for pdf.js api where normally I'd provide a http url. But when I run a test via Jest the file I'm providing is in the local filing system. Use of the "file" protocol with a relative path would be a CORs vialoation. If I were manually testing I could make it statically available as part of the wepback build, but of course I'm running a Jest test so that doesn't apply.



Added note: I bypassed http url problem by passing a blob represented in base64 into pdf.js getDocument. This solution works pretty well as the string is pretty succinct. However I'm still interested in figuring out how to access http resources through Jest in case I run into something like this again.







javascript webpack jestjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 20:41









skyboyer

3,53111128




3,53111128










asked Nov 19 '18 at 15:43









Greg MalcolmGreg Malcolm

8911022




8911022













  • Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

    – Ninh Pham
    Nov 19 '18 at 16:10



















  • Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

    – Ninh Pham
    Nov 19 '18 at 16:10

















Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

– Ninh Pham
Nov 19 '18 at 16:10





Expose your loader to a sepearted file. And mock it when you run tests. getDocument accepts both URL and Uint8Array data binary. You might use Node.js API to read the local PDF files as binary and load it into pdfjs.

– Ninh Pham
Nov 19 '18 at 16:10












1 Answer
1






active

oldest

votes


















1














Since it's a unit test, you shouldn't perform any http requests, but mock them instead, using something like moxios.



I recommend you to import a predefined example file from your filesystem and make the mocked http return its content.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378138%2fis-it-possible-to-make-an-asset-http-fetchable-as-part-of-a-jest-test%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Since it's a unit test, you shouldn't perform any http requests, but mock them instead, using something like moxios.



    I recommend you to import a predefined example file from your filesystem and make the mocked http return its content.






    share|improve this answer




























      1














      Since it's a unit test, you shouldn't perform any http requests, but mock them instead, using something like moxios.



      I recommend you to import a predefined example file from your filesystem and make the mocked http return its content.






      share|improve this answer


























        1












        1








        1







        Since it's a unit test, you shouldn't perform any http requests, but mock them instead, using something like moxios.



        I recommend you to import a predefined example file from your filesystem and make the mocked http return its content.






        share|improve this answer













        Since it's a unit test, you shouldn't perform any http requests, but mock them instead, using something like moxios.



        I recommend you to import a predefined example file from your filesystem and make the mocked http return its content.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 15:51









        rubentdrubentd

        1,077821




        1,077821






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378138%2fis-it-possible-to-make-an-asset-http-fetchable-as-part-of-a-jest-test%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?