function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian YarsawichBrian Yarsawich 

How to run tests against community profiles in unlocked pacakge?

I'm trying to build an unlocked pacakge that includes a community and am recieveing a bunch of errors in my tests.  The errors are all based on being unable to find a custom profile for community users.

This is the function to create a test user to run tests against in the community.
static User initUser(String userName) {

		User thisUser =
				[SELECT Id, UserRoleId
				FROM User
				WHERE Id = :UserInfo.getUserId()];
		
		Account a = new Account(
				Name = 'Test Account For Cart',
				OwnerId = thisUser.Id
		);

		insert a;

		Contact con = new Contact(
				FirstName = 'Test',
				LastName = 'Name',
				AccountId = a.Id
		);
		User u = new User();
		insert con;
		system.runAs(thisuser) {
			Profile p =
					[SELECT Id
					FROM Profile
					WHERE Name ='Customer Community Login User (TOC)'];

			u = new User(
					Alias = 'testUser',
					LastName = 'Testing',
					UserName = userName,
					ProfileId = p.Id,
					Email = username,
					EmailEncodingKey = 'UTF-8',
					LanguageLocaleKey = 'en_US',
					LocaleSidKey = 'en_US',
					TimeZoneSidKey = 'America/New_York',
					ContactId = con.Id
			);

			insert u;
		}

		return u;
	}
The problem is if i use a custom profile the tests fail on package creation, if i use the standard community profile then I get errors on those users not being able to do anything in salesforce.

I have read that profiles are not allowed in custom unlocked pacakges and I have tried cloning the standard community profile in the tests, but you can't run DML statements on a profile so i can't insert a new profile.

Is there any way to actually test code that uses a community user with a community profile?