Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to I get a valid session from a jest script? #2396

Closed
shreyas-jadhav opened this issue Jul 18, 2021 · 0 comments
Closed

How to I get a valid session from a jest script? #2396

shreyas-jadhav opened this issue Jul 18, 2021 · 0 comments
Labels
question Ask how to do something or how something works

Comments

@shreyas-jadhav
Copy link

Question 💬

I was trying to test my APIs with Jest, wondering if it's possible to sign in and get a valid session from a jest script.
I hoping if it can be possible without sign in method. because if i use @jest-environment jsdom I can run the sign in method but I don't get to pass the absolute URL.
for example the auth url is https://localhost:3000/auth
but as I'm using node fetch. (because the normal fetch is not available) the URL becomes relative in the signIn method. that is it becomes /auth and i get an error. let me know if there can be a better way to get a valid session from a server environment..

How to reproduce ☕️

/**
 * @jest-environment jsdom
 */

import { Match } from 'models/match.interface';
import { getSession, signIn } from 'next-auth/client';
global.fetch = require('node-fetch');

test('Able to Post Scores', async () => {
	// get a valid session
	const session = await getSignedIn();  // getting error as relative URL not allowed

	const testData: Match = {
		isActive: true,
		matchName: `testMatch12345789asdfh`,
		matchDate: '17/06/2021',
	};
	const result = await fetch(`http://localhost:3000/api/match`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify(testData),
	});

	expect(result.status).toBe(200);

	// checking if data available in database
	const entry = await fetch(`http://localhost:3000/api/match?matchName=${testData.matchName}`, { method: 'GET' });

	expect(entry.ok).toBe(true);
	expect(entry.status).toBe(200);

	console.log(JSON.stringify(entry.json()));
});

const getSignedIn = async () => {
	
	const result = await signIn('credentials', {
		redirect: false,
		email: '[email protected]',
		password: 'testpassword',
	});

	return await getSession();
};
export {};

Contributing 🙌🏽

No, I am afraid I cannot help regarding this

@shreyas-jadhav shreyas-jadhav added the question Ask how to do something or how something works label Jul 18, 2021
@nextauthjs nextauthjs locked and limited conversation to collaborators Jul 18, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

2 participants