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

Add key packages in Script #1356

Merged
merged 3 commits into from
Feb 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
develop:
unreleased:
new features:
- GH-1356 Add new key `packages` to Script
chores:
- >-
GH-1357 Fixed a bug where invalid JSDoc prevented generating docs and
Expand Down
12 changes: 10 additions & 2 deletions examples/collection-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,22 @@
"id": "my-global-script-1",
"script": {
"type": "text/javascript",
"exec": "console.log(\"hello\");"
"exec": [
"const package1 = pm.require(\"package1\");",
"console.log(\"hello\", package1);"
],
"packages": { "package1": { "id": "script-package-1" } }
}
},
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": "console.log(\"hello\");"
"exec": [
"const package1 = pm.require(\"package1\");",
"console.log(\"hello\", package1);"
],
"packages": { "package1": { "id":"script-package-1" } }
}
}
],
Expand Down
15 changes: 15 additions & 0 deletions lib/collection/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var _ = require('../util').lodash,

SCRIPT_NEWLINE_PATTERN = /\r?\n/g;

/**
* A map of package names to the IDs of the packages
*
* @typedef {Object.<string, { id: string }>} Packages
*/

_.inherit((

/**
Expand Down Expand Up @@ -49,6 +55,7 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
* @param {String} [options.type] Script type
* @param {String} [options.src] Script source url
* @param {String[]|String} [options.exec] Script to execute
* @param {Packages} [options.packages] Packages required by the script
*/
update: function (options) {
// no splitting is being done here, as string scripts are split right before assignment below anyway
Expand All @@ -62,6 +69,14 @@ _.assign(Script.prototype, /** @lends Script.prototype */ {
* @type {string}
*/
this.type = options.type || 'text/javascript';

/**
* The packages required by the script
*
* @type {Packages}
*/
this.packages = options.packages;

_.has(options, 'src') && (

/**
Expand Down
12 changes: 8 additions & 4 deletions test/unit/event-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('EventList', function () {
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: [{ id: 'script-package-1', name: 'package1' }]
}
}];

Expand All @@ -35,7 +36,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: { package1: { id: 'script-package-1' } }
}
}]),
eventListJSON;
Expand All @@ -46,7 +48,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: { package1: { id: 'script-package-1' } }
}
}]);

Expand All @@ -63,7 +66,8 @@ describe('EventList', function () {
script: {
id: 'test-script-1',
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: { package1: { id: 'script-package-1' } }
}
});

Expand Down
31 changes: 28 additions & 3 deletions test/unit/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('Event', function () {
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
exec: 'console.log("hello");',
packages: {}
}
};

Expand Down Expand Up @@ -52,6 +53,7 @@ describe('Event', function () {
expect(postmanEvent).to.have.property('script').that.is.an('object');
expect(postmanEvent.script).to.have.property('type', 'text/javascript');
expect(postmanEvent.script).to.have.property('exec').that.is.an('array');
expect(postmanEvent.script).to.have.property('packages').that.is.an('object');
});
});

Expand Down Expand Up @@ -120,7 +122,8 @@ describe('Event', function () {
expect(eventJSON).to.have.property('script').that.has.property('id');
expect(eventJSON.script).to.deep.include({
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: {}
});
});

Expand All @@ -132,7 +135,8 @@ describe('Event', function () {
expect(beforeJSON).to.have.property('script').that.has.property('id');
expect(beforeJSON.script).to.deep.include({
type: 'text/javascript',
exec: ['console.log("hello");']
exec: ['console.log("hello");'],
packages: {}
});

event.update({ script: { id: 'my-new-script' } });
Expand All @@ -142,5 +146,26 @@ describe('Event', function () {
expect(afterJSON.script).to.have.property('id', 'my-new-script');
expect(afterJSON.script.exec).to.be.undefined;
});

it('should not add packages key if not present', function () {
var rawEvent = {
listen: 'test',
id: 'my-global-script-1',
script: {
type: 'text/javascript',
exec: 'console.log("hello");'
}
},
event = new Event(rawEvent),
beforeJSON = event.toJSON(),
afterJSON;

expect(beforeJSON.script).to.not.have.property('packages');

event.update({ script: { exec: 'console.log("updated");' } });
afterJSON = event.toJSON();

expect(afterJSON.script).to.not.have.property('packages');
});
});
});
4 changes: 2 additions & 2 deletions test/unit/script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Script', function () {
var source = script.toSource();

expect(source).to.be.a('string');
expect(source).to.equal(rawScript.exec);
expect(source).to.equal(rawScript.exec.join('\n'));
});
});
});
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('Script', function () {

expect(jsonified).to.deep.include({
type: rawScript.type,
exec: rawScript.exec.split('\n')
exec: rawScript.exec
});
expect(jsonified.src).to.eql(rawScript.src);
});
Expand Down
13 changes: 13 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,13 @@ declare module "postman-collection" {
static createFromNode(response: any, cookies: any): any;
}

/**
* A map of package names to the IDs of the packages
*/
export type Packages = {
[key: string]: { id: string; };
};

/**
* Postman scripts that are executed upon events on a collection / request such as test and pre request.
* @param options - -
Expand All @@ -2162,13 +2169,19 @@ declare module "postman-collection" {
* @param [options.type] - Script type
* @param [options.src] - Script source url
* @param [options.exec] - Script to execute
* @param [options.packages] - Packages required by the script
*/
update(options?: {
type?: string;
src?: string;
exec?: string[] | string;
packages?: Packages;
}): void;
type: string;
/**
* The packages required by the script
*/
packages: Packages;
src: Url;
exec: string[];
/**
Expand Down