Javascript JEST:函数内的测试用例未从构造函数获取值
我尝试编写封装在类中的测试用例,以便调用方法将执行测试用例。但由于依赖性因素,url值将在beforeAll/beforeach块内初始化。在这种情况下,我没有得到url值,这导致测试用例执行失败。我无法将url作为参数传递,因为url将仅在beforeAll块中初始化。是否有其他解决方案可用于解决此问题 样本测试Javascript JEST:函数内的测试用例未从构造函数获取值,javascript,node.js,typescript,jestjs,Javascript,Node.js,Typescript,Jestjs,我尝试编写封装在类中的测试用例,以便调用方法将执行测试用例。但由于依赖性因素,url值将在beforeAll/beforeach块内初始化。在这种情况下,我没有得到url值,这导致测试用例执行失败。我无法将url作为参数传递,因为url将仅在beforeAll块中初始化。是否有其他解决方案可用于解决此问题 样本测试 export interface TestCaseArgumentsType { baseUrl: string; url: string; } export class S
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();
});
});
原因是代码的执行顺序 关键点是在调用函数之前执行所有函数之前,而不是在调用sample.sampleTestFunction之前。因此,当调用sample.sampleTestFunction方法时,beforeAll函数中的语句sample.args={url:'sampleUrl'}将不会执行。sample的arguments属性未定义。这就是为什么会出现错误: TypeError:无法读取未定义的属性“url” 当jestjs测试运行程序准备调用它时,测试运行程序将首先调用beforeAll函数 sampleTest.ts: 导出接口TestCaseArgumentsType{ baseUrl:字符串; url:string; } 出口类样品{ 设置argsvalue:TestCaseArgumentsType{ this.arguments=值; } 私有参数!:TestCaseArgumentsType; 样本测试函数{ console.log'==执行2==='; console.logthis.arguments.url;//另一个sampleUrl 它“检查测试用例中每次执行前的效果”,=>{ console.log'==执行4==='; console.logthis.arguments.url;//sampleUrl }; } } sampleTestSuite.test.ts: 从“/sampleTest”导入{Sample,TestCaseArgumentsType}; 描述“用户路线”,=>{ 让样本=新样本; 描述“获取请求”,=>{ console.log'==执行1==='; sample.args={url:`other sampleUrl`}; beforeallalsync=>{ console.log'==执行3==='; sample.args={url:`sampleUrl`}; }, 20000; sample.sampleTestFunction; }; }; 单元测试结果: 通过src/stackoverflow/58480169/sampleTestSuite.test.ts 7.092s 用户路由 获取请求 ✓ 在测试用例3ms中检查每次执行前的效果 console.log src/stackoverflow/58480169/sampleTestSuite.test.ts:8 ==执行1=== console.log src/stackoverflow/58480169/sampleTest.ts:11 ==执行2=== console.log src/stackoverflow/58480169/sampleTest.ts:12 另一个样本URL console.log src/stackoverflow/58480169/sampleTestSuite.test.ts:11 ==执行3=== console.log src/stackoverflow/58480169/sampleTest.ts:15 ==执行4=== console.log src/stackoverflow/58480169/sampleTest.ts:16 样本URL 测试套件:1个通过,共1个 测试:1项通过,共1项 快照:共0个 时间:8.89秒
如您所见,我放置了一些console.log来指示代码的执行顺序。执行顺序为1,2,3,4。对不起,我的英语不好。我执行了你的代码,它工作正常。@slideshowp2你在测试用例中得到url了吗?我得到了一个错误。我得到了console.logthis.arguments.url的sampleUrl和测试密码您是如何运行此代码的。通过将jest?与test jest和jestis一起安装,可以在不初始化两次的情况下获得值inside Descripte和inside beforeAll。