Mocking and testing utilities for developing APIs with Swagger/OpenAPI
The following example starts a mock server that hosts each of the spec files in the specified folder. The server will restart when any of the spec files are updated. The regex determines which files should be run. But default, any file ending with .oas2.json
will be run.
const { watch } = require('rigorous');
watch('/path/to/my/spec/folder', {
port: 8080,
regex: /^spec\.json$/,
});
The following example will generate Mocha tests for negative cases based on the provided specs. The tests can then be run as you would run any suite of Mocha tests.
const fs = require('fs');
const {
createTestCases,
createBadParameters,
createMethodNotAllowed,
createMissingParameters,
createSmokeTests,
generate,
} = require('rigorous');
const spec = fs.readFileSync('/path/to/spec.oas2.json');
createTestCases(spec, [
createBadParameters,
createMethodNotAllowed,
createMissingParameters,
createSmokeTests,
]).then(testCases =>
fs.writeFileSync('/path/to/spec.tests.js', generate(testCases)),
);
- Build the code:
npm run build
- Run it!
npm start
- Add tests by creating files with the
.tests.ts
suffix - Run the tests:
npm t
- Test coverage can be viewed at
/coverage/lcov-report/index.html