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);
expect(nodeVersion).to.equal('v10.17.0'); 
// ✅ call a custom function
const boolean = await cliAction((param) => param, true);
expect(boolean).to.equal(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('http://jsbin.testim.io/quh/1');
    const ten = await cliAction((ron) => ron, 10);
    expect(ten).to.equal(10);
    const string = await cliAction((benji) => benji, "benji");
    expect(string).to.equal("benji");
    const boolean = await cliAction((elad) => elad, true);
    expect(boolean).to.equal(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]*"));

    expect(packageData.fooBar).to.equal("fooBar");
    expect(packageData.moment).to.be.a("string");
});