# How can I access global context in tests?

**URL:** <https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820>\
**Category:** Developing Nodes\
**Created:** [4 October 2021 06:56 UTC](https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820 "2021-10-04T06:56:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roman](https://discourse.nodered.org/u/Roman)\
**Post date:** [4 October 2021 06:56 UTC](https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820/1 "2021-10-04T06:56:17Z")

</div>

I'm using node-red-node-test-helper for tests of my nodes. During work nodes change global context and I should text this changes. Here is my code:

```auto
let n1 = helper.getNode("n1");
n1.on('input', function(msg){
    let gcontext = n1.context().global;
    // other stuff
}

```

But gcontext is undefined. Am I missing something?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [4 October 2021 08:05 UTC](https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820/2 "2021-10-04T08:05:29Z")

</div>

There is documentation how to access data in context. You have the wrong syntax

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 October 2021 08:06 UTC](https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820/3 "2021-10-04T08:06:32Z")

</div>

Hi @Roman

The test helper doesn't automatically initialise context. It probably should do more to help in that area.

For now, you have to initialise context yourself - you can see how that is done in the Change node's unit tests - [node-red/15-change\_spec.js at master · node-red/node-red · GitHub](https://github.com/node-red/node-red/blob/master/test/nodes/core/function/15-change_spec.js#L31)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 October 2021 08:07 UTC](https://discourse.nodered.org/t/how-can-i-access-global-context-in-tests/51820/4 "2021-10-18T08:07:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
