gpt4 book ai didi

javascript - 按顺序运行测试文件?

转载 作者:行者123 更新时间:2023-12-04 17:57:58 25 4
gpt4 key购买 nike

我有多个测试文件。每个文件都有一个 beforeafter 函数,执行服务器启动、关闭和数据库清理事件。

示例测试文件的结构:

describe('some test:', function() {

before('reset db; start server', start_server)
after('close server', close_server)

// some tests
describe('#clear_expired_signals:', clear_expired_signals)
describe('#delete_signal ', delete_signal)
}

当这些文件一起执行时,大多数时候我都会遇到 EADDRINUSE 异常,因为服务器在每次 before 调用期间在同一端口启动。

是否可以强制 mochajs 一次执行单个文件(文件顺序不是问题)?

编辑:

before function sample

  function start_server(done) {   
intialize_server.start().then(function(options) {

seneca = options.seneca;
seneca.client({
host: 'localhost',
port:options.port,
});
done();
}

}



function intialize_server(){
return new Promise(function (resolve, reject) {
seneca.ready(function () {

seneca.listen({
host: 'localhost',
port: custom_port,
});

resolve({
seneca: seneca,
port: custom_port
});
// console.log('test server listening')
});
});
}

after function sample

 function close_server(done) {
console.log('closing seneca instance');
seneca.close(done);
}

最佳答案

我解决了这个问题。我没有为每个测试文件运行 beforeafter,而是添加了 ROOT-LEVEL HOOKS,如文档中给出的那样

ROOT-LEVEL HOOKS

You may also pick any file and add “root”-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the “root suite”).

关于javascript - 按顺序运行测试文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38780821/

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