3

I'm writing a web component using StencilJS. One of the helper classes (ArrayHelper.ts) which is imported by my component has these import at the top:

import transform from 'lodash/transform';
import isEmpty from 'lodash/isEmpty';
import isEqual from 'lodash/isEqual';
import isObject from 'lodash/isObject';

this all works fine when compiling and serving locally. However, when writing some unit tests and executing them using jest, jest can't resolve these imports properly:

TypeError: isObject_1.default is not a function at Function.Object. 
<anonymous>.ArrayHelper.toArray (/mycomponent/ArrayHelper.ts:15:35)

When I change the imports in ArrayHelper.ts to

import { isEmpty, isEqual, isObject, transform } from 'lodash';

then Jest will succesfully run the tests without problems but then the TS compilation by stencilJS doesn't work any more:

Missing Export: mycomponents/ArrayHelper.js:5:27
'isObject' is not exported by node_modules/lodash/lodash.js

Any ideas how to get these imports right for both situations?

Canta
  • 1,480
  • 1
  • 13
  • 26
Niels Boogaard
  • 116
  • 1
  • 5

3 Answers3

1

Try import lodash like this

import * as _ from "lodash";
jesse._z
  • 139
  • 1
  • 2
0

Try adding this to your stencil.config.js:

nodeResolve: {
  browser: true
}

This is happening because lodash is not available where Jest is running your tests. Is it an experimental browser technology.

Refer - Link1 Link2

Prasheel
  • 985
  • 5
  • 22
-2
import * as transform from 'lodash/transform';
import * as isEmpty from 'lodash/isEmpty';
import * as isEqual from 'lodash/isEqual';
import * as isObject from 'lodash/isObject';
Michael Motúz
  • 167
  • 1
  • 4
  • 13
  • Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your answer and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Jan 12 '23 at 13:37