0

I'm a C++ developer. I barely do any web related stuff. So please bear with me.

I'm using adonisjs. Everything is fine. I have a javascript file which works just fine. It does some numerical calculations.

I want to add unit tests to it. I've read a lot of questions like this: How to use ES6 modules in CommonJS? but have not been able to figure out what I need to do to use the file in my unit tests.

My last attempt was to use dynamic import, but I still get an error. This is the minimal code I have that reproduces the issue:

import { test } from '@japa/runner'

Test('seisUtils lib test', async ({ assert }) => {

  import ('../../resources/js/libs/seisUtils.mjs').then(seisUtils => {
    let signal1 = utils.ricker(100, 10, 0);
    assert.isTrue(true);
  });

})

result:

require() of ES Module

<directory>/resources/js/libs/seisUtils.mjs not supported.
Instead change the require of <directory>/resources/js/libs/seisUtils.mjs to a dynamic import() which is available in all CommonJS modules.
  
   at anonymous tests/unit/first_breaks_lib_test.spec.ts:28

I tried using webpack, but then I messed up the configuration of adonis and gave up. Maybe that's the correct way to do it?

1
  • make sure the imports is asynchronously. module.exports = async () => { const esmModule = await import('./esmModule.mjs'); return esmModule; };
    – emild_
    Commented Nov 23 at 12:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.