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

Should setup group-hook inject stuff into test context #14

Closed
brkn opened this issue May 14, 2022 · 1 comment
Closed

Should setup group-hook inject stuff into test context #14

brkn opened this issue May 14, 2022 · 1 comment
Assignees
Labels
Type: Question Needs clarification

Comments

@brkn
Copy link

brkn commented May 14, 2022

Description

I've tried injecting stuff into the test context and it worked. But this is not documented, is this a bad pattern?

test.group("foo endpoint", (group) => {
  group.each.setup(async (test) => {
    const drive = Drive.fake("gcs");

    // @ts-ignore
    test.context.drive = drive;

    return () => Drive.restoreAll();
  });

  test("mocked disk test", async ({ test: { context } }) => {
    // @ts-ignore
    console.log(context.drive); // 👈prints actually
  });
});

Alternative is getting the instance in the test scope but restoring everything at the teardown, I guess. But it feels weird to copy paste drive mocking setup in each test.

test.group("foo endpoint", (group) => {
  group.each.teardown(async (test) => {
    Drive.restoreAll();
  });

  test("mocked disk test", async ({ test: { context } }) => {
    const drive = Drive.fake("gcs");
	
	// send request here

	// assertions
	assert.equal(await drive.get('package.json'), JSON.stringify({}))

	// let the teardown hook do the cleanup
  });
});

Package version

v2.0.7

@brkn brkn changed the title Should setup inject stuff into test context Should setup group-hook inject stuff into test context May 14, 2022
@thetutlage
Copy link
Contributor

Since the APIs allows you to inject custom properties in the context, you can do that. Yes, it is not documented, because not everything can be captured in documentation and a lot of stuff is about experimenting and building custom flows around the public APIs.

I guess. But it feels weird to copy paste drive mocking setup in each test.

It depends on how you like to code. I am personally okay with copy/pasting code within multiple tests and do not try to remove every duplicate line of code. Dan Abramov (maintainer of React) wrote a great article on the same https://overreacted.io/the-wet-codebase/.

I will close the issue as there is nothing actionable here 👍

@thetutlage thetutlage self-assigned this May 17, 2022
@thetutlage thetutlage added the Type: Question Needs clarification label May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Needs clarification
Projects
None yet
Development

No branches or pull requests

2 participants