Running commands before Jest within configuration

In order to setup my test environment I need to run the following command:

env-cmd ./config/test.env jest --watch --env=node

Within the Jest config panel I see where to add Jest options, but how am I supposed to run env-cmd before jest itself?

0

You can create a shell script that would run

env-cmd ./config/test.env

command, set it up as external tool in Settings | Tools | External Tools and then add this tool to Before launch section of your Jest run configuration

 

0

Thanks Elena!

I did try it, but I get an error message back "Cannot run program "env-cmd" (in directory "~/src/test"): error=2, No such file or directory". This script does work, so I'm clearly messing up the config.

Here is how I config that:

0

what is "env-cmd"? https://github.com/toddbluhm/env-cmd? specify a full path to the executable, seems it's installed locally and thus not added to your $PATH

0

Yes, that's the correct package! However, I'm not being lucky. If I do specify the full path: "/something/something/app/node_modules/.bin/env-cmd" i get this error: "too few arguments passed...". But if I do pass the path to the config file in the same line, the Program input, then I get the same error as before: "No such file or directory..."

0

Doesn't work indeed, as it requires a command to run (jest in your case):

Usage: env-cmd [options] [env_file | env_name] command [command options]

it's env-cmd that runs jest in your case... It's a single command, not a commands sequence, that's why it's not possible to run env-cmd before launching jest

I can only suggest specifying your environment vars directly in your run configuration, Environment variables: field

0

I see. That makes a lot of sense, should have figured out earlier. Thanks for your kind support! I guess I'll follow your suggestion :)

 

1
Avatar
Permanently deleted user

Hi, I just encountered the same problem.

Like OP, I am using env-cmd to load my environment config when im starting the program via CLI, and was looking for a way to have intelliJ pick up on my environment variables.

There is a workaround which does not require you to fiddle with your IntelliJ config everytime the env changes, and instead allows you to pickup the env vars from your .env files instead. This involves reading the vars at runtime in your test-runners setup file (in this case, setupTests.{ts/js}).

Here you have two options:

- use env-cmd's "GetEnvVars" function ( https://www.npmjs.com/package/env-cmd#getenvvars ) and write them to process.env yourself.

- use dotenv (https://www.npmjs.com/package/dotenv) to read the files and populate process.env for you:

2

请先登录再写评论。