| id | typescript |
|---|---|
| title | TypeScript Setup |
Similar to Babel setup, you can register TypeScript to compile your .ts files in your before hook of your config file. You will need ts-node and tsconfig-paths as the installed devDependencies.
// wdio.conf.js
before: function() {
require('ts-node').register({ files: true });
},Similarly for mocha:
// wdio.conf.js
mochaOpts: {
ui: 'bdd',
require: [
'tsconfig-paths/register'
]
},and your tsconfig.json needs to look like:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio"]
},
"include": [
"./src/**/*.ts"
]
}You can even use a typed configuration if you desire. All you have to do is create a plain js config file that registers typescript and requires the typed config:
require("ts-node/register")
module.exports = require("wdio.conf.ts")And in your typed configuration file:
const config: WebdriverIO.Config = {
// Put your webdriverio configuration here
}
export { config }Depending on the framework you use, you will need to add the typings for that framework to your tsconfig.json types property.
For instance, if we decide to use the mocha framework, we need to add it like this to have all typings globally available:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio", "@wdio/mocha-framework"]
},
"include": [
"./src/**/*.ts"
]
}Instead of having all type definitions globally available, you can also import only the typings that you need like this:
/*
* These import the type definition for the `test` and `suite` variables that are available in
* the beforeTest, afterTest, beforeSuite and afterSuite hooks.
*/
import { Suite, Test } from "@wdio/mocha-framework"