在 mac 上运行但不在 linux 上运行的函数中调用的 AVA 测试
AVA tests called within a function working on mac, but not on linux
我有一些 AVA 测试,我试图 运行 多次,但在不同的模拟上。例如,我想要 运行 相同的 5 个测试,但要确保这些测试适用于各种结构化的数据。我设计了一种将模拟导入测试文件并将它们与它们应该如何解析的方法进行比较,看起来类似于以下内容:
import test from 'ava';
import * as fs from 'fs';
let mockSampleEmail;
let mockDecomposedEmail;
function readJsonFile(fname) {
return JSON.parse(fs.readFileSync(fname).toString());
}
fs.readdir('pre', (err, files) => {
files.forEach(file => {
if (!file.match(/\.json$/)) {
return null;
}
mockSampleEmail = readJsonFile("pre/" + file);
mockDecomposedEmail = readJsonFile("post/" + file);
runEmailDecomposerTests(mockSampleEmail, mockDecomposedEmail, mockRiskyUpdates, file);
})
})
运行EmailDecomposerTests 函数如下所示:
const runEmailDecomposerTests = (mockSampleEmail, mockDecomposedEmail, mockRiskyUpdates, fname) => {
test(`(${fname}) Converts the email message received from the websocket into a simple email object`, t => {
//Assertions here
})
}
我的目录结构如下所示:
test/
--emails/
----emailDecomposer.spec.js
----pre/
------sampleEmail.json
----post/
------sampleEmail.json
这一切都在我的 mac 上本地运行,但是当我将它推送到我们的 jenkins 服务器以测试持续集成时,它失败并显示错误:
✖ No tests found in test/emails/emailDecomposer.spec.js
尽管我可以确认确实调用了 运行EmailDecomposerTests 函数。令人困惑的部分是它在我的 mac 上本地传递。 jenkins 服务器是一个 Linux 虚拟机,所以这就是为什么我倾向于 mac/linux 问题,但我不能确定。更进一步,大约五分之一的时间它通过我们的 CI 服务器,所以这可能是某种竞争条件?
来自 AVA 文档:
You must define all tests synchronously. They can't be defined inside setTimeout
, setImmediate
, etc.
fs.readdir
是异步的,所以我很惊讶它实际上可以在 macOS 上运行,但正如您所经历的那样,它会导致竞争问题。我建议切换到 fs.readdirSync
或在 test.before()
挂钩中执行异步操作,然后利用 t.context
.
我有一些 AVA 测试,我试图 运行 多次,但在不同的模拟上。例如,我想要 运行 相同的 5 个测试,但要确保这些测试适用于各种结构化的数据。我设计了一种将模拟导入测试文件并将它们与它们应该如何解析的方法进行比较,看起来类似于以下内容:
import test from 'ava';
import * as fs from 'fs';
let mockSampleEmail;
let mockDecomposedEmail;
function readJsonFile(fname) {
return JSON.parse(fs.readFileSync(fname).toString());
}
fs.readdir('pre', (err, files) => {
files.forEach(file => {
if (!file.match(/\.json$/)) {
return null;
}
mockSampleEmail = readJsonFile("pre/" + file);
mockDecomposedEmail = readJsonFile("post/" + file);
runEmailDecomposerTests(mockSampleEmail, mockDecomposedEmail, mockRiskyUpdates, file);
})
})
运行EmailDecomposerTests 函数如下所示:
const runEmailDecomposerTests = (mockSampleEmail, mockDecomposedEmail, mockRiskyUpdates, fname) => {
test(`(${fname}) Converts the email message received from the websocket into a simple email object`, t => {
//Assertions here
})
}
我的目录结构如下所示:
test/
--emails/
----emailDecomposer.spec.js
----pre/
------sampleEmail.json
----post/
------sampleEmail.json
这一切都在我的 mac 上本地运行,但是当我将它推送到我们的 jenkins 服务器以测试持续集成时,它失败并显示错误:
✖ No tests found in test/emails/emailDecomposer.spec.js
尽管我可以确认确实调用了 运行EmailDecomposerTests 函数。令人困惑的部分是它在我的 mac 上本地传递。 jenkins 服务器是一个 Linux 虚拟机,所以这就是为什么我倾向于 mac/linux 问题,但我不能确定。更进一步,大约五分之一的时间它通过我们的 CI 服务器,所以这可能是某种竞争条件?
来自 AVA 文档:
You must define all tests synchronously. They can't be defined inside
setTimeout
,setImmediate
, etc.
fs.readdir
是异步的,所以我很惊讶它实际上可以在 macOS 上运行,但正如您所经历的那样,它会导致竞争问题。我建议切换到 fs.readdirSync
或在 test.before()
挂钩中执行异步操作,然后利用 t.context
.