How to use stackIsFromUser method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

collectHandles.js

Source: collectHandles.js Github

copy
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4  value: true
5});
6exports.default = collectHandles;
7exports.formatHandleErrors = formatHandleErrors;
8
9function _jestMessageUtil() {
10  const data = require('jest-message-util');
11
12  _jestMessageUtil = function _jestMessageUtil() {
13    return data;
14  };
15
16  return data;
17}
18
19function _jestUtil() {
20  const data = require('jest-util');
21
22  _jestUtil = function _jestUtil() {
23    return data;
24  };
25
26  return data;
27}
28
29function _stripAnsi() {
30  const data = _interopRequireDefault(require('strip-ansi'));
31
32  _stripAnsi = function _stripAnsi() {
33    return data;
34  };
35
36  return data;
37}
38
39function _interopRequireDefault(obj) {
40  return obj && obj.__esModule ? obj : {default: obj};
41}
42
43/**
44 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
45 *
46 * This source code is licensed under the MIT license found in the
47 * LICENSE file in the root directory of this source tree.
48 */
49function stackIsFromUser(stack) {
50  // Either the test file, or something required by it
51  if (stack.includes('Runtime.requireModule')) {
52    return true;
53  } // jest-jasmine it or describe call
54
55  if (stack.includes('asyncJestTest') || stack.includes('asyncJestLifecycle')) {
56    return true;
57  } // An async function call from within circus
58
59  if (stack.includes('callAsyncCircusFn')) {
60    // jest-circus it or describe call
61    return (
62      stack.includes('_callCircusTest') || stack.includes('_callCircusHook')
63    );
64  }
65
66  return false;
67} // Inspired by https://github.com/mafintosh/why-is-node-running/blob/master/index.js
68// Extracted as we want to format the result ourselves
69
70function collectHandles() {
71  const activeHandles = new Map();
72  let hook;
73
74  try {
75    const asyncHooks = require('async_hooks');
76
77    hook = asyncHooks.createHook({
78      destroy(asyncId) {
79        activeHandles.delete(asyncId);
80      },
81
82      init: function initHook(asyncId, type) {
83        if (type === 'PROMISE' || type === 'TIMERWRAP') {
84          return;
85        }
86
87        const error = new (_jestUtil()).ErrorWithStack(type, initHook);
88
89        if (stackIsFromUser(error.stack || '')) {
90          activeHandles.set(asyncId, error);
91        }
92      }
93    });
94    hook.enable();
95  } catch (e) {
96    const nodeMajor = Number(process.versions.node.split('.')[0]);
97
98    if (e.code === 'MODULE_NOT_FOUND' && nodeMajor < 8) {
99      throw new Error(
100        'You can only use --detectOpenHandles on Node 8 and newer.'
101      );
102    } else {
103      throw e;
104    }
105  }
106
107  return () => {
108    hook.disable();
109    const result = Array.from(activeHandles.values());
110    activeHandles.clear();
111    return result;
112  };
113}
114
115function formatHandleErrors(errors, config) {
116  const stacks = new Set();
117  return (
118    errors
119      .map(err =>
120        (0, _jestMessageUtil().formatExecError)(
121          err,
122          config,
123          {
124            noStackTrace: false
125          },
126          undefined,
127          true
128        )
129      ) // E.g. timeouts might give multiple traces to the same line of code
130      // This hairy filtering tries to remove entries with duplicate stack traces
131      .filter(handle => {
132        const ansiFree = (0, _stripAnsi().default)(handle);
133        const match = ansiFree.match(/\s+at(.*)/);
134
135        if (!match || match.length < 2) {
136          return true;
137        }
138
139        const stack = ansiFree.substr(ansiFree.indexOf(match[1])).trim();
140
141        if (stacks.has(stack)) {
142          return false;
143        }
144
145        stacks.add(stack);
146        return true;
147      })
148  );
149}
150
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)