# Module Exports

When using modules, there are two ways to export functions for use within tests:

# Option 1: Using the Global Context

When using the global context make sure you're module is flag as not explicit export:

Not Explicit Export

In this mode, your module will be available as if it were in the same context as the test being run. However, this means that you must be careful to namespace your modules appropriately so they do not collide with each other.

# Example:

Module: account.js

async function example(driver, param1) {
    await macro.get(driver, 'https://app.bionicmetrics.com');
}

async function anotherExample() {
    logger.info('Hello World');
}

// Exports so you can then use account.example(...)
const account = {
    anotherExample,
    example
};

Test:

async function test(baseUrl, driver, data) {
    await account.anotherExample(); 
}
test(baseUrl, driver, data); // implicitly returns a promise

# Option 2: Explicit Exports

You can also explicitly export from your module which has the advantage of not polluting the global context. When doing so, only the name of the module will be exported and used in the global context for use in tests. By doing this you could have two modules with the same function names or const declarations without collision.

To mark a module as explicit export, check the box in the module edit modal:

Explicit Export

# Example:

Module: account.js

async function example(driver, param1) {
    await macro.get(driver, 'https://app.bionicmetrics.com');
}

async function anotherExample() {
    logger.info('Hello World');
}

// Exports so you can then use account.example(...)
const account = {
    anotherExample,
    example
};
exports = account;

Note: the only difference from Option 1 is the last line with exports = account;

Note: It is also valid to use

module.exports = account;

However, this causes autocompletion/intellisense to fail given a known issue with Visual Studio Code.

Note: It is also valid to use a new object when exporting from a module:

exports = { // or module.exports
  anotherExample,
  example
};

but this also causes the editor to lose its auto-complete intellisense for this module. To ensure that intellisense works for a module use the format:

const account = {
    anotherExample,
    example
};
exports = account;

Note: It is syntactically correct to export upon definition multiple times throughout a module, Bionic Metrics does not support this:

Invalid Example

module.exports.func1 = function invalid1(); 
module.exports.func2 = function invalid2(); 
module.exports.func3 = function invalid3(); 

Test:

async function test(baseUrl, driver, data) {
    await account.anotherExample(); 
}
test(baseUrl, driver, data); // implicitly returns a promise

If you have any questions, please contact us on Slack or support@bionicmetrics.com