`deno test`: beforeEach() prevents use of permissions configuration
This may or may not be intended behaviour. However I think at least the behaviour/error message is unintuitive, because it is not necessarily obvious as a user that beforeEach is in the nested hierarchy.
When creating a test like this:
beforeEach(() => {
console.log('this acts as an outer nested scope called "global"')
console.log('but there is no way to assign permissions on this outer nested layer')
});
const permissions = {
run: true,
write: true,
};
describe("example",{ permissions }, () => {
it("should do some foo", async () => {
// Error: permissions option not available for nested tests
});
});
I wonder if we need to throw there ( https://github.com/denoland/std/blob/3b75ee7c1925388e01dd69540d51d9387e26dcb0/testing/_test_suite.ts#L347-L351 )
@KyleJune Do you remember why this usage of permission is prevented?
@KyleJune Do you remember why this usage of permission is prevented?
I believe that at the time, you could define permissions for a top-level test but not a test step. It looks like that is still the case.
https://docs.deno.com/api/deno/~/Deno.TestStepDefinition
Tests inside a describe block are basically registered as test steps. Describe blocks nested in other describe blocks are also internally registered as test steps. So if we didn't throw an error, it would give users the false impression that the permissions argument is being respected.
When you declare a hook like beforeEach outside of a describe block, it causes a test to get registered for the whole file, then each test after it in the file ends up being registered as a step nested inside it. This is needed because hooks need to run inside the context of a registered test.
To work around this, they could instead define their own top-level test that takes the permissions argument, then move the hooks inside the describe block. That way, the describe block taking the permissions argument is registered with Deno.test instead of t.step.
OP is right that there currently isn't a way to define most test options currently at the file/global level. All you can define currently is the hooks. If any hooks are defined outside the context of a describe block, a describe block is created for the file and all other tests in the file are considered as being nested in it.
Not sure it's worth providing a way to define global/file level test options though considering there is an essy way to work around the issue, by just manually creating the top level describe instead of depending on bdd to create one.