cliAction(javascriptFunction [,...args])


This is a TDK feature

This is not supported in Testim's visual editor

This method is used to execute Node.js code in the context of a test. You can read more about the CLI action step here.

Note: This method does not support closures and both the function and the return value must be serializable:

  • javascriptFunction {function} a JavaScript function to evaluate
  • args {any[]} arguments to pass to the function, must be serializable or NodePackage
  • returns: Promise which fulfills with the return value (in JavaScript).
// ✅ get the Node.js version
const nodeVersion = await cliAction(() => process.version);
// ✅ call a custom function
const boolean = await cliAction((param) => param, true);

// ❌ pass a function and not a string
await cliAction('someFunction()');
// ❌ do not use closures
const foo = '15';
await cliAction(() => return foo);

Full Example:

'use strict';

const {expect} = require('chai');
const {go, test, cliAction, NodePackage} = require('testim');

test("cli action", async () => {
    await go('');
    const ten = await cliAction((ron) => ron, 10);
    const string = await cliAction((benji) => benji, "benji");
    const boolean = await cliAction((elad) => elad, true);
    const object = await cliAction((obj) => obj, {x: 3});
    expect(object).to.deep.equal({x: 3});

    const packageData = await cliAction((fooBar, _, moment) => {
        return {fooBar: _.camelCase(fooBar), moment: moment().endOf('day').fromNow()};
    }, "Foo Bar", new NodePackage("lodash"), new NodePackage("[email protected]*"));