Duplicate request initiated and not sure why, causes issues when trying to run all cucumber scenarios at once - cucumber, pactum #386
-
I am having trouble with with my I have a Background step that runs before each test to get the auth bearer token for use in next steps. If I use ✖ Given an authorised user # test/api/step_definitions/auth.ts:12
TypeError: Invalid URL
at new URL (node:internal/url:814:29)
at setBaseUrl (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/helpers/requestProcessor.js:47:16)
at Object.process (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/helpers/requestProcessor.js:16:5)
at Tosser.toss (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/models/Tosser.js:32:39)
at Spec.toss (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/models/Spec.js:552:19)
at Spec.then (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/models/Spec.js:556:10)
at processTicksAndRejections (node:internal/process/task_queues:95:5) If I use ✖ Given an authorised user # test/api/step_definitions/auth.ts:11
Error: Duplicate request initiated. Existing request - POST https://fusion.auth.--.--.--/oauth2/token
at validateRequestUrl (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/models/Spec.js:578:11)
at Spec.post (/../../../../../../../../node_modules/.pnpm/pactum@3.7.3/node_modules/pactum/src/models/Spec.js:106:5)
at World.<anonymous> (/../../../../../../../../../../test/api/step_definitions/auth.ts:26:6) hooks file import { After, Before } from "@cucumber/cucumber"
import { request, settings } from "pactum";
require('dotenv').config({ path: './.env' });
const pactum = require('pactum');
export let baseUrl;
export let spec = pactum.spec();
Before(() => {
console.log('--------Starting test--------')
baseUrl = process.env.API_URL_LOCAL;
request.setBaseUrl(baseUrl)
settings.setLogLevel('DEBUG');
spec = pactum.spec();
})
After(() => {
console.log('--------Ending test--------');
spec.end();
}) auth steps file (note that I have both uses in the auth file below just to show you the options I use, I am using one or the other, not both together,). import { Before, Given } from '@cucumber/cucumber';
require('dotenv').config({ path: './.env' });
const pactum = require('pactum');
// export let bearerToken;
export let spec = pactum.spec();
const devFusionAuth = process.env.FUSION_AUTH_URI;
Given(/^an authorised user$/, async () => {
await pactum.spec() //Stops - Error: Duplicate request initiated. But creates issue 'Invalid URL'
await spec //Shares auth code but creates duplicate request issue
.withHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'fusion.auth.--.--.--',
'Cookie': 'fusionauth.sso=Ah0CfFInltQLGjQyIjnpjtVtGPY9rFVQs6XOyUrpApOC',
})
.withForm({
'scope': 'offline_access',
'username': process.env.API_USERNAME,
'password': process.env.API_PASSWORD,
'grant_type': 'password',
'client_id': process.env.API_CLIENT_ID
})
.post(devFusionAuth + '/oauth2/token')
.expectStatus(200)
.toss();
// bearerToken =
await spec
.stores('bearerToken', 'access_token');
}); 2 questions, why are I receiving the duplicate request? I only see one post so is there something I'm missing here... And how to I get past this issue as can only run 1 test at a time which is obviously not ideal. I am still new to Pactum, and JS/TS. Note when I run one test at a time I do see 2 post requests to the auth endpoint but can't work out why. Please help 🥲 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Given your test case, you might not need to call spec multiple times. Just chain all the calls to make it simple. Given(/^an authorised user$/, async () => {
// Single, clean authentication request
await spec()
.withHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'fusion.auth.--.--.--',
'Cookie': 'fusionauth.sso=Ah0CfFInltQLGjQyIjnpjtVtGPY9rFVQs6XOyUrpApOC',
})
.withForm({
'scope': 'offline_access',
'username': process.env.API_USERNAME,
'password': process.env.API_PASSWORD,
'grant_type': 'password',
'client_id': process.env.API_CLIENT_ID
})
.post(process.env.FUSION_AUTH_URI + '/oauth2/token')
.expectStatus(200)
.stores('bearerToken', 'access_token');
}); |
Beta Was this translation helpful? Give feedback.
Given your test case, you might not need to call spec multiple times. Just chain all the calls to make it simple.