【问题标题】:JEST: Test case inside a function not getting values from the constructorJEST:函数内的测试用例未从构造函数获取值
【发布时间】:2021-01-16 13:08:03
【问题描述】:

我尝试编写包装在类中的测试用例,以便调用方法将执行测试用例。但是由于依赖因素,url 值将在 beforeAll/beforeEach 块内初始化。在那种情况下,我没有得到 url 值,这会导致测试用例执行失败。我也无法将 url 作为参数传递(url 只会在 beforeAll 块中初始化)。是否有任何替代方案可以解决此问题?

sampleTest.ts

export interface TestCaseArgumentsType {
  baseUrl: string;
  url: string;
}
export class Sample {
  set args(value: TestCaseArgumentsType) {
    this.arguments = value;
  }
  private arguments!: TestCaseArgumentsType;
  sampleTestFunction() {
    console.log(this.arguments.url); // **expected**: sampleUrl **actual**: cannot set property url of undefined

    it('to check the before each execution effects in the test case', () => {
      console.log(this.arguments.url); // sampleUrl
    });
  }
}

sampleTestSuite.test.ts

import { Sample, TestCaseArgumentsType } from './sampleTest';

describe('User Route', () => {
  let sample = new Sample();
  // Test suite
  describe('GET Request', () => {
    // Preparing Test Suite
    beforeAll(async () => {
      sample.args = <TestCaseArgumentsType>{ url: `sampleUrl` };
    }, 20000);
    // Executing
    sample.sampleTestFunction();
  });
});

【问题讨论】:

  • 我执行了你的代码,它运行良好。
  • @slideshowp2 你得到了测试用例中的 url 吗?我遇到了一个错误。
  • 我得到了sampleUrl for console.log(this.arguments.url) 并且测试通过了
  • 你是如何运行这段代码的。通过安装 jest?
  • test-jestjest

标签: javascript node.js typescript jestjs


【解决方案1】:

原因是代码的执行顺序。

关键是beforeAll函数在调用it之前执行,而不是在调用sample.sampleTestFunction()之前。因此,当您调用sample.sampleTestFunction() 方法时,beforeAll 函数内部的语句sample.args = &lt;TestCaseArgumentsType&gt;{ url: 'sampleUrl' } 将不会执行。 samplearguments 属性是 undefined。这就是你得到错误的原因:

TypeError: 无法读取未定义的属性“url”

jestjs测试运行器准备调用it时,测试运行器将首先调用beforeAll函数

sampleTest.ts:

export interface TestCaseArgumentsType {
  baseUrl: string;
  url: string;
}
export class Sample {
  set args(value: TestCaseArgumentsType) {
    this.arguments = value;
  }
  private arguments!: TestCaseArgumentsType;
  sampleTestFunction() {
    console.log('===execute 2===');
    console.log(this.arguments.url); // another sampleUrl

    it('to check the before each execution effects in the test case', () => {
      console.log('===execute 4===');
      console.log(this.arguments.url); // sampleUrl
    });
  }
}

sampleTestSuite.test.ts:

import { Sample, TestCaseArgumentsType } from './sampleTest';

describe('User Route', () => {
  let sample = new Sample();
  describe('GET Request', () => {
    console.log('===execute 1===');
    sample.args = <TestCaseArgumentsType>{ url: `another sampleUrl` };
    beforeAll(async () => {
      console.log('===execute 3===');
      sample.args = <TestCaseArgumentsType>{ url: `sampleUrl` };
    }, 20000);
    sample.sampleTestFunction();
  });
});

单元测试结果:

 PASS  src/stackoverflow/58480169/sampleTestSuite.test.ts (7.092s)
  User Route
    GET Request
      ✓ to check the before each execution effects in the test case (3ms)

  console.log src/stackoverflow/58480169/sampleTestSuite.test.ts:8
    ===execute 1===

  console.log src/stackoverflow/58480169/sampleTest.ts:11
    ===execute 2===

  console.log src/stackoverflow/58480169/sampleTest.ts:12
    another sampleUrl

  console.log src/stackoverflow/58480169/sampleTestSuite.test.ts:11
    ===execute 3===

  console.log src/stackoverflow/58480169/sampleTest.ts:15
    ===execute 4===

  console.log src/stackoverflow/58480169/sampleTest.ts:16
    sampleUrl

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        8.89s

如你所见,我放了一些console.log来表示代码的执行顺序。执行顺序为1,2,3,4。对不起我的英语。

【讨论】:

  • 是否可以在不初始化两次的情况下获取值(在describe内部和在beforeAll内部)。
猜你喜欢
  • 2022-10-20
  • 2016-03-11
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
相关资源
最近更新 更多