如何在子测试中访问Jest测试环境的类属性

如何在子测试中访问Jest测试环境的类属性

本文介绍了如何在子测试中访问Jest测试环境的类属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为开玩笑创建了一个测试环境.它非常紧密地基于他们的官方文档.

I have created a testing environment for jest. It's based very closely to their official docs.

我在构造函数中设置了一些值,这些值可以提供给环境中使用的测试使用. (请参见this.foo = bar.)

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).

// 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

在此测试环境中进行测试的测试中,我在哪里可以访问this.foo?

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');
  });
});

我尝试用es5函数格式替换两个箭头函数(希望this在范围内)并且没有任何运气.

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?

推荐答案

很遗憾,您不能这样做.我建议以与this.global.someGlobalObject = createGlobalObject();类似的方式公开foo并在setup函数内添加this.global.foo = 'bar'.然后,您可以通过调用foo在测试套件中访问此变量.

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
  });
});

这篇关于如何在子测试中访问Jest测试环境的类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:41