I’m using setupFiles
in Jest to call an authentication flow for a new test framework. As part of that effort I’ve started abstracting common request types to a separate file, then referencing them in my auth setup file.
The behavior I’m seeing is Jest seems to scan any function called or referenced in that file then run them independently. As I need to pass arguments to my request
function, this causes some issues.
Am I doing something wrong, or is this a bug in Jest?
jest.config.cjs
setupFiles: ['./tests/lib/auth.cjs'],
auth.cjs
const request = require('./requests.cjs');
const microsoftLoginUrl = 'https://login.microsoftonline.com/';
async function getAuthCookie() {
// Login
const loginResponse = await request.post(
`${microsoftLoginUrl}xxxxx/login`,
{
login: 'xxxx',
passwd: 'xxxxx',
canary: canary,
ctx: ctx,
hpgRequestId: sessionId,
flowToken: sft,
},
{
'Content-Type': 'application/x-www-form-urlencoded',
}
);
requests.cjs
async function post(url, body, headers) {
body.headers = headers;
const postConstants = { method: 'POST', body: new URLSearchParams({ body }) };
return await fetch(url, postConstants);
}
module.exports = post();