Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance validateMethod to Include File Paths in Duplicate Command Errors #4324

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/api/_loaders/_command-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
this.ignoreUnderscoreLeadingNames = true;
}

static commandRegistry = new Map();

Check failure on line 19 in lib/api/_loaders/_command-loader.js

View workflow job for this annotation

GitHub Actions / linux (16.x)

'Map' is not defined

Check failure on line 19 in lib/api/_loaders/_command-loader.js

View workflow job for this annotation

GitHub Actions / windows (16.x)

'Map' is not defined

Check failure on line 19 in lib/api/_loaders/_command-loader.js

View workflow job for this annotation

GitHub Actions / linux (18.x)

'Map' is not defined

Check failure on line 19 in lib/api/_loaders/_command-loader.js

View workflow job for this annotation

GitHub Actions / windows (18.x)

'Map' is not defined

Check failure on line 19 in lib/api/_loaders/_command-loader.js

View workflow job for this annotation

GitHub Actions / linux (20.x)

'Map' is not defined

static isTypeImplemented(instance, method, type) {
const methodTypes = method.split('|');

Expand Down Expand Up @@ -50,15 +52,26 @@
this.nightwatchInstance.overridableCommands.add(this.commandName);
}

if (this.nightwatchInstance.isApiMethodDefined(this.commandName, namespace) && !this.nightwatchInstance.overridableCommands.has(this.commandName)) {
const err = new TypeError(`Error while loading the API commands: the ${this.type} ${this.namespace || ''}.${this.commandName}() is already defined.`);
const commandKey = `${this.namespace || ''}.${this.commandName}`;
const currentFile = this.fileName;

if (BaseCommandLoader.commandRegistry.has(commandKey)) {
const firstDefinedFile = BaseCommandLoader.commandRegistry.get(commandKey);

const err = new TypeError(
`Error while loading the API commands: the ${this.type} ${commandKey}() is already defined.\n` +
`- First defined in: ${firstDefinedFile}\n` +
`- Current file: ${currentFile}`
);

err.displayed = false;
err.detailedErr = `Source: ${this.fileName}`;
err.showTrace = false;

throw err;
}

BaseCommandLoader.commandRegistry.set(commandKey, currentFile);

return this;
}

Expand Down
49 changes: 49 additions & 0 deletions test/src/api/commands/testDuplicateCommandError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const assert = require('assert');
const BaseCommandLoader = require('../../../../lib/api/_loaders/_command-loader.js');

describe('BaseCommandLoader - Duplicate Command Error', function () {
it('should throw an error with file paths when duplicate commands are defined', function () {
// Create two instances of the command loader to simulate commands from two different files
const loader1 = new BaseCommandLoader({});
loader1.fileName = '/path/to/firstFile.js';
loader1.commandName = 'testCommand';
loader1.namespace = 'testNamespace';

const loader2 = new BaseCommandLoader({});
loader2.fileName = '/path/to/secondFile.js';
loader2.commandName = 'testCommand';
loader2.namespace = 'testNamespace';

// Validate the first command, it should register without issues
loader1.validateMethod(null);

// The second command should throw a detailed error
assert.throws(
() => loader2.validateMethod(null),
(err) => {
assert.ok(err instanceof TypeError);
assert.ok(err.message.includes('testNamespace.testCommand'));
assert.ok(err.message.includes('/path/to/firstFile.js'));
assert.ok(err.message.includes('/path/to/secondFile.js'));

return true;
}
);
});

it('should not throw an error for commands in different namespaces', function () {
const loader1 = new BaseCommandLoader({});
loader1.fileName = '/path/to/firstFile.js';
loader1.commandName = 'testCommand';
loader1.namespace = 'namespace1';

const loader2 = new BaseCommandLoader({});
loader2.fileName = '/path/to/secondFile.js';
loader2.commandName = 'testCommand';
loader2.namespace = 'namespace2';

// Both commands should register without issues
assert.doesNotThrow(() => loader1.validateMethod(null));
assert.doesNotThrow(() => loader2.validateMethod(null));
});
});
Loading