import module problem when doing js unittest/debug using jest / babel

I have a setup with jest/babel for unit-testing my js code, which works fine.

Importing a local module with 

import mustache from "./mustache.js";
console.log(mustache.version);

work fine, but fails when trying to import from another path '

import mustache from "../public_html/js/mustache.js";

With this output:

Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

D:\WebSiteRipper\_web\www\public_html\js\mustache.js:748
export default mustache;
^^^^^^

SyntaxError: Unexpected token 'export'

2 | import { hello, mis } from "./simple.js"
3 |
> 4 | import mustache from "../public_html/js/mustache.js";
| ^
5 | //import mustache from "./mustache.js";
6 | let f = mustache.version;
7 |

at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (my.test.js:4:1)


How do i solves this ?

The same file but just moves to another path.

3 comments
Comment actions Permalink

The error comes from Jest and not from the IDE. You'll probably get the same error on running it in the command line. 

Could you provide a sample project that we could try and check?

0
Comment actions Permalink

Looks as if .js files in www\public_html\js are not transformed by Jest; please check your Jest configuration file - what do transform and transformIgnorePatterns settings look like?

0
Comment actions Permalink

I found a solution by changing the export from 

export default mustache;

to CommonJS format

module.exports = mustache;

and import using require()

var mustache = require('../public_html/js/mustache')
0

Please sign in to leave a comment.