Testing: use global resource automatically

I removed a big section of code from our main repo and got it working as a plugin.

Now I’m going through trying to fix all the tests. The problem I’m running into is that I’ve removed all the <require from="..."></require> from the templates since all the components are being used globally. But I’m not sure how I can get Jest to respect that too…

The best I’ve been able to find is to do this on every single test (and it works, just not ideal):

component = StageComponent.withResources().inView('<div></div>');
component.bootstrap(au => au.use.standardConfiguration().feature('index'));
await component.create(bootstrap);

Is there a way to make this a global setting for all the tests instead?

1 Like

Jest has a concept of setupFiles where you can define things run before every testsuite (file). That might be what you’re looking for. Honestly though, for readability and understandable tests I’d extract your setup in a function and import/call it per file. Its just a few lines and adds far more flexibility. Think of a scenario that for certain suites it should behave different. Your custom arrange function could consume a condition and you stay flexible

1 Like

I ended up doing this:

// jest-pretest.ts
global.bootStrapEnvironment = async (component) => {
    component.bootstrap(au => au.use.standardConfiguration().feature('index'));
    await component.create(bootstrap);
}

// generic-component.test.ts
...
component = StageComponent.withResources().inView('<bleh></bleh>');
await bootStrapEnvironment(component);
1 Like

Yep pretest is ok as well pretty much the same as importing from another file :+1:

1 Like