@zohodesk/testinglibrary 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,13 +10,9 @@ exports.verifyIfCookieFileExists = verifyIfCookieFileExists;
10
10
  var _path = _interopRequireDefault(require("path"));
11
11
  var _fs = require("fs");
12
12
  var _readConfigFile = require("../../readConfigFile");
13
- var _checkAuthDirectory = require("../checkAuthDirectory");
14
13
  var _logger = require("../../../../utils/logger");
15
14
  /* eslint-disable no-console */
16
15
 
17
- const authContent = {
18
- cookies: []
19
- };
20
16
  function getAuthFileDirectory() {
21
17
  const {
22
18
  uatDirectory
@@ -24,11 +20,7 @@ function getAuthFileDirectory() {
24
20
  return _path.default.resolve(_path.default.join(uatDirectory, 'playwright', '.auth'));
25
21
  }
26
22
  function verifyIfCookieFileExists(authFile) {
27
- (0, _checkAuthDirectory.createAuthDirectoryIfNotExist)();
28
- if (!(0, _fs.existsSync)(authFile)) {
29
- console.log('creating auth file..');
30
- (0, _fs.writeFileSync)(authFile, JSON.stringify(authContent, null, 2));
31
- }
23
+ return (0, _fs.existsSync)(authFile);
32
24
  }
33
25
  function convertCookiesToParse(cookies, authFilePath) {
34
26
  try {
@@ -39,8 +31,11 @@ function convertCookiesToParse(cookies, authFilePath) {
39
31
  }
40
32
  }
41
33
  async function loadCookiesIfPresent(page, authFile) {
42
- verifyIfCookieFileExists(authFile);
43
- const cookies = (0, _fs.readFileSync)(authFile);
44
- const parsedCookies = convertCookiesToParse(cookies, authFile);
45
- await page.context().addCookies(parsedCookies.cookies === undefined ? [] : parsedCookies.cookies);
34
+ if (verifyIfCookieFileExists(authFile)) {
35
+ const fileContent = (0, _fs.readFileSync)(authFile);
36
+ const stroageState = convertCookiesToParse(fileContent, authFile);
37
+ if (stroageState.cookies) {
38
+ await page.context().addCookies(stroageState.cookies);
39
+ }
40
+ }
46
41
  }
@@ -10,6 +10,7 @@ var _fileMutex = _interopRequireDefault(require("../fileMutex"));
10
10
  var _fileMutexConfig = require("../../constants/fileMutexConfig");
11
11
  var _checkAuthCookies = require("./checkAuthCookies");
12
12
  var _checkAuthDirectory = require("../checkAuthDirectory");
13
+ var _fs = require("fs");
13
14
  /* eslint-disable no-console */
14
15
 
15
16
  async function performLoginSteps(testInfo, isLoggedIn, loginSteps) {
@@ -21,9 +22,15 @@ async function performLoginSteps(testInfo, isLoggedIn, loginSteps) {
21
22
  authFilePrefix = authFilePrefix || email;
22
23
  const authFile = _path.default.resolve(_path.default.join((0, _checkAuthCookies.getAuthFileDirectory)(), `${authFilePrefix}-cookies.json`));
23
24
  const lockFileName = email.replace(/[@.]/g, '_');
24
- const fileMutex = new _fileMutex.default((0, _checkAuthDirectory.getLockDirectoryPath)(), lockFileName, email, authFile, _fileMutexConfig.fileDeletionTimeoutConfig);
25
+ const fileMutex = new _fileMutex.default((0, _checkAuthDirectory.getLockDirectoryPath)(), lockFileName, _fileMutexConfig.fileDeletionTimeoutConfig);
26
+ let loginUsingCookie = false;
25
27
  try {
26
- await fileMutex.acquire();
28
+ if ((0, _checkAuthCookies.verifyIfCookieFileExists)(authFile)) {
29
+ console.log(`${email} Cookie file exists. Loading cookies, worker index - ${process.env.TEST_WORKER_INDEX}`);
30
+ loginUsingCookie = true;
31
+ } else {
32
+ await fileMutex.acquire();
33
+ }
27
34
  await (0, _checkAuthCookies.loadCookiesIfPresent)(page, authFile);
28
35
  const isAlreadyLoggedIn = await isLoggedIn(testInfo);
29
36
  if (!isAlreadyLoggedIn) {
@@ -36,7 +43,9 @@ async function performLoginSteps(testInfo, isLoggedIn, loginSteps) {
36
43
  } catch (error) {
37
44
  console.error(`Error during login for ${email}:`, error);
38
45
  } finally {
39
- await fileMutex.release();
46
+ if (!loginUsingCookie) {
47
+ await fileMutex.release();
48
+ }
40
49
  }
41
50
  }
42
51
  var _default = exports.default = performLoginSteps;
@@ -12,25 +12,19 @@ function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("C
12
12
  function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
13
13
  var _FileMutex_brand = /*#__PURE__*/new WeakSet();
14
14
  class FileMutex {
15
- constructor(directory, lockFileName, email, authFile, fileDeletionTimeoutConfig) {
15
+ constructor(directory, lockFileName, fileDeletionTimeoutConfig) {
16
16
  _classPrivateMethodInitSpec(this, _FileMutex_brand);
17
17
  this.directory = directory;
18
18
  this.lockFileName = lockFileName + ".lock";
19
- this.email = email;
20
- this.authFile = authFile;
21
19
  this.fileDeletionTimeout = fileDeletionTimeoutConfig.timeout;
22
- this.createDirectoryIfNotExist();
23
20
  this.lockFilePath = _assertClassBrand(_FileMutex_brand, this, _getLockFilePath).call(this);
21
+ _assertClassBrand(_FileMutex_brand, this, _createDirectoryIfNotExist).call(this);
24
22
  }
25
23
  async acquire() {
26
- if ((0, _fs.existsSync)(this.authFile)) {
27
- console.log(`${this.email} Cookie file exists. Loading cookies...`);
28
- return;
29
- }
30
24
  return new Promise((resolve, reject) => {
31
25
  if (!(0, _fs.existsSync)(this.lockFilePath)) {
32
26
  (0, _fs.writeFileSync)(this.lockFilePath, 'locked');
33
- console.log(`Lock file created: ${this.lockFilePath}`);
27
+ console.log(`Lock file created: ${this.lockFilePath}, worker index - ${process.env.TEST_WORKER_INDEX}`);
34
28
  return resolve();
35
29
  }
36
30
  const timeout = setTimeout(() => {
@@ -41,7 +35,7 @@ class FileMutex {
41
35
  try {
42
36
  if (eventType === 'rename' && filename === this.lockFileName) {
43
37
  clearTimeout(timeout);
44
- console.log(`Lock file deleted! Proceeding...`);
38
+ console.log(`Lock file deleted! Proceeding, worker index - ${process.env.TEST_WORKER_INDEX}`);
45
39
  watcher.close();
46
40
  resolve();
47
41
  }
@@ -56,21 +50,22 @@ class FileMutex {
56
50
  try {
57
51
  if ((0, _fs.existsSync)(this.lockFilePath)) {
58
52
  (0, _fs.unlinkSync)(this.lockFilePath);
59
- console.log(`Lock file deleted: ${this.lockFilePath}`);
53
+ console.log(`Lock file deleted: ${this.lockFilePath}, worker index - ${process.env.TEST_WORKER_INDEX}`);
60
54
  }
61
55
  } catch (err) {
62
- console.error(`Error deleting lock file: ${err.message}`);
63
- }
64
- }
65
- async createDirectoryIfNotExist() {
66
- if (!(0, _fs.existsSync)(this.directory)) {
67
- (0, _fs.mkdirSync)(this.directory, {
68
- recursive: true
69
- });
56
+ console.error(`Error deleting lock file: ${err.message}, worker index - ${process.env.TEST_WORKER_INDEX}`);
57
+ throw err;
70
58
  }
71
59
  }
72
60
  }
73
61
  function _getLockFilePath() {
74
62
  return _path.default.resolve(_path.default.join(this.directory, this.lockFileName));
75
63
  }
64
+ async function _createDirectoryIfNotExist() {
65
+ if (!(0, _fs.existsSync)(this.directory)) {
66
+ (0, _fs.mkdirSync)(this.directory, {
67
+ recursive: true
68
+ });
69
+ }
70
+ }
76
71
  var _default = exports.default = FileMutex;
@@ -8,8 +8,6 @@ jest.mock('fs');
8
8
  describe('FileMutex', () => {
9
9
  const directory = '/tmp/locks';
10
10
  const lockFileName = 'test-lock';
11
- const email = 'test@example.com';
12
- const authFile = '/tmp/authFile';
13
11
  const fileDeletionTimeoutConfig = {
14
12
  timeout: 1000
15
13
  };
@@ -17,27 +15,14 @@ describe('FileMutex', () => {
17
15
  let fileMutex;
18
16
  beforeEach(() => {
19
17
  jest.clearAllMocks();
20
- fileMutex = new _fileMutex.default(directory, lockFileName, email, authFile, fileDeletionTimeoutConfig);
18
+ fileMutex = new _fileMutex.default(directory, lockFileName, fileDeletionTimeoutConfig);
21
19
  });
22
20
  describe('acquire', () => {
23
- it('should create directory if it does not exist', async () => {
24
- _fs.existsSync.mockReturnValue(false);
25
- await fileMutex.createDirectoryIfNotExist();
26
- expect(_fs.mkdirSync).toHaveBeenCalledWith(directory, {
27
- recursive: true
28
- });
29
- });
30
21
  it('should create the lock file if it does not exist', async () => {
31
- _fs.existsSync.mockImplementation(filePath => filePath === authFile ? false : false);
32
22
  _fs.writeFileSync.mockImplementation(() => {});
33
23
  await fileMutex.acquire();
34
24
  expect(_fs.writeFileSync).toHaveBeenCalledWith(fileMutex.lockFilePath, 'locked');
35
25
  });
36
- it('should resolve immediately if auth file exists', async () => {
37
- _fs.existsSync.mockImplementation(filePath => filePath === authFile);
38
- await fileMutex.acquire();
39
- expect(_fs.existsSync).toHaveBeenCalledWith(authFile);
40
- });
41
26
  it('should wait for lock file deletion if it exists', async () => {
42
27
  _fs.existsSync.mockImplementation(filePath => filePath === fileMutex.lockFilePath);
43
28
  _fs.watch.mockImplementation((dir, callback) => {
@@ -88,9 +73,7 @@ describe('FileMutex', () => {
88
73
  _fs.unlinkSync.mockImplementation(() => {
89
74
  throw new Error(errorMessage);
90
75
  });
91
- console.error = jest.fn();
92
- await fileMutex.release();
93
- expect(console.error).toHaveBeenCalledWith(`Error deleting lock file: ${errorMessage}`);
76
+ await expect(fileMutex.release()).rejects.toThrow(errorMessage);
94
77
  });
95
78
  });
96
79
  });