-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add ENV variables to the extension? #492
Comments
I have the same problem. I'm running tests with this command |
You can set environment variables in jest.config.js ie:
I didn't found any direct way to set the env variables directly within the extension. |
@ejose19 are you sure about that? I tried setting the Ideally this plugin would use VS Code launch configurations, which we could edit to include environment variables. |
It would be really nice to have options that work like |
@sanderploegsma , this worked for me at the top of jest.config.js: process.env.NODE_ICU_DATA = 'node_modules/full-icu' |
@sanderploegsma Setting |
@ab-pm @sanderploegsma @thccorni Example:
|
@AlirezaInGitHub Iirc that's exactly what I tried, but that only adds a new "Jest all" launch option, it does not affect the individual test runs that show up in the code lens next to the respective test code. |
I had the same problem, when saving the test file it used .env instead of .env.testing, all I did was import the variables from this file before anything in my test file. require('dotenv').config({
path: '.env.testing'
})
const request = require('supertest')
const app = require('../src/app')
test('Should sign up a new user', async () => {
await request(app)
.post('/users')
.send({
name: 'myName',
email: '[email protected]',
password: '12345678'
})
.expect(201)
}) |
I tried the above-suggested method and it worked. |
How about configuring |
I just solved the same problem, but in a slightly different way. I launch VSCode from the terminal with the So in a terminal with all of your desired ENV vars already loaded, and in the project directory just run Works for me on Mac OS. I'd be curious to know if this is the same for everyone. |
FYI all, you can do this using Jest's |
It's much easier if you run tests in container with bash. Just add to "script" section in your package.json file: "test": "export $(cat ./build/.env.test) && jest". Change "./build/.env.test" to your directory with .env. |
@nubelsondev pointed out to a valid solution. Adding
in the test and having specific env variables in a file works. I also tried adding it more globally to my jest tests in the jest.config.js file, and it works too:
|
What worked for me was to use |
Here is my solution, without changing we know vscode-jest runs by running command so open the file add you custome logic here like(Here I'm using #!/usr/bin/env node
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
++ require('dotenv').config({ path: '.env.test' });
const importLocal = require('import-local');
if (!importLocal(__filename)) {
require('jest-cli/bin/jest');
} |
Thanks for several posts above. |
This one solved my problem using VSCode Jest extension ( # package.json
{
"scripts" {
"test": "env-cmd -f ./my-test.env node ./node_modules/.bin/jest"
}
} |
Environment
node -v
: v12.6.0npm -v
: 6.9.0npm ls jest
ornpm ls react-scripts
(if you haven’t ejected): [email protected] & [email protected]your vscode-jest settings if customized:
Operating system: Ubuntu 18.04
Prerequisite
npm run test
ornode_modules/.bin/jest
) npm run testProblem
Hi, I'm trying to use the extension, but all my tests fails because there's missing ENV variables required to make my tests work. My test script looks like this
dotenv -e .env.test jest
and it works fine, but not with the extension. I tried usingjest.pathToJest
and set it tonpm run test --
, but I run into the #316 issue, because I'm using ts-jest. Is there another alternative to my problem?Thank you.
The text was updated successfully, but these errors were encountered: