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.
But for some reason the function is called without passing any arguments.
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();
2
Answers
Not exactly sure why, but changing my export and import fixes it. So:
module.exports = { post };
const { post } = require('./requests.cjs');
I would prefer not to use brackets with
requests
only having a single function, but I do plan to add other request types, so I guess it's ok.module.exports = post()
exports the result ofpost
, not the function itself. In case this is not the intention, it should be