Skip to content
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

test(playwright): enhance manager procedure mocking #1254

Merged
merged 6 commits into from
Dec 26, 2023

Conversation

angeloashmore
Copy link
Member

Context

This PR enhance's @slicemachine/manager mocking in Playwright tests.

This PR can be used as is or as a base for a refined implementation.

The Solution

  • Add a procedures fixture with a procedures.mock() method.
  • Maintain a list of procedure handlers to allow precise mock orchestration.
  • Allow tests to register procedure mocks one at a time.

Impact / Dependencies

Checklist before requesting a review

  • I hereby declare my code ready for review.
  • If it is a critical feature, I have added tests.
  • The CI is successful.
  • If there could backward compatibility issues, it has been discussed and planned.

🐕

Copy link

vercel bot commented Dec 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
slice-machine ✅ Ready (Inspect) Visit Preview Dec 26, 2023 8:08pm

Copy link
Collaborator

@xrutayisire xrutayisire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you to have started this, so for me I tried this way and first the typescript problem is blocking, and I don't see a beautiful way to fix it :/
So maybe we should convert it to a function with two sub function inside?

I explain:

// Path: playwright/utils/mockManagerProcedures.ts

async function handleRoute() {}

function mockManagerProcedures() {
  const procedures: Map<string, MockManagerProcedure[]> = new Map();

  init: (page: Page) => Promise<void>;
  mock: ({ path, handler?, config? }) => void;
}

// Path: playwright/fixtures/index.ts

export type DefaultFixtures = {
  mockProcedure: ({ path, handler?, config? }) => void
};

export const defaultTest = () => {
  return baseTest.extend<DefaultFixtures>({
    mockProcedure: async ({ page }, use) => {
      const { init, mock } = mockManagerProcedures();
      await init(page);
      
      await use(mock);
    }
  });

WDYT?

* Mocks
*/
procedures: async ({ page }, use) => {
await use(await MockManagerProcedures.init(page));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here this is broken in typescript with any :/

I guess it's because of the confusion with class for typescript.

Maybe we should create a classic TS function to handle this? It could remove the problem with the constructor. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be because I renamed mockManagerProcedures.ts to MockManagerProcedures.ts and eslint/tsserver isn't picking it up. I have full typing on my end.

Git properly shows the rename that was done via git mv. If it's still giving you trouble, maybe we can just rename the file something else temporarily.

A classic function is possible, but seems a bit messy considering we already have a class that encapsulates the mocking logic. The alternative was:

const mockManagerProcedures = new MockManagerProcedures()
await mockManagerProcedures.setup(page)

The setup/init methods are necessary because constructors cannot return Promises, thus they cannot use async/await.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing to consider re: using a plain function:

We may want to check the list of mocked procedures. Returning the full MockProceduresClass allows us to read the instance's procedures property. Providing a single mockProcedures fixture wouldn't allow that, unless we returned an object.

playwright/utils/MockManagerProcedures.ts Show resolved Hide resolved
Copy link
Collaborator

@xrutayisire xrutayisire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the typescript problem 🚀
Super work! Thanks!

@angeloashmore angeloashmore merged commit 99f5aa6 into main Dec 26, 2023
16 of 27 checks passed
@angeloashmore angeloashmore deleted the aa/mock-procedures branch December 26, 2023 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants