Reputation: 662
I am trying to implement an app which takes testing config/cases as input (yaml/json) files and does bunch of assertions.
case:
- operation: fetch_user
args:
- name: id
value: 1
- checks:
- attribute: username
expected_value: user1
- attribute: email
expected_value: [email protected]
I want to write generic test blocks (it()) to perform assertion dynamically.
I've tried Does mocha/supertest create express server for each test suite?
I am fetching user information in index.js and have assertion in assertion.js file.
index.js
//read yaml file
const body = userService.get(userId);
body.then(userResponse => {
const suite = createTestSuite("Test", checks, userResponse);
suite.run();
});
assertion.js
function createTestSuite(name, checks, userResponse) {
let suite = new Suite("Moch test");
suite.addTest(new Test("Check Username", () => {
expect(userResponse.username).to.equal(checks.expected_username);
}));
return suite;
}
Upvotes: 1
Views: 516
Reputation: 1199
The --delay
option should help (docs).
Try something like (untested):
// test.js
// run with `mocha --delay test.js`
// getData will return a Promise which fulfills with an array of test data
getData().then(testData => {
describe('mocha tests', function() {
testData.forEach(data => {
it('should do something with data', function() {
// assertion here
});
});
});
run(); // this is a global which starts the test run
});
The --delay
option causes the test run to wait until you call run()
, which allows you to perform async operations (e.g., gather some data) before defining tests.
Mocha currently doesn't support asynchronous suites, which makes it awkward to dynamically generate tests from async data sources.
Upvotes: 2