我进行了此测试,当测试失败并打印名为errorMessage
的变量时,它以我认为似乎无法进行的方式打印。这是我的代码:
function getNamesFromTypescriptFiles(): string[] {
const items = fs.readdirSync('./tsc/names');
const filenames = _.filter(items, (fn) => {
return fn.indexOf('.spec.') === -1;
});
const names = _.map(filenames, (fn) =>
fn.replace('.ts', '')
);
return names;
}
it('has an array element for each typescript file', () => {
const names = getNamesFromTypescriptFiles();
expect(names.length).toBeGreaterThan(0);
for (const name of names) {
expect(
(nameArray as ReadonlyArray<string>).includes(
name
)
).toBe(
true,
'The nameArray did not include ' + name
);
const path = './story/names/' + name + '-content.twee';
const exists = fs.existsSync(path);
expect(exists).toBe(true, `Could not find ${path}`);
const content = fs.readFileSync(path, { encoding: 'utf8' });
const passageLines = _.filter(content.split('\n'), (line) => {
return line.startsWith('::');
});
expect(passageLines.length).toBeGreaterThan(0);
for (const passageLine of passageLines) {
if (!/.*\[.*\].*/.test(passageLine)) {
const errorMessage =
name +
' has a passage named "' +
passageLine +
'" that has no tags. You must at least put an exclude tag.';
console.log(errorMessage);
expect(true).toBe(false, errorMessage);
}
}
}
});
打印时,看起来像这样:
Message:
" that has no tags. You must at least put an exclude tag.'. Passage name
Stack:
" that has no tags. You must at least put an exclude tag.'.med ":: Passage name
at <Jasmine>
我无法弄清楚这种错误消息是怎么可能的(它在console.log中说的是相同的内容):好像是将字符串切成两半然后重新排列一样。这怎么可能?似乎我有某种竞争状况,但据我所知,这不是并行运行的。
我正在使用打字稿,并在Jasmine中运行测试。就其价值而言,此测试始终会适当地失败,这只是错误的消息。