I have created a testing environment for jest. It's based very closely to their official docs.
I am setting a few values in the constructor that I would like to make available to the tests that are used within the environment. (See this.foo = bar
).
Testing Env:
// my-custom-environment
const NodeEnvironment = require('jest-environment-node');
class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
this.testPath = context.testPath;
this.foo = 'bar'; // Trying to access
}
async setup() {
await super.setup();
await someSetupTasks(this.testPath);
this.global.someGlobalObject = createGlobalObject();
}
async teardown() {
this.global.someGlobalObject = destroyGlobalObject();
await someTeardownTasks();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = CustomEnvironment;
I run my tests using the equivalent of:
jest --env ./tests/<testing-env>.js
Where do I access this.foo
within my tests that are tested within this testing environment?
describe('Sample Test', () => {
it('this.foo = bar', () => {
expect(this.foo).toBe('bar');
});
});
I tried replacing both arrow functions with es5 function formats (hoping that this
would be in scope) and didn't have any luck.
How can I get class properties from my testing environment from within my tests in that environment?