我知道与此问题相关的问题很多,据我所知,我理解它的本质,但是我不明白为什么在我明确设置和控制的类型上会发生这种问题。
我故意在noImplicitAny
内将true
设置为tsconfig.json
;我知道,如果我将其转为false
,此问题将消失-但我不想这样做……但:)
这是代码:
# ./src/page/base.page.ts
export default class BasePage {
public open(path: string) {
browser.url(path);
}
}
# ./src/page/home-search.page.ts
import BasePage from "./base.page";
class HomeSearchPage extends BasePage {
public fillForm(term: string) {
$("#search_form_input_homepage").setValue(term);
}
public open() {
super.open(`${browser.options.baseUrl}`);
}
public submit() {
$("#search_button_homepage").click();
}
}
export default new HomeSearchPage();
# ./test/spec/search.engine.spec.ts
import HomeSearchPage from "@page/home-search.page";
import { expect } from "chai";
describe("DuckDuckGo (DDG) Search Engine", () => {
it("Verify search result(s) for given term", () => {
const expected: string = "WebdriverIO · Next-gen WebDriver test framework for Node.js";
const input: string = "webdriverio";
HomeSearchPage.open();
HomeSearchPage.fillForm(input);
HomeSearchPage.submit();
expect($("div#r1-0 h2.result__title a.result__a").getText()).to.contain(expected);
});
});
...最后,我的tsconfig.json
(位于项目的根目录):
{
"compilerOptions": {
"alwaysStrict": true,
"baseUrl": ".",
"module": "commonjs",
"paths": {
"*": ["./*"],
"@page/*": ["./src/page/*"]
},
"removeComments": true,
"strict": true,
"target": "es2018",
"types": [
"@wdio/mocha-framework",
"@wdio/sync",
"@types/chai",
"@types/mocha",
"node"
],
//---------------------------------------------------------------------------------------------
// Experimental Settings
//---------------------------------------------------------------------------------------------
"noImplicitAny": true
},
"exclude": ["node_modules/"],
"include": ["./src/**/*", "./test/**/*"]
}
通过该设置,我总是收到相同的错误消息:
[0-0] RUNNING in chrome - /test/spec/search.engine.spec.ts
0-0 worker error { name: 'TSError',
message:
'⨯ Unable to compile TypeScript:\nsrc/page/home-search.page.ts(5,14): error TS7006: Parameter \'term\' implicitly has an \'any\' type.\n',
stack:
'TSError: ⨯ Unable to compile TypeScript:\nsrc/page/home-search.page.ts(5,14): error TS7006: Parameter \'term\' implicitly has an \'any\' type.\n\n at createTSError
...
at Module.load (internal/modules/cjs/loader.js:653:32)' }
[0-0] FAILED in chrome - /test/spec/search.engine.spec.ts
答案 0 :(得分:1)
此错误来自ts-node
。在您的wdio.conf.js
中,将您的before: function()
更改为此:
before: function (capabilities, specs) {
require("ts-node").register({ files: true, transpileOnly: true });
},
This question here还提到了一个与您遇到的问题非常相似的问题。我在本地撤下了您的仓库,并成功进行了测试。