GithubHelp home page GithubHelp logo

async-local-storage-repro's Introduction

Reproduction Steps

We have a basic usage of AsyncLocalStorage (see index.js):

import { AsyncLocalStorage } from "async_hooks";
const asl = new AsyncLocalStorage();

async function pretendDbCall() {
}

async function doWork() {
  return new Promise((resolve) => {
    asl.run({value: "1234"}, async () => {
      console.log(asl.getStore());
      await pretendDbCall();
      console.log(asl.getStore());
      resolve();
    });
  });
}

doWork().then(() => console.log("done"));

Invoked via nodejs directly works

This works in nodejs 16.2.0 when invoked directly (we see value: 1234 twice):

$ node index.js
{ value: '1234' }
{ value: '1234' }
done

Invoked via jest doesn't work

However, when running the same code snippet in (see index.test.js) via jest:

node --experimental-vm-modules node_modules/.bin/jest
 PASS  ./index.test.js
  asl
    ✓ works (17 ms)

  console.log
    { value: '1234' }

      at index.test.js:13:19

  console.log
    undefined

      at index.test.js:15:19

The 2nd asl.getStore() returns undefined.

Invoke via jest + detectOpenHandles does works

Note that running Jest and detectOpenHandles restores the behavior:

node --experimental-vm-modules node_modules/.bin/jest --detectOpenHandles
 PASS  ./index.test.js
  asl
    ✓ works (17 ms)

  console.log
    { value: '1234' }

      at index.test.js:13:19

  console.log
    { value: '1234' }

      at index.test.js:15:19

Working vs. Broken Matrix

Node Version Invoked Via Result
16.1.0 node works
16.1.0 jest works
16.1.0 jest + detectOpenHandles works
16.2.0 node works
16.2.0 jest broken
16.2.0 jest + detectOpenHandles works

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.