summaryrefslogtreecommitdiff
path: root/test/parallel/test-async-local-storage-contexts.js
blob: 9a6327133794f6a706207feae184f055105640fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';

require('../common');
const assert = require('assert');
const vm = require('vm');
const { AsyncLocalStorage } = require('async_hooks');

// Regression test for https://github.com/nodejs/node/issues/38781

const context = vm.createContext({
  AsyncLocalStorage,
  assert
});

vm.runInContext(`
  const storage = new AsyncLocalStorage()
  async function test() {
    return storage.run({ test: 'vm' }, async () => {
      assert.strictEqual(storage.getStore().test, 'vm');
      await 42;
      assert.strictEqual(storage.getStore().test, 'vm');
    });
  }
  test()
`, context);

const storage = new AsyncLocalStorage();
async function test() {
  return storage.run({ test: 'main context' }, async () => {
    assert.strictEqual(storage.getStore().test, 'main context');
    await 42;
    assert.strictEqual(storage.getStore().test, 'main context');
  });
}
test();