gpt4 book ai didi

javascript - 让 beforeEach 仅运行其所在文件中的测试

转载 作者:行者123 更新时间:2023-12-01 01:31:49 26 4
gpt4 key购买 nike

我有两个 Mocha 测试文件,每个文件都有自己的 beforeEach 函数。每个文件中的 beforeEach 对所有测试用例运行。用代码更好地解释:

用户.test.js:

beforeEach((done) => {
console.log('user before each');
done();
});
describe('Running user tests', () => {

it('user test #1', () => {
console.log('in user test 1');
});

it('user test #2', () => {
console.log('in user test 2');
});

});

tod​​o.test.js

beforeEach((done) => {
console.log('todo before each');
done();
});

describe('Running todo tests', () => {

it('todo test #1', (done) => {
console.log('in todo test 1');
done();
});

it('todo test #2', (done) => {
console.log('in todo test 2');
done();
});

});

package.json

{
"name": "before-each",
"version": "1.0.0",
"description": "",
"main": "todo.test.js",
"scripts": {
"test": "mocha **/*.test.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"mocha": "^5.2.0"
}
}

运行时输出:npm test

  Running todo tests
todo before each
user before each
in todo test 1
✓ todo test #1
todo before each
user before each
in todo test 2
✓ todo test #2

Running user tests
todo before each
user before each
in user test 1
✓ user test #1
todo before each
user before each
in user test 2
✓ user test #2


4 passing (7ms)

有没有办法让 todo 文件中的 beforeEach() 仅针对 todo 文件中的测试运行,以及用户文件中的 beforeEach() 仅针对用户测试运行?我有 Java/JUnit 背景,这表现得非常不同。

谢谢!

最佳答案

beforeEach 移至 describe 内。这是放置属于特定 describebeforeEach 的正确位置:

describe('Running user tests', () => {
beforeEach((done) => {
console.log('user before each');
done();
});

it('user test #1', () => {
console.log('in user test 1');
});

it('user test #2', () => {
console.log('in user test 2');
});
});

关于javascript - 让 beforeEach 仅运行其所在文件中的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53211074/

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