gshigeto / ionic-environment-variables

Easy to use environment variables for Ionic3!
MIT License
277 stars 36 forks source link

How to use with Unit testing with Karma #20

Closed inggil2407 closed 6 years ago

inggil2407 commented 6 years ago

I want to create unit testing but, always get "Uncaught Error: Cannot find module "@app/env""

srjovanovic commented 6 years ago

When I try to start karma unit testing, console shows errors: Module not found: Error: Can't resolve '@ app/env' in...

P.S. Compiling and building works as it should (ionic serve, ionic build cordova, ionic cordova compile, ...).

amtb commented 6 years ago

Hi,

You need to tell webpack where to find (resolve) the app/env files... I guess you have a webpack.test.js somewhere, if so, update the resolve block to look like this.

resolve: {
    alias: {
      '@app/env': path.resolve(environmentPath('dev'))
    },
    extensions: ['.ts', '.js']
  },
... 

Define your environmentPath function to return the path to your env config as in the README of the project. You can even extract it into a env-helper.js file outside of the webpack configs to avoid code duplication, and require your helper.

Hope it helps ;)

srjovanovic commented 6 years ago

Thank you very much!

Srdačan pozdrav,

Srđan Jovanović | Developer Manager Emisia Consulting, Kragujevac, Serbia

On Tue, Jan 23, 2018 at 8:04 PM, Alpha Barry notifications@github.com wrote:

Hi,

You need to tell webpack where to find (resolve) the app/env files... I guess you have a webpack.test.js somewhere, if so, update the resolve block to look like this.

resolve: { alias: { '@app/env': path.resolve(environmentPath('dev')) }, extensions: ['.ts', '.js'] }, ...

Define your environmentPath function to return the path to your env config as in the README of the project. You can even extract it into a env-helper.js file outside of the webpack configs to avoid code duplication, and require your helper.

Hope it helps ;)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/gshigeto/ionic-environment-variables/issues/20#issuecomment-359895554, or mute the thread https://github.com/notifications/unsubscribe-auth/Af_RwEVpIzqsQxpXwKW0nohgt1s3SAMrks5tNi1CgaJpZM4RhlQw .

gshigeto commented 6 years ago

I am going to close this as it seems to be resolved. Thanks!

GFoley83 commented 6 years ago

@gshigeto This info should probably be added to the README as it's likely the majority of people will be using unit tests and this needs to be inplace in order to make them work.

jhonycage commented 6 years ago

Thanks god I found this, really...