最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

es6 modules - how to use ES6 file in CommonJS using Adonisjs - Stack Overflow

programmeradmin3浏览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?

发布评论

评论列表(0)

  1. 暂无评论