I'd like to use Jest as a testing framework to kick off a suite of Integration tests, and inside each test, I need to access a SALT - which will be passed in via a HTTP request, the same request which will eventually call:
import * as jest from 'jest'
const res = await jest.runCLI(cliArgv, domainPaths)
Without wanting to include the entire cliArgv
, some include:
cliArgv['projects'] =['somePathTo/jest-config.js']
cliArgv['globalSetup'] = setupScriptPath
cliArgv['globalTeardown'] = teardownScriptPath
cliArgv['testEnvironment'] = path.join(__dirname, 'custom-jest-environment.js')
I've tried many options to try and pass environment variables passed into the HTTP endpoint, including:
cliArgv['globals']
objectglobal.SOME_VARIABLE
option.for context, this is the inside of the custom environment, pretty basic:
import NodeEnvironment from 'jest-environment-node'
class CustomTestEnvironment extends NodeEnvironment {
globalConfig: any
constructor(config: any, context: any) {
super(config, context)
this.globalConfig = config
}
async setup() {
await super.setup()
console.log(`this.globalConfig`, this.globalConfig)
}
async teardown() {
await super.teardown()
}
}
module.exports = CustomTestEnvironment
But what's happening, is no mapper what I pass in, the this.globalConfig
log line will only show me {}
even if I pass in something like:
globals: {
SALT: mySalt,
__DEV__: true
},
It turns out, the process is taking the globals
argument from what you've specified inside of the jest.config.ts
- which you cannot dynamically pass arguments into.
I am at a loss as to how to pass in a variable passed into the running environment from a HTTP controller, and can flow through the jestCLI()
and be used inside a running test.
It turns out that you have to pass in a string
into globals
that is JSON
serialisable.
For example:
globals: JSON.stringify({ __TEST_SALT__: salt, OTHER: 'THIS IS ANOTHER Variable' })