question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setInterval(...).unref is not a function when testing @google-cloud/firestore

See original GitHub issue

🐛 Bug Report

When I use Jest to test data retieval from google-cloud firestore I get the error :

TypeError: setInterval(…).unref is not a function at new unref (…/…/node_modules/.pnpm/registry.npmjs.org/@grpc/grpc-js/0.6.6/node_modules/@grpc/grpc-js/build/src/subchannel-pool.js:55:36) at Object.<anonymous> (…/…/node_modules/.pnpm/registry.npmjs.org/@grpc/grpc-js/0.6.6/node_modules/@grpc/grpc-js/build/src/subchannel-pool.js:99:30) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/@grpc/grpc-js/0.6.6/node_modules/@grpc/grpc-js/build/src/channel.js:21:27) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/@grpc/grpc-js/0.6.6/node_modules/@grpc/grpc-js/build/src/index.js:22:19) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/google-gax/1.7.0/node_modules/google-gax/build/src/grpc.js:37:14) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/google-gax/1.7.0/node_modules/google-gax/build/src/index.js:33:16) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:16:13) at Object.require (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/v1/index.js:15:25) at Function.require (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/index.js:1154:18) at ClientPool.v1 [as clientFactory] (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/index.js:315:45) at ClientPool.clientFactory [as acquire] (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/pool.js:64:35) at ClientPool.acquire [as run] (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/pool.js:121:29) at Firestore.run [as request] (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/index.js:962:33) at request (…/…/node_modules/.pnpm/registry.npmjs.org/@google-cloud/firestore/2.4.0/node_modules/@google-cloud/firestore/build/src/reference.js:1706:18)

To Reproduce

  1. install package ’ @google-cloud/firestore ’ (version 2.4.0 )
  2. Obviously you need a google account, and a firestore database with some data in it.

Test Code

 import { Firestore }  from '@google-cloud/firestore';
describe('my bugy tests', () => {
    it('test bugy test', () => {
        expect.hasAssertions();
        // Monkey Patch
        //setInterval().__proto__.unref = function () {}
        const firestore = new Firestore({
            projectId: 'dummy-project',
            keyFilename: '/Users/igor/proj1/gcp-credentials.json'
        });
        return firestore.collection('testCollection').listDocuments().then(r => {
            return firestore.getAll(...r).then(r2 => {
                console.log(r2)
                expect(
                    r2
                ).toHaveLength(2)
            })
        })
    });
});

Workaround

I was able to code an ungly workaround by using this line of code setInterval().__proto__.unref = function () {} If you uncomment this line of code in the code example below, the test executes as expected.

Jest Debug output

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "/var/folders/zm/bcn7tz5157j76dhskz618z4c0000gn/T/jest-cache-services",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "cwd": "/Users/igor/proj1/packages/wear-services",
      "dependencyExtractor": null,
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "filter": null,
      "forceCoverageMatch": [],
      "globalSetup": null,
      "globalTeardown": null,
      "globals": {},
      "haste": {
        "computeSha1": false,
        "providesModuleNodeModules": [],
        "throwOnModuleCollision": false
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
      ],
      "moduleNameMapper": {},
      "modulePathIgnorePatterns": [],
      "name": "b5e7db77dc957df263fa613d7f9236aa",
      "prettierPath": "prettier",
      "resetMocks": false,
      "resetModules": false,
      "resolver": null,
      "restoreMocks": false,
      "rootDir": "/Users/igor/proj1/packages/wear-services",
      "roots": [
        "/Users/igor/proj1/packages/wear-services/test"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupFilesAfterEnv": [],
      "skipFilter": false,
      "snapshotSerializers": [],
      "testEnvironment": "/Users/igor/proj1/node_modules/.pnpm/registry.npmjs.org/jest-environment-jsdom/24.9.0/node_modules/jest-environment-jsdom/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.[jt]s?(x)",
        "**/?(*.)+(spec|test).[tj]s?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      "testRegex": [],
      "testRunner": "/Users/igor/proj1/node_modules/.pnpm/registry.npmjs.org/jest-jasmine2/24.9.0/node_modules/jest-jasmine2/build/index.js",
      "testURL": "http://localhost",
      "timers": "real",
      "transform": [
        [
          "^.+\\.jsx?$",
          "/Users/igor/proj1/node_modules/.pnpm/registry.npmjs.org/babel-jest/24.9.0_@babel+core@7.6.2/node_modules/babel-jest/build/index.js"
        ],
        [
          "^.+\\.tsx?$",
          "/Users/igor/proj1/node_modules/.pnpm/registry.npmjs.org/ts-jest/24.1.0_jest@24.9.0/node_modules/ts-jest/dist/index.js"
        ]
      ],
      "transformIgnorePatterns": [
        "/!node_modules\\/lodash-es/"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": 0,
    "changedFilesWithAncestor": false,
    "collectCoverage": false,
    "collectCoverageFrom": null,
    "coverageDirectory": "/Users/igor/proj1/packages/wear-services/coverage",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "coverageThreshold": null,
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "filter": null,
    "globalSetup": null,
    "globalTeardown": null,
    "json": false,
    "listTests": false,
    "maxConcurrency": 5,
    "maxWorkers": 3,
    "noStackTrace": false,
    "nonFlagArgs": [
      "database/publishedProducts"
    ],
    "notify": false,
    "notifyMode": "failure-change",
    "onlyChanged": false,
    "passWithNoTests": false,
    "projects": null,
    "rootDir": "/Users/igor/proj1/packages/wear-services",
    "runTestsByPath": false,
    "skipFilter": false,
    "testFailureExitCode": 1,
    "testPathPattern": "database/publishedProducts",
    "testResultsProcessor": null,
    "testSequencer": "/Users/igor/proj1/node_modules/.pnpm/registry.npmjs.org/@jest/test-sequencer/24.9.0/node_modules/@jest/test-sequencer/build/index.js",
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": null,
    "watch": false,
    "watchman": true
  },
  "version": "24.9.0"
}

envinfo

System:
    OS: macOS High Sierra 10.13.6
    CPU: (4) x64 Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz
  Binaries:
    Node: 12.11.1 - /usr/local/bin/node
    npm: 6.11.3 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

41reactions
igoroviccommented, Oct 12, 2019

In case someone is facing the same issue using jest --env node is the solution. Thank you for your advice.

5reactions
SimenBcommented, Oct 10, 2019

unref is a node timer thing, so make sure to run your tests using jets-environment-node and not the default jsdom env

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: setTimeout(...).unref is not a function - Stack Overflow
I am running my jest test suite in nodejs ...
Read more >
Unblocking Node With Unref() - HTTP Toolkit
Unref (). Timer functions like setInterval and setTimeout in Node.js return a Timeout object, representing the ongoing timer.
Read more >
Timers | Node.js v19.3.0 Documentation
If callback is not a function, a TypeError will be thrown. This method has a custom variant for promises that is available using...
Read more >
Node.js – Timeout-ref() & Timeout-unref() method
The Timeout object is internally created and is returned from the setTimeout() and setInterval() methods. You can use this object and pass ...
Read more >
Using setTimeout() and other timer APIs in Node.js
Setting Node.js timer functions. setTimeout(); setInterval(); setImmediate(). Clearing Node.js timer functions. clearTimeout(); clearInterval() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found