# How to get or create global DI container for testing

**URL:** <https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443>\
**Category:** vNext\
**Created:** [June 12, 2024, 8:47am UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443 "2024-06-12T08:47:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 12, 2024, 8:47am UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/1 "2024-06-12T08:47:27Z")

</div>

In Aurelia 1 I was able to configure a global DI container like:

```typescript
import {Container} from "aurelia-dependency-injection";
const container = new Container();
container.makeGlobal();
const bytesFormatValueConverter = container.get(ByteFormatValueConverter);

```

How do I do this in Aurelia 2?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 12, 2024, 11:35am UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/2 "2024-06-12T11:35:54Z")

</div>

There’s no global container in v2, but you call always have something like this in your jest setup (based on your other question):

```ts
import { DI, IContainer } from 'aurelia';

declare global {
  export let container: IContainer
}

beforeEach(() => {
  global.container = DI.createContainer();
})

```

Then you will have a new container for each test at the global property `container`

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 12, 2024, 6:24pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/3 "2024-06-12T18:24:04Z")

</div>

How do I make

```typescript
const container = DI.createContainer()
const numberValueFormatter = container.get(NumberValueConverter)
numberValueFormatter.setLocale("de")

```

and

```typescript
const { appHost, startPromise, tearDown } = createFixture(
    '<div>${numberUnderTest | number}</div>',
    class App {
        numberUnderTest = 13.37;
    },
    [NumberValueConverter]
);

```

using the same instance of `NumberValueConverter`?

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 2:51pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/4 "2024-06-13T14:51:34Z")

</div>

Do you need to create it before starting the app? If not, the following suffices

```ts
let numberValueFormater: NumberValueConverter;

it('....', function () {
  const { container } = createFixture(...)

  numberValueFormater = container.get(NumberValueConverter);
})

```

* * *

If they need to be the same then you can create a single test context and use it for all fixtures:

```ts
import { TextContext } from '@aurelia/testing';

const testContext = TestContext.create();
const formatter = testContext.container.get(NumberValueFormatter);

it('...', function () {
  const {} = createFixture('<div>', class {}, [], testContext)
})

```

Though this way of reusing container is discouraged, since you are likely to be in for a surprise later as the same container in the global test context is gonna be used for everything.

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 2:58pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/5 "2024-06-13T14:58:15Z")

</div>

The second approach is what I want. But when I pass `testContext` I get the error: AUR0009

```typescript
const testContext = TestContext.create();
    const numberValueFormatter = testContext.container.get(NumberValueConverter);
    numberValueFormatter.setLocale("de")

    beforeAll(() => {
        bootstrapTestEnvironment();
    });

    // Integration Test
    it('works within a view', async () => {
        const { appHost, startPromise, tearDown } = createFixture(
            '<div>${numberUnderTest | number}</div>',
            class App {
                numberUnderTest = 13.37;
            },
            [NumberValueConverter],
            true,
            testContext
        );
...

```

When I don’t pass `textContext` the test passes.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 3:10pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/6 "2024-06-13T15:10:28Z")

</div>

I missed the order of the parameters, it should be

```diff
it('...', function () {
  const {} = createFixture(
    '<div>',
    class {},
    [],
+ true, 
    testContext
  )
})

```

edit: nvm I saw you have the same code

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 3:20pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/7 "2024-06-13T15:20:00Z")

</div>

An example how it works is here [https://stackblitz.com/edit/au2-conventions-5uskd3?file=src%2Fmy-app.spec.ts](https://stackblitz.com/edit/au2-conventions-5uskd3?file=src%2Fmy-app.spec.ts)

The reason you got 0009 with `IPlatform` is because `TestContext.create()` was called before `bootstrapTestEnvironment()`, which is what prepares the registration for `IPlatform`

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 3:22pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/8 "2024-06-13T15:22:28Z")

</div>

It works! 🤩  
This should be definitely added to the documentation!

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 3:23pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/9 "2024-06-13T15:23:54Z")

</div>

Well we definitely don’t want to encourage a reusable test context, and it likely won’t work with multiple tests I think. We probably need to have a better way to specify the root container instead. Maybe it can be an improvement for your type of usage.

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 3:26pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/10 "2024-06-13T15:26:03Z")

</div>

I don’t want a reusable test context but a mutable Singleton for the test.

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [June 13, 2024, 3:27pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/11 "2024-06-13T15:27:55Z")

</div>

Does it have to be a singleton? Whatabout

```ts
let testContext: TestContext;
let converter: NumberValueConverter;

beforeEach(() => {
  testContext = TestContext.create();
  converter = testContext.container.get(NumberValueConverter);
})

it('...', function () {
  const {} = createFixture(..., ..., ..., ..., testContext)
})

```

---

<div class="post-metadata">

**Author:** ![mreiche](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mreiche/32/1912_2.png) [@mreiche](https://discourse.aurelia.io/u/mreiche)\
**Post date:** [June 13, 2024, 3:29pm UTC](https://discourse.aurelia.io/t/how-to-get-or-create-global-di-container-for-testing/5443/12 "2024-06-13T15:29:11Z")

</div>

This is also fine. I mean, better.
