Skip to content

Commit

Permalink
fix: MockServer_test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Mar 6, 2024
1 parent 2f1cfe6 commit 8115f4a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
export const __dirname = dirname(__filename);
3 changes: 2 additions & 1 deletion lib/helper/MockServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mock, settings } from 'pactum';
import pkg from 'pactum';
const { mock, settings } = pkg;

/**
* ## Configuration
Expand Down
7 changes: 4 additions & 3 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { default as axios } from 'axios';
import Helper from '@codeceptjs/helper';
import Secret from '../secret.js';
import { beautify } from '../utils.js';
import { output } from '../output.js';

/**
* ## Configuration
Expand Down Expand Up @@ -163,20 +164,20 @@ class REST extends Helper {
await this.config.onRequest(request);
}

this.options.prettyPrintJson ? this.debugSection('Request', beautify(JSON.stringify(_debugRequest))) : this.debugSection('Request', JSON.stringify(_debugRequest));
this.options.prettyPrintJson ? output.debugSection('Request', beautify(JSON.stringify(_debugRequest))) : output.debugSection('Request', JSON.stringify(_debugRequest));

let response;
try {
response = await this.axios(request);
} catch (err) {
if (!err.response) throw err;
this.debugSection('Response', `Response error. Status code: ${err.response.status}`);
output.debugSection('Response', `Response error. Status code: ${err.response.status}`);
response = err.response;
}
if (this.config.onResponse) {
await this.config.onResponse(response);
}
this.options.prettyPrintJson ? this.debugSection('Response', beautify(JSON.stringify(response.data))) : this.debugSection('Response', JSON.stringify(response.data));
this.options.prettyPrintJson ? output.debugSection('Response', beautify(JSON.stringify(response.data))) : output.debugSection('Response', JSON.stringify(response.data));
return response;
}

Expand Down
7 changes: 4 additions & 3 deletions test/helper/MockServer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import path from 'path';

import { expect } from 'chai';
import { like } from 'pactum-matchers';
import MockServer from '../../lib/helper/MockServer';
import REST from '../../lib/helper/REST';
import MockServer from '../../lib/helper/MockServer.js';
import REST from '../../lib/helper/REST.js';
import { __dirname } from '../../lib/dirname.js';

global.codeceptjs = require('../../lib');
global.codeceptjs = '../../lib';

let I;
let restClient;
Expand Down

0 comments on commit 8115f4a

Please sign in to comment.