Search code examples
typescriptprotractorcucumberjs

Typescript Error:TS2345: Argument of type '{ theme: string; jsonFile: string; output: string; }; }' is not assignable to parameter of type 'Options'


I am getting the error in the title from the following code:

import * as fs from 'fs'
import { mkdirp } from 'mkdirp'
import * as report from 'cucumber-html-reporter'
const Cucumber = require('cucumber')

export class CucumberReportExtension {

    private static jsonDir = __dirname + '/../../test/reports/json';
    private static htmlDir = __dirname + '/../../test/reports/html';
    private static jsonFile = CucumberReportExtension.jsonDir + '/cucumber_report.json';

    private static cucumberReporterOptions = {
        theme: 'bootstrap',
        jsonFile: CucumberReportExtension.jsonFile,
        output: CucumberReportExtension.htmlDir + '/cucumber_reporter.html',
        reportSuiteAsScenarios: true,
        launchReport: true,
        metadata: {
            'App Version':'1.0.0',
            'Test Environment': 'Development',
            Browser: 'Chrome  84.0.4147.105',
            Platform: 'Windows 10',
            Parallel: 'Scenarios',
            Executed: 'Local'
        }
    }

    public static CreateReportFile(dirName) {
        // Check of the directory exist
        if (!fs.existsSync(dirName))
            mkdirp.sync(dirName);
    }

    public static GenerateCucumberReport(){

        report.generate(CucumberReportExtension.cucumberReporterOptions);
    }

}

My package.json contains:

"dependencies": {
    "@types/jasmine": "^3.5.11",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^14.0.14",
    "faker": "^5.1.0",
    "jasmine": "^3.5.0",
    "lodash": "^4.17.20",
    "log4js": "^6.3.0",
    "log4js-protractor-appender": "^1.1.2",
    "mkdirp": "latest",
    "moment": "^2.28.0",
    "pg": "^8.3.3",
    "pg-hstore": "^2.3.3",
    "protractor": "^7.0.0",
    "qs": "^6.9.4",
    "stringinject": "^2.1.1"
  },
  "devDependencies": {
    "@babel/core": "^7.10.4",
    "@babel/preset-env": "^7.10.4",
    "@types/chai": "^4.2.11",
    "@types/cucumber": "^6.0.1",
    "@types/load-json-file": "^5.1.0",
    "axios": "^0.19.2",
    "babel-loader": "^8.1.0",
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "chai-smoothie": "^0.3.2",
    "core-js": "^3.6.5",
    "cucumber": "^6.0.5",
    "cucumber-html-reporter": "^5.2.0",
    "https": "^1.0.0",
    "k6": "0.0.0",
    "load-json-file": "^6.2.0",
    "multiple-cucumber-html-reporter": "^1.16.3",
    "nodejs-nodemailer-outlook": "^1.2.3",
    "protractor-cucumber-framework": "^6.2.1",
    "request": "^2.88.2",
    "ts-node": "^8.10.2",
    "tslint": "^6.1.3",
    "tslint-etc": "^1.13.6",
    "typescript": "^3.9.7",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12"
  }
}

Solution

  • From looking at the expected type for the Options object passed to report.generate, I can see that the type which they expect for theme is a union of string literals.

    Your theme 'bootstrap' is one of those options, but in the error message you can see that typescript interpreted it as string instead of the literal string 'bootstrap'. You can use as const so that typescript sees it as a literal:

    private static cucumberReporterOptions = {
         theme: 'bootstrap' as const,
         ....