Skip to content

Commit

Permalink
fix: error printing and withJson invalid file
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Mar 28, 2024
1 parent 4fc5a20 commit fa76ed7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/exports/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,18 @@ class Have {

_validate() {
try {
return this.expect.validate({}, this.response);
return this.expect.validate({}, this.response).catch((err) => { this._handle_validate_error(err) });
} catch (error) {
if (this.spec && this.spec.status !== 'FAILED') {
this.spec.status = 'FAILED';
utils.printReqAndRes(this.spec._request, this.response);
}
throw error;
return this._handle_validate_error(error);
}
}

_handle_validate_error(error) {
if (this.spec && this.spec.status !== 'FAILED') {
this.spec.status = 'FAILED';
utils.printReqAndRes(this.spec._request, this.response);
}
throw error;
}

}
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/file.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ function saveSnapshot(name, data) {
* @param {string} name
*/
function findFile(name, dir = config.data.dir) {
const result = _findFile(name, dir);
if (result) {
return result;
}
throw new Error(`File Not Found - '${name}'`);
}

function _findFile(name, dir = config.data.dir) {
if (fs.existsSync(name)) {
return fs.readFileSync(name);
}
Expand All @@ -47,7 +55,7 @@ function findFile(name, dir = config.data.dir) {
const dirPath = path.resolve(dir, file);
const stats = fs.statSync(dirPath);
if (stats.isDirectory()) {
const result = findFile(name, dirPath);
const result = _findFile(name, dirPath);
if (result) {
return result;
}
Expand Down
37 changes: 37 additions & 0 deletions test/component/expects.custom.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { spec } = require('../../src');
const assert = require('assert');
const expect = require('chai').expect;

describe('Expects - Custom', () => {

it('custom expect handler - fails', async () => {
let err;
try {
await spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expect((_) => { assert.fail('error'); })
.expectStatus(200);
} catch (error) {
err = error;
}
expect(err.message).equals(`error`);
});

it('custom expect handler - bdd - fails', async () => {
let err;
try {
const _spec = spec();
await _spec
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectStatus(200);
await _spec.response().to.have._((_) => {
assert.fail('error');
});
} catch (error) {
err = error;
}
expect(err.message).equals(`error`);
});
})
13 changes: 13 additions & 0 deletions test/component/withJson.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { spec, settings } = require('../../src/index');
const expect = require('chai').expect;

describe('withJson', () => {

Expand Down Expand Up @@ -47,5 +48,17 @@ describe('withJson', () => {
.withJson('sample-2.json')
.expectStatus(200);
});

it('with invalid file', async () => {
let err;
try {
await spec()
.post('http://localhost:9393/file')
.withJson('invalid-file.json')
} catch(error) {
err = error;
}
expect(err.message).equals(`File Not Found - 'invalid-file.json'`);
})

});

0 comments on commit fa76ed7

Please sign in to comment.