gpt4 book ai didi

javascript - 在流程 : test whether variables got set 中编写规范

转载 作者:行者123 更新时间:2023-11-30 14:26:20 25 4
gpt4 key购买 nike

我有以下规范,其中 somePromise() 返回 true 或抛出错误:

describe('context level 1', () => {
let a;
let error;

beforeEach(async () => {
a = undefined;
error = undefined;

try {
a = await somePromise();
} catch (e) {
error = e;
}
});

afterEach(() => {
a = null;
error = null;
});


describe('context level 2', () => {
before(() => {
doSomeSetup();
});

it('test case 1', () => {
a.should.balahalah();
});
});

describe('context level 2', () => {
before(() => {
doSomeSetup();
});

it('test case 2', () => {
error.message.should.equal('xxxdasfsda');
});
});
});

规范按预期工作,但流检查(我的项目使用 facebook 流)显示如下错误:

Cannot get error.message because property message is missing in null [1].

问题:这些specs应该怎么写成flow?

最佳答案

您可以显式处理所有可能的场景以避免流程错误。

describe('context level 1', () => {
describe('context level 2', () => {
before(() => {
doSomeSetup();
});

it('test case 1', () => {
try {
const a = await subject();
should(result).balahalah();
} catch (e) {
should.not.exist(e);
}
});
});

describe('context level 2', () => {
before(() => {
doSomeSetup();
});

it('test case 2', () => {
try {
const a = await subject();
should.not.exist(a);
} catch (e) {
e.message.should.equal('xxxdasfsda');
}
});
});
});

关于javascript - 在流程 : test whether variables got set 中编写规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com