skip to Main Content

I have a project that I think its time to write tests for. In this project I load in external functions that look like this in the html:

<script src="library/VoiceRecordingHelper.js"></script>

The .js looks like this:

async function uploadVoiceRecordingToBucket(data, ticketId, cloudProvider) {
  'code here'
}

As you see im not exporting the function. I didn’t need to until now and everything works fine. If I add exports in the VoiceRecordingHelper.js another framework will fail so this is not an option.

I now have a VoiceRecordingHelper.test.js that I can run with npm test that uses jest. But I can’t find a way to import the function from the .js file, without using export or module or such. Is there a way I can keep my .js file the same, and make the function testeable in the test.js file?

Tried using export module in the .js file. This makes my fdk run fail. Tried using require(.js) in the test.js file but It still cant find the function it says.

Any help is greatly appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    Fixed this by not directly exporting the function but doing it in the bottom of the .js:

    // Export the function for testing purposes. module.exports = { uploadVoiceRecordingToBucket };

    And then I can import it without trouble: const { uploadVoiceRecordingToBucket } = require("../app/library/VoiceRecordingHelper.js");


  2. A code like this is the best way to solve your problem

    Note: this is global pollution but your code is already like that:

    self.uploadVoiceRecordingToBucket = uploadVoiceRecordingToBucket
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search