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

Support forced evaluations like let! #68

Open
mjewell opened this issue Nov 13, 2023 · 0 comments
Open

Support forced evaluations like let! #68

mjewell opened this issue Nov 13, 2023 · 0 comments

Comments

@mjewell
Copy link

mjewell commented Nov 13, 2023

This library is analogous to let in rspec, but there isn't an equivalent for let!. We are using it for cypress tests and we almost always want to eagerly evaluate our given variables (because they correspond to API calls we want to resolve before the test starts), so it would be really nice if there was a shorthand for this eager evaluation. Currently our tests looks something like

given('a', () => createSomething());
given('b', () => createSomethingElse());

beforeEach(() => {
  given.a;
  given.b;
});

it('works', () => { /* */ });

But if this library supported that behavior we could remove the beforeEach. There are a few APIs I have thought might work but I'm open to other suggestions:

// lots of different naming options here - now / eager / bang / other ideas?
given.now('a', () => createSomething());

// this one is a bit more complex to implement but I think it can be made to work with template literal types
// https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
given('!a', () => => createSomething());
// or to make it look less like a boolean operator
given('a!', () => => createSomething());

I think this is relatively easy to implement by adding a __lazy__ property to the given object (pretty much identical to the __props__ property), and pushing in true or false based on how this function is called. Then you can add a beforeEach that checks the top of the list to decide if it should access given[key]. I'm happy to submit a PR if we can agree on an API.

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

No branches or pull requests

1 participant