How should I unit test a rule?
See original GitHub issueQuestion about GraphQL Shield
I’m unit testing my rules and I managed to do it like this but it doesn’t feels right, is very verbose and I have to use “any” on typescript so types don’t fail.
it("is valid when a valid token is provided", async () => {
const context = {
req: jest.fn()
}
const isValid = await isAuthenticated.resolve(
null, // parent
null, // args
context, // Context
null, // Info
{} as any, // Options
);
expect(isValid).toEqual(true);
});
Like this resolve does nothing 😕
What approach you will use?
- [x ] I have checked other questions and found none that matches mine.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:8
Top Results From Across the Web
The 5 unit testing guidelines - Medium
Unit tests have one assert per test. · Avoid if-statements in a unit test. · Unit tests only “new()” the unit under test....
Read more >Unit testing rules | Pega Academy
Unit test rules to ensure application configurations behave as expected. After completing this module, you should be able to: Identify the role of...
Read more >All about unit testing: 11 best practices and overview
7. One use case per unit test. Each test should focus on a single use case, and verify the output is as expected...
Read more >SSW.Rules | Rules to Better Unit Tests
When you encounter a bug in your application you should never let the same bug happen again. The best way to do this...
Read more >Unit Testing for Rules - Prometheus.io
You can use promtool to test your rules. # For a single test file. ./promtool test rules test.yml # If you have multiple...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve been fairly interested in graphql-shield and writing specs that is specific to my codebase. So I’ll share on how I’ve been designing my unit tests in order to get confidence in my app without necessarily having to unit test graphql-shield. This library is solid and rather than writing tests in your own library validating the library works as its advertised, all tests related to graphql-shield logic should lie inside this repo. As for in your codebase, you should write specs more geared to the rules you write so that should allow you to feel confident in reusing your rules. I would manually just validate that the caching works completely fine (unless someone has an easy to implement spec 😃 )
So I will be speaking around the topic of resolver level rules, and not field specific rules. Right now we are currently still developing so I haven’t had to face that challenge yet.
Because i’m not doing any field specific rules, I’ve created helper functions that assert whether the permission passed or did not. (Note: I’m using mocha/chai for my unit tests) The premise of these specs is, if there’s anything in the “data” key, then assume it worked and expect no errors. For validating failing tests, assert the exact error. Just to note, the
validateSuccessfulRequest
expects your the resolvers inputs/payloads to be required in the schema.resolver(input: ResolverInput!): ResolverPayload!
Then I have reusable spec helpers that are tied to specific rules. The two spec helper functions are tied to specific rules which are named
isAdmin
andisAuthenticated
respectively.And then I individually test resolvers independently from one another and I write a describe block related to specifically around permissions
So far this approach has been splendid. Right now, in the codebase I’m in, we have all the fields
whiteliste
in order to deny any permissions outright and we have to go field by field in order to think about the proper permissions and enable them. So we’ve gotten to the point where we think about what rules we need to implement, drop in the helper functions to test specific rules, fail them, and then get them passing once the rule is applied.We’ve even gone as far as made a
ruleSet
helper functions since some specs are specific to the type of argument passed in. These help us group specific mutations/queries based off of the input and reuse all of the same specs for different resolvers.fruitIdArgPermissionsSpec
vegetableIdArgPermissionSpec
and each of them import the smaller helper specs, so we can just drop in a function and get a lot of free tests.Let me know if you guys need me to clean something up in this. Hope someone finds us around this topic. I’m considering even creating an article around the subject but I’d like to get more insight on the topic to see if anyone else has another approach. I’m still tweaking and thinking of new ways.
This seems to fail when the rule has { cache: ‘strict’ } option. I’m still looking for a good solution for this. Any ideas?