Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test cases for node 20 #351

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node-version: [18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion src/exports/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Have {

status(code, message = '') {
this.expect.statusCode = code;
this._expect.customMessage = message;
this.expect.customMessage = message;
this._validate();
}

Expand Down
17 changes: 12 additions & 5 deletions src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class Expect {
if (this.updateSnapshot) {
log.warn(`Update snapshot is enabled for '${snapshot_name}'`);
file.saveSnapshot(snapshot_name, response.json);
}
}
if (value) {
const current_rules = jmv.getMatchingRules(value, '$.body');
let errors = jmv.validate(actual, jmv.getRawValue(value), current_rules, '$.body');
Expand All @@ -355,7 +355,7 @@ class Expect {
Object.assign(all_rules, current_rules);
}
}

const expected = file.getSnapshotFile(snapshot_name, response.json);
if (Object.keys(all_rules).length > 0) {
const errors = jmv.validate(actual, expected, all_rules, '$.body', true);
Expand Down Expand Up @@ -420,9 +420,16 @@ class Expect {
for (let i = 0; i < this.errors.length; i++) {
const expected = this.errors[i];
if (typeof expected === 'string') {
const actual = response.toString();
if (!actual.includes(expected)) {
this.fail(`Error - "${actual}" doesn't include - ${expected}`);
if (response.errors && Array.isArray(response.errors) && response.errors.length > 0) {
const actual = response.errors[0].toString();
if (!actual.includes(expected)) {
this.fail(`Error - "${actual}" doesn't include - ${expected}`);
}
} else {
const actual = response.toString();
if (!actual.includes(expected)) {
this.fail(`Error - "${actual}" doesn't include - ${expected}`);
}
}
}
if (typeof expected === 'object') {
Expand Down
4 changes: 2 additions & 2 deletions test/component/bdd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('BDD', () => {
});

it('should return a header', () => {
expect(response).to.have.header('connection', 'close');
expect(response).to.have.headerContains('connection', 'cl');
expect(response).to.have.header('connection', /\w+/);
expect(response).to.have.headerContains('connection', 'l');
});

it('should return cookies', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/component/expects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ describe('Expects', () => {
} catch (error) {
err = error;
}
expect(err.message).equals(`Header value 'value' did not match for header 'connection': 'close'`);
expect(err.message).includes(`Header value 'value' did not match for header 'connection':`);
});

it('header value not found - RegEx', async () => {
Expand All @@ -187,7 +187,7 @@ describe('Expects', () => {
} catch (error) {
err = error;
}
expect(err.message).equals(`Header regex (/value/) did not match for header 'connection': 'close'`);
expect(err.message).includes(`Header regex (/value/) did not match for header 'connection':`);
});

it('header contains key not found', async () => {
Expand All @@ -213,7 +213,7 @@ describe('Expects', () => {
} catch (error) {
err = error;
}
expect(err.message).equals(`Header value 'value' did not match for header 'connection': 'close'`);
expect(err.message).includes(`Header value 'value' did not match for header 'connection':`);
});

it('header contains value not found - RegEx', async () => {
Expand All @@ -226,7 +226,7 @@ describe('Expects', () => {
} catch (error) {
err = error;
}
expect(err.message).equals(`Header regex (/value/) did not match for header 'connection': 'close'`);
expect(err.message).includes(`Header regex (/value/) did not match for header 'connection':`);
});

it('failed body', async () => {
Expand Down
4 changes: 1 addition & 3 deletions test/component/interactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ describe('Mock', () => {
.put('http://localhost:9393/api/projects/1')
.withBody("Hello")
.expectStatus(404)
.expectBody('Interaction Not Found')
.expectHeader('connection', 'close')
.expectHeaderContains('connection', 'close');
.expectBody('Interaction Not Found');
});

it('PATCH - invalid interaction', async () => {
Expand Down
5 changes: 2 additions & 3 deletions test/component/remote.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('Remote- post single mock interaction', () => {
await pactum.spec()
.get(`http://localhost:9393/api/pactum/interactions?ids=${id}`)
.expectStatus(200)
.expectJson([
.expectJsonLike([
{
"callCount": 1,
"exercised": true,
Expand All @@ -134,8 +134,7 @@ describe('Remote- post single mock interaction', () => {
"path": "/api/projects/1",
"query": {},
"headers": {
"host": "localhost:9393",
"connection": "close"
"host": "localhost:9393"
},
"body": ""
},
Expand Down
10 changes: 5 additions & 5 deletions test/component/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ describe('Response', () => {
});

it('with default expected response header', async () => {
response.setDefaultExpectHeaders('connection', 'close');
response.setDefaultExpectHeaders('content-type', 'application/json');
await pactum.spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get');
});

it('with default expected response header - failure', async () => {
response.setDefaultExpectHeaders('connection', 'open');
response.setDefaultExpectHeaders('content-type', 'application/xml');
let err;
try {
await pactum.spec()
Expand All @@ -76,15 +76,15 @@ describe('Response', () => {
} catch (error) {
err = error;
}
expect(err.message).equals(`Header value 'open' did not match for header 'connection': 'close'`);
expect(err.message).equals(`Header value 'application/xml' did not match for header 'content-type': 'application/json'`);
});

it('with default expected response header - override value', async () => {
response.setDefaultExpectHeaders('connection', 'open');
response.setDefaultExpectHeaders('content-type', 'application/xml');
await pactum.spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectHeader('connection', 'close');
.expectHeader('content-type', 'application/json');
});

it('with default expected response handler', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/component/returns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ describe('Returns', () => {
})
.get('http://localhost:9393/api/users')
.expectStatus(200)
.returns('res.headers.connection');
expect(response).equals('close');
.returns('res.headers');
expect(response['content-type']).equals('application/json');
});

it('return request headers', async () => {
Expand Down
Loading