diff --git a/examples/collection-v2.json b/examples/collection-v2.json index 0186aa964..13b655487 100644 --- a/examples/collection-v2.json +++ b/examples/collection-v2.json @@ -42,14 +42,16 @@ "id": "my-global-script-1", "script": { "type": "text/javascript", - "exec": "console.log(\"hello\");" + "exec": "console.log(\"hello\");", + "packages": { "package1": {"id":"script-package-1"}} } }, { "listen": "prerequest", "script": { "type": "text/javascript", - "exec": "console.log(\"hello\");" + "exec": "console.log(\"hello\");", + "packages": { "package1": {"id":"script-package-1"}} } } ], diff --git a/lib/collection/script.js b/lib/collection/script.js index 8ec0f6775..12a64cb02 100644 --- a/lib/collection/script.js +++ b/lib/collection/script.js @@ -49,6 +49,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 {Object} [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 @@ -62,6 +63,9 @@ _.assign(Script.prototype, /** @lends Script.prototype */ { * @type {string} */ this.type = options.type || 'text/javascript'; + + this.packages = options.packages || {}; + _.has(options, 'src') && ( /** diff --git a/test/unit/collection.test.js b/test/unit/collection.test.js index c413dad2a..ccecb3ff5 100644 --- a/test/unit/collection.test.js +++ b/test/unit/collection.test.js @@ -25,7 +25,8 @@ describe('Collection', function () { script: { id: 'my-script-1', type: 'text/javascript', - exec: ['console.log("This doesn\'t matter");'] + exec: ['console.log("This doesn\'t matter");'], + packages: {} } }] }, @@ -178,7 +179,8 @@ describe('Collection', function () { script: { id: 'test-script-1', type: 'text/javascript', - exec: ['console.log("Random");'] + exec: ['console.log("Random");'], + packages: {} } }); expect(collectionJSON.event[1]).to.have.property('listen', 'prerequest'); @@ -218,7 +220,8 @@ describe('Collection', function () { type: 'text/javascript', exec: [ 'console.log("bcoz I am batman!");' - ] + ], + packages: { superman: { id: 'script-apckage-1' } } } }], item: [{ @@ -280,7 +283,8 @@ describe('Collection', function () { type: 'text/javascript', exec: [ 'console.log("bcoz I am batman!");' - ] + ], + packages: { superman: { id: 'script-apckage-1' } } } }], item: [{ diff --git a/test/unit/event-list.test.js b/test/unit/event-list.test.js index abe404a63..7126475c2 100644 --- a/test/unit/event-list.test.js +++ b/test/unit/event-list.test.js @@ -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' }] } }]; @@ -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; @@ -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-apckage-1' } } } }]); @@ -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-apckage-1' } } } }); diff --git a/test/unit/event.test.js b/test/unit/event.test.js index b830ae1c3..7fba46b01 100644 --- a/test/unit/event.test.js +++ b/test/unit/event.test.js @@ -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: {} } }; @@ -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'); }); }); @@ -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: {} }); }); @@ -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' } }); diff --git a/test/unit/item-group.test.js b/test/unit/item-group.test.js index ff7d9966f..f8ef2ec1e 100644 --- a/test/unit/item-group.test.js +++ b/test/unit/item-group.test.js @@ -27,7 +27,8 @@ describe('ItemGroup', function () { script: { id: 'my-script-1', type: 'text/javascript', - exec: ['console.log("This doesn\'t matter");'] + exec: ['console.log("This doesn\'t matter");'], + packages: { package1: { id: 'script-apckage-1' } } } }], protocolProfileBehavior: { @@ -527,7 +528,8 @@ describe('ItemGroup', function () { script: { id: 'my-test-script', type: 'text/javascript', - exec: ['console.log("hello there!");'] + exec: ['console.log("hello there!");'], + packages: { package1: { id: 'script-apckage-1' } } } }], item: [{ diff --git a/test/unit/item.test.js b/test/unit/item.test.js index ac5108ff1..e1d9ea226 100644 --- a/test/unit/item.test.js +++ b/test/unit/item.test.js @@ -18,7 +18,8 @@ describe('Item', function () { script: { id: 'my-script-1', type: 'text/javascript', - exec: ['console.log("This doesn\'t matter");'] + exec: ['console.log("This doesn\'t matter");'], + packages: { package1: { id: 'script-apckage-1' } } } }], request: { diff --git a/test/unit/script.test.js b/test/unit/script.test.js index 85d05c8b4..157b94a61 100644 --- a/test/unit/script.test.js +++ b/test/unit/script.test.js @@ -89,7 +89,8 @@ describe('Script', function () { expect(scriptJSON).to.have.property('id'); expect(scriptJSON).to.deep.include({ type: 'text/javascript', - exec: ['console.log("This is a line of test script code");'] + exec: ['console.log("This is a line of test script code");'], + packages: {} }); }); @@ -201,7 +202,8 @@ describe('Script', function () { expect(jsonified).to.deep.include({ type: rawScript.type, - exec: rawScript.exec.split('\n') + exec: rawScript.exec.split('\n'), + packages: rawScript.packages }); expect(jsonified.src).to.eql(rawScript.src); }); diff --git a/types/index.d.ts b/types/index.d.ts index d4a3e7fb3..cc2373c26 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2158,15 +2158,18 @@ 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 to be preloaded */ update(options?: { type?: string; src?: string; exec?: string[] | string; + packages?: Object; }): void; type: string; src: Url; exec: string[]; + packages: Object; /** * Check whether an object is an instance of ItemGroup. * @param obj - -