Newman: "identifier has already been declared at test-script"

Hello,

I am trying out newman (version 6.2.1) on my local computer using an exported collection and environment. I am running the test on a single folder in my collection.

The test runs but I get the following error for the two requests Identifier 'responseBody' has already been declared at test-script.

My post-response test script is defined at the collection folder level. At the top of the script I have: const responseBody = pm.response.json(); and I do not redeclare that variable in the script. I only use that variable to run tests such as pm.expect(responseBody)....

I can run these same tests using the Postman CLI and I do not get this error.

Any thoughts on what is going on?

This is a weird behaviour. Is the variable “responseBody” declared in any other request or folder? For example, in the scripts at the collection or folder level?

Hi @gbadebo-bello - I recognize you from youtube!

I am not sure if the behavior is weird - this is my first time experimenting with newman.

I have two folders in my collection. Each folder has its own post-response script. Both scripts declare the variable responseBody. However, I am only running the tests in one folder using this command: npx newman run my_collection.json --environment my_environment.json --folder "Invalid Calls"

I get the error shown in the snip below. I did try renaming the responseBody variable in the post-request script in the folder I am not testing but I get the same error.

Hi @cpaye, Will it be possible for you to share a screenshot of the test script where this error happens and that of the folder where it says there is a duplicate?

@gbadebo-bello Here’s the screenshot with the folder and the script.

I’m not really sure what is going on here, and I wasn’t able to replicate this on my end. When you run this collection in Postman, do you get any similar error?

@gbadebo-bello When I run the requests individually in Postman or when I run as a collection runner, I don’t get an error.

Hello @gbadebo-bello . I have a simple example collection where I can get the error. The collection has two folders. Each folder has the same post-request script. Each script declares a “responseBody” variable. I can get the error by running the command npx newman run Temp.postman_collection.json --folder folder_one or npx newman run Temp.postman_collection.json.

It looks like I can’t upload a json file, I will just paste the contents here.

{
	"info": {
		"_postman_id": "fake_id",
		"name": "Temp",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
		"_exporter_id": "36314241",
		"_collection_link": "https://fakeurl"
	},
	"item": [
		{
			"name": "folder_one",
			"item": [
				{
					"name": "Postman Echo One",
					"event": [
						{
							"listen": "prerequest",
							"script": {
								"exec": [
									"if(pm.collectionVariables.has('env')) {\r",
									"    console.log('collectionVariables includes environment variables')\r",
									"}\r",
									"\r",
									"if(pm.variables.has('env')) {\r",
									"    console.log('pm.variables includes environment variables')\r",
									"}\r",
									"console.log('Pre-request script ran at request level.')"
								],
								"type": "text/javascript",
								"packages": {}
							}
						},
						{
							"listen": "test",
							"script": {
								"exec": [
									"const responseBody = pm.response.json();\r",
									"\r",
									"console.log(responseBody.args);\r",
									"\r",
									"pm.test(\"Status code is 200\", function () {\r",
									"    pm.response.to.have.status(200);\r",
									"});"
								],
								"type": "text/javascript",
								"packages": {}
							}
						}
					],
					"request": {
						"method": "GET",
						"header": [],
						"url": {
							"raw": "https://postman-echo.com/get",
							"protocol": "https",
							"host": [
								"postman-echo",
								"com"
							],
							"path": [
								"get"
							]
						}
					},
					"response": []
				}
			]
		},
		{
			"name": "folder_two",
			"item": [
				{
					"name": "Postman Echo Two",
					"event": [
						{
							"listen": "prerequest",
							"script": {
								"exec": [
									""
								],
								"type": "text/javascript",
								"packages": {}
							}
						},
						{
							"listen": "test",
							"script": {
								"exec": [
									"const responseBody = pm.response.json();\r",
									"\r",
									"console.log(responseBody.args);\r",
									"\r",
									"pm.test(\"Status code is 200\", function () {\r",
									"    pm.response.to.have.status(200);\r",
									"});"
								],
								"type": "text/javascript",
								"packages": {}
							}
						}
					],
					"request": {
						"method": "GET",
						"header": [],
						"url": {
							"raw": "https://postman-echo.com/get",
							"protocol": "https",
							"host": [
								"postman-echo",
								"com"
							],
							"path": [
								"get"
							]
						}
					},
					"response": []
				}
			]
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"packages": {},
				"exec": [
					""
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"packages": {},
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "collectionVariable",
			"value": "1",
			"type": "string"
		},
		{
			"key": "tmp",
			"value": "tmp",
			"type": "string"
		},
		{
			"key": "env",
			"value": "collection_env",
			"type": "string"
		}
	]
}

Hey @cpaye :wave:

In previous versions of Postman certain keywords were used in a global scope to access certain items like responseBody or data.

Users would use them like this but these are now deprecated.

As that name has a declaration as a global keyword, it’s likely to be causing the error as you’re trying to reassign it.

Changing your variable name to resBody or something else will remove that error you’re seeing from Newman.

1 Like

@danny-dainton Good call! That was it. I changed the variable name to resBody and the test ran as expected.

Thank you to both you and @gbadebo-bello for looking into this.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.