How to use fs.pathExists 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.

tests.js

Source: tests.js Github

copy
1import { rollup } from "rollup";
2import fs from "fs-extra";
3import path from "path";
4import os from "os";
5import copy from "../src";
6
7let TEST_DIR;
8
9beforeEach(async () => {
10  TEST_DIR = path.join(os.tmpdir(), "rollup-plugin-copy-assets");
11  await fs.emptyDir(TEST_DIR);
12});
13
14afterAll(async () => {
15  await fs.emptyDir(TEST_DIR);
16});
17
18it("should copy the assets to the output dir", async () => {
19  const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
20  const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
21
22  const input = {
23    input: `${__dirname}/fixtures/index.js`,
24    plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
25  };
26  const output = {
27    file: BUNDLE_PATH,
28    format: "iife",
29    name: "test",
30  };
31  const bundle = await rollup(input);
32  await bundle.write(output);
33
34  await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
35  await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
36});
37
38it("should not fail when used with an array of inputs", async () => {
39  const BUNDLE1_PATH = path.join(TEST_DIR, "index.js");
40  const BUNDLE2_PATH = path.join(TEST_DIR, "index2.js");
41  const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
42
43  const input = {
44    input: [
45      path.join(__dirname, "fixtures", "index.js"),
46      path.join(__dirname, "fixtures", "index2.js"),
47    ],
48    plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
49  };
50  const output = {
51    dir: TEST_DIR,
52    format: "cjs",
53  };
54  const bundle = await rollup(input);
55  await bundle.write(output);
56
57  await expect(fs.pathExists(BUNDLE1_PATH)).resolves.toEqual(true);
58  await expect(fs.pathExists(BUNDLE2_PATH)).resolves.toEqual(true);
59  await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
60});
61
62it("should not fail when used with an object of inputs", async () => {
63  const BUNDLE1_PATH = path.join(TEST_DIR, "index.js");
64  const BUNDLE2_PATH = path.join(TEST_DIR, "index2.js");
65  const ASSET_PATH = path.join(TEST_DIR, "top-level-item.txt");
66
67  const input = {
68    input: {
69      index: path.join(__dirname, "fixtures", "index.js"),
70      index2: path.join(__dirname, "fixtures", "index2.js"),
71    },
72    plugins: [copy({ assets: ["fixtures/top-level-item.txt"] })],
73  };
74  const output = {
75    dir: TEST_DIR,
76    format: "cjs",
77  };
78  const bundle = await rollup(input);
79  await bundle.write(output);
80
81  await expect(fs.pathExists(BUNDLE1_PATH)).resolves.toEqual(true);
82  await expect(fs.pathExists(BUNDLE2_PATH)).resolves.toEqual(true);
83  await expect(fs.pathExists(ASSET_PATH)).resolves.toEqual(true);
84});
85
86it("should copy directories of assets", async () => {
87  const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
88  const TOP_LEVEL_ASSET = path.join(TEST_DIR, "top-level-item.txt");
89  const ASSET_FOLDER = path.join(TEST_DIR, "assets");
90  const CSV_ASSET = path.join(ASSET_FOLDER, "bar.csv");
91  const TXT_ASSET = path.join(ASSET_FOLDER, "foo.txt");
92
93  await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(false);
94  await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(false);
95  await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(false);
96  await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(false);
97
98  const input = {
99    input: path.join(__dirname, "fixtures", "index.js"),
100    plugins: [
101      copy({ assets: ["fixtures/assets", "fixtures/top-level-item.txt"] }),
102    ],
103  };
104  const output = {
105    file: BUNDLE_PATH,
106    format: "iife",
107    name: "test",
108  };
109
110  const bundle = await rollup(input);
111  await bundle.write(output);
112
113  await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
114  await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
115  await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
116  await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
117});
118
119it("should not fail when an asset or directory already exists", async () => {
120  const BUNDLE_PATH = path.join(TEST_DIR, "bundle.js");
121  const ASSET_FOLDER = path.join(TEST_DIR, "assets");
122  const TOP_LEVEL_ASSET = path.join(TEST_DIR, "top-level-item.txt");
123  const CSV_ASSET = path.join(ASSET_FOLDER, "bar.csv");
124  const TXT_ASSET = path.join(ASSET_FOLDER, "foo.txt");
125
126  // Create all of the files so they exist
127  await fs.ensureFile(BUNDLE_PATH);
128  await fs.ensureFile(TOP_LEVEL_ASSET);
129  await fs.ensureFile(CSV_ASSET);
130  await fs.ensureFile(TXT_ASSET);
131
132  await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
133  await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
134  await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
135  await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
136
137  const input = {
138    input: path.join(__dirname, "fixtures", "index.js"),
139    plugins: [
140      copy({ assets: ["fixtures/assets", "fixtures/top-level-item.txt"] }),
141    ],
142  };
143  const output = {
144    file: BUNDLE_PATH,
145    format: "iife",
146    name: "test",
147  };
148
149  const bundle = await rollup(input);
150  await bundle.write(output);
151
152  await expect(fs.pathExists(BUNDLE_PATH)).resolves.toEqual(true);
153  await expect(fs.pathExists(TOP_LEVEL_ASSET)).resolves.toEqual(true);
154  await expect(fs.pathExists(CSV_ASSET)).resolves.toEqual(true);
155  await expect(fs.pathExists(TXT_ASSET)).resolves.toEqual(true);
156});
157
Full Screen

index.test.js

Source: index.test.js Github

copy
1/* eslint-env mocha */
2
3const chai = require('chai')
4const path = require('path')
5const fs = require('fs-extra')
6const os = require('os')
7const proxyquire = require('proxyquire')
8const Logger = require('../../../src/utils/logger')
9
10const expect = chai.expect
11const temp = os.tmpdir()
12
13const modulePath = '../../../src/utils/create-new-project'
14const createNewProject = require(modulePath)
15
16const defaultOptions = {
17  skipGit: true,
18  skipInstall: true,
19  quiet: true,
20  logger: new Logger({ quiet: true })
21}
22
23let appName, appPath
24
25describe('utils/create-new-project', () => {
26  beforeEach(() => {
27    appName = `test-${Date.now()}`
28    appPath = path.join(temp, appName)
29  })
30
31  it('should create an app project from the template', async () => {
32    await createNewProject(temp, 'app', appName, defaultOptions)
33    expect(await fs.pathExists(appPath)).to.equal(true)
34
35    const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
36    expect(packageInfo.name).to.equal(appName)
37
38    const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
39    expect(koopConfig.type).to.equal('app')
40    expect(koopConfig.plugins).to.be.an('Array')
41
42    expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
43    expect(await fs.pathExists(path.join(appPath, 'src/routes.js'))).to.equal(true)
44    expect(await fs.pathExists(path.join(appPath, 'src/plugins.js'))).to.equal(true)
45    expect(await fs.pathExists(path.join(appPath, 'src/request-handlers/welcome-page.js'))).to.equal(true)
46  })
47
48  it('should create a provider project from the template', async () => {
49    await createNewProject(temp, 'provider', appName, defaultOptions)
50    expect(await fs.pathExists(appPath)).to.equal(true)
51
52    const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
53    expect(packageInfo.name).to.equal(appName)
54
55    const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
56    expect(koopConfig.type).to.equal('provider')
57    expect(koopConfig.name).to.be.a('string')
58    expect(koopConfig.allowedParams).to.be.an('object')
59
60    expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
61    expect(await fs.pathExists(path.join(appPath, 'src/model.js'))).to.equal(true)
62
63    expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
64    expect(await fs.pathExists(path.join(appPath, 'test/model.test.js'))).to.equal(true)
65  })
66
67  it('should create an auth plugin project from the template', async () => {
68    await createNewProject(temp, 'auth', appName, defaultOptions)
69    expect(await fs.pathExists(appPath)).to.equal(true)
70
71    const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
72    expect(packageInfo.name).to.equal(appName)
73
74    const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
75    expect(koopConfig.type).to.equal('auth')
76
77    expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
78    expect(await fs.pathExists(path.join(appPath, 'src/authenticate.js'))).to.equal(true)
79    expect(await fs.pathExists(path.join(appPath, 'src/authorize.js'))).to.equal(true)
80    expect(await fs.pathExists(path.join(appPath, 'src/authentication-specification.js'))).to.equal(true)
81
82    expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
83    expect(await fs.pathExists(path.join(appPath, 'test/authenticate.test.js'))).to.equal(true)
84    expect(await fs.pathExists(path.join(appPath, 'test/authentication-specification.test.js'))).to.equal(true)
85    expect(await fs.pathExists(path.join(appPath, 'test/data.geojson'))).to.equal(true)
86  })
87
88  it('should create an output plugin project from the template', async () => {
89    await createNewProject(temp, 'output', appName, defaultOptions)
90    expect(await fs.pathExists(appPath)).to.equal(true)
91
92    const packageInfo = await fs.readJson(path.join(appPath, 'package.json'))
93    expect(packageInfo.name).to.equal(appName)
94
95    const koopConfig = await fs.readJson(path.join(appPath, 'koop.json'))
96    expect(koopConfig.type).to.equal('output')
97
98    expect(await fs.pathExists(path.join(appPath, 'src/index.js'))).to.equal(true)
99    expect(await fs.pathExists(path.join(appPath, 'src/routes.js'))).to.equal(true)
100    expect(await fs.pathExists(path.join(appPath, 'src/request-handlers/serve.js'))).to.equal(true)
101
102    expect(await fs.pathExists(path.join(appPath, 'test/index.test.js'))).to.equal(true)
103    expect(await fs.pathExists(path.join(appPath, 'test/routes.test.js'))).to.equal(true)
104    expect(await fs.pathExists(path.join(appPath, 'test/request-handlers/serve.test.js'))).to.equal(true)
105    expect(await fs.pathExists(path.join(appPath, 'test/data.geojson'))).to.equal(true)
106  })
107
108  it('should update the config file if the config is specified with a JSON', async () => {
109    await createNewProject(
110      temp,
111      'app',
112      appName,
113      {
114        ...defaultOptions,
115        config: { port: 3000 }
116      }
117    )
118
119    const configPath = path.join(appPath, 'config/default.json')
120    expect(await fs.pathExists(configPath)).to.equal(true)
121
122    const config = await fs.readJson(configPath)
123    expect(config.port).to.equal(3000)
124  })
125
126  it('should set the npm client if specified', async () => {
127    await createNewProject(
128      temp,
129      'app',
130      appName,
131      {
132        ...defaultOptions,
133        npmClient: 'yarn'
134      }
135    )
136
137    const configPath = path.join(appPath, 'koop.json')
138    const config = await fs.readJson(configPath)
139    expect(config.npmClient).to.equal('yarn')
140  })
141
142  it('should set the deployment addon files if the deployment targe is specified', async () => {
143    const createMock = proxyquire(modulePath, {
144      './add-deployment-target': (cwd, options) => {
145        expect(options.deploymentTarget).to.equal('docker')
146      }
147    })
148
149    await createMock(
150      temp,
151      'app',
152      appName,
153      {
154        ...defaultOptions,
155        deploymentTarget: 'docker'
156      }
157    )
158  })
159})
160
Full Screen

_fsUtils.js

Source: _fsUtils.js Github

copy
1const path = require('path');
2const fs = require('fs-extra');
3const _Directorys = require('./_directorys');
4const sleep = require('./_sleep');
5
6const createDirectory = async (directoryToMake) => {
7  await sleep(250);
8  if (await fs.pathExists(directoryToMake) === false) await fs.mkdir(directoryToMake);
9};
10
11const createRecursiveDirectory = async (directoriesToMake) => {
12  try {
13    await sleep(250);
14    for (const dir of directoriesToMake) await createDirectory(dir);
15  } catch (err) {
16    return console.error(err);
17  }
18};
19
20const checkWorkingDirectory = async () => {
21  await sleep(250);
22  if (await fs.pathExists(_Directorys.shopRoot) === true
23    && await fs.pathExists(_Directorys.productionRoot) === true
24    && await fs.pathExists(_Directorys.developmentRoot) === true
25    && await fs.pathExists(_Directorys.devRoot) === true
26    && await fs.pathExists(_Directorys.scriptsRoot) === true
27    && await fs.pathExists(_Directorys.scriptsModuleRoot) === true
28    && await fs.pathExists(_Directorys.stylesRoot) === true
29    && await fs.pathExists(_Directorys.fontsRoot) === true
30    && await fs.pathExists(_Directorys.imagesRoot) === true) return true;
31};
32
33const checkDistDirectory = async () => {
34  await sleep(250);
35  if (await fs.pathExists(_Directorys.shopRoot) === true
36    && await fs.pathExists(_Directorys.productionRoot) === true
37    && await fs.pathExists(_Directorys.distAssetsRoot) === true
38    && await fs.pathExists(_Directorys.distConfigRoot) === true
39    && await fs.pathExists(_Directorys.distLayoutRoot) === true
40    && await fs.pathExists(_Directorys.distLocalesRoot) === true
41    && await fs.pathExists(_Directorys.distSectionsRoot) === true
42    && await fs.pathExists(_Directorys.distSnippetsRoot) === true
43    && await fs.pathExists(_Directorys.distTemplatesRoot) === true) return true;
44};
45
46const cloneDirectory = async (directoryToCopy = _Directorys.productionRoot, directoryDestination = _Directorys.developmentRoot) => {
47  await sleep(250);
48  await fs.copy(directoryToCopy, directoryDestination);
49};
50
51const moveFile = async (fileToMove, fileDestination) => {
52  await sleep(250);
53  await fs.move(fileToMove, `${fileDestination}/${path.basename(fileToMove)}`)
54};
55
56module.exports = {
57  createDirectory,
58  createRecursiveDirectory,
59  checkWorkingDirectory,
60  checkDistDirectory,
61  cloneDirectory,
62  moveFile
63};
64
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)