2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
domdambrogia
  • 2,054
  • 24
  • 31
  • 2
    Unfortunately, you can't. You'd have to expose it in your setup func as `this.global.foo = 'bar'` then you can access it in your test suite by just calling `foo`. – Dom Feb 06 '19 at 01:07
  • 1
    Not what I was *hoping* to hear, but I have tested and confirmed this is a viable solution. If you would like to write this as an answer I'd be glad to mark it as accepted. Thank you for the help! – domdambrogia Feb 06 '19 at 18:04
  • Anytime! Let me know if you run into any issues. – Dom Feb 06 '19 at 18:57

2 Answers2

4

Unfortunately, you can't. I'd recommend exposing foo in a similar manner as this.global.someGlobalObject = createGlobalObject(); and add this.global.foo = 'bar' within the setup function. You can then access this variable within your test suites by calling foo.

// my-custom-environment
const NodeEnvironment = require('jest-environment-node');

class CustomEnvironment extends NodeEnvironment {
  constructor(config, context) {
    super(config, context);
    this.testPath = context.testPath;
  }

  async setup() {
    await super.setup();
    await someSetupTasks(this.testPath);
    this.global.someGlobalObject = createGlobalObject();
    this.global.foo = 'bar'; // <-- will make foo global in your tests
  }

  async teardown() {
    this.global.someGlobalObject = destroyGlobalObject();
    await someTeardownTasks();
    await super.teardown();
  }

  runScript(script) {
    return super.runScript(script);
  }
}

module.exports = CustomEnvironment;

Then within your test suite:

// test suite
describe('Sample Test', () => {
  it('foo = bar', () => {
    expect(foo).toBe('bar'); // <-- foo since it's globally accessible 
  });
});
Dom
  • 38,906
  • 12
  • 52
  • 81
0

Another potential solution is add a set function to your constructor.

setThis(key, val) {
   if (process.env.TEST) this[key] = val
}

And perhaps build the same for a getThis()

Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75