How to use findNodeInFullPath method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

02-with-fix-path.js

Source: 02-with-fix-path.js Github

copy
1// https://folktale.origamitower.com/api/v2.3.0/en/folktale.concurrency.task.html
2const Task = require('folktale/concurrency/task')
3
4const debug = require('debug')('verbose')
5const which = require('which')
6
7// wrap Node callback style function in a Task
8const asyncWhich = Task.fromNodeback(which)
9
10function findNodeInFullPath () {
11  debug('finding Node with $PATH %s', process.env.PATH)
12
13  // "tap" and debug log implemented using "comma operator"
14  const tapDebug = path => (debug('found Node %o', { path }), path)
15
16  return (
17    asyncWhich('node')  // async Task
18      .map(tapDebug)    // success flow
19      .orElse(err => {  // error flow
20        debug('could not find Node %o', { err })
21        // keep the rejection (still error flow)
22        return Task.rejected(err)
23      })
24  )
25}
26
27const findNodePathAndVersion = () =>
28  findNodeInFullPath()
29    // if we cannot find Node right away
30    // fix the "PATH" and try again
31    .orElse(() => {
32      debug('fixing path before searching again')
33      fixPath()
34      return findNodeInFullPath()
35    })
36
37if (!module.parent) {
38  findNodePathAndVersion()
39    .run()
40    .promise()
41    .then(console.log, console.error)
42}
43
Full Screen

find_system_node.js

Source: find_system_node.js Github

copy
1const debug = require('debug')('cypress:server:find_system_node')
2const execa = require('execa')
3const fixPath = require('fix-path')
4const Promise = require('bluebird')
5const which = require('which')
6
7const NODE_VERSION_RE = /^v(\d+\.\d+\.\d+)/m
8
9/*
10 * Find the full path to a `node` binary on the current PATH.
11 * Note about fix-path:
12 *   while fix-path is good, it can cause unexpected behavior when running Cypress locally
13 *   for example, using NVM we set local Node to 8
14 *   then fix-path adds all user paths, and the found Node is whatever we have
15 *   installed globally, like 6 or 10 (NVM path comes later)
16 *   So this function only fixes the path, if the Node cannot be found on first attempt
17 */
18function findNodeInFullPath () {
19  debug('finding Node with $PATH %s', process.env.PATH)
20
21  return Promise.fromCallback((cb) => {
22    return which('node', cb)
23  })
24  .catch(() => {
25    debug('could not find Node, trying to fix path')
26    // Fix the $PATH on macOS when run from a GUI app
27    fixPath()
28    debug('searching again with fixed $PATH %s', process.env.PATH)
29
30    return Promise.fromCallback((cb) => {
31      return which('node', cb)
32    })
33  })
34  .tap((path) => {
35    debug('found Node %o', { path })
36  })
37  .catch((err) => {
38    debug('could not find Node %o', { err })
39
40    throw err
41  })
42}
43
44function findNodeVersionFromPath (path) {
45  if (!path) {
46    return Promise.resolve(null)
47  }
48
49  return execa.stdout(path, ['-v'])
50  .then((stdout) => {
51    debug('node -v returned %o', { stdout })
52    const matches = NODE_VERSION_RE.exec(stdout)
53
54    if (matches && matches.length === 2) {
55      const version = matches[1]
56
57      debug('found Node version', { version })
58
59      return version
60    }
61  })
62  .catch((err) => {
63    debug('could not resolve Node version %o', { err })
64
65    throw err
66  })
67}
68
69function findNodePathAndVersion () {
70  return findNodeInFullPath()
71  .then((path) => {
72    return findNodeVersionFromPath(path)
73    .then((version) => {
74      return {
75        path, version,
76      }
77    })
78  })
79}
80
81module.exports = {
82  findNodePathAndVersion,
83}
84
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 Cypress 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)