or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

dependency-constraints.mdglobal-configuration.mdindex.mdrule-testing.mdtest-cases.mdutilities.md

global-configuration.mddocs/

0

# Global Configuration

1

2

Static configuration methods for managing default settings across all RuleTester instances and test execution control.

3

4

## Capabilities

5

6

### Default Configuration Management

7

8

Static methods for setting and managing default configuration that applies to all RuleTester instances.

9

10

```typescript { .api }

11

class RuleTester {

12

/**

13

* Sets the default configuration for all future RuleTester instances

14

* @param config - Configuration object that will be merged with existing defaults

15

*/

16

static setDefaultConfig(config: RuleTesterConfig): void;

17

18

/**

19

* Returns the current default configuration used for all tests

20

* @returns Read-only copy of the current default configuration

21

*/

22

static getDefaultConfig(): Readonly<RuleTesterConfig>;

23

24

/**

25

* Resets the configuration to the initial default state, removing any customizations

26

*/

27

static resetDefaultConfig(): void;

28

}

29

```

30

31

**Usage Examples:**

32

33

```typescript

34

import { RuleTester } from "@typescript-eslint/rule-tester";

35

36

// Set global defaults for all test suites

37

RuleTester.setDefaultConfig({

38

defaultFilenames: {

39

ts: "test.ts",

40

tsx: "test.tsx",

41

},

42

languageOptions: {

43

parserOptions: {

44

project: "./tsconfig.json",

45

ecmaVersion: 2022,

46

sourceType: "module",

47

},

48

},

49

dependencyConstraints: {

50

typescript: ">=4.0.0",

51

},

52

});

53

54

// All RuleTester instances will now use these defaults

55

const ruleTester1 = new RuleTester(); // Uses global config

56

const ruleTester2 = new RuleTester(); // Also uses global config

57

58

// Override specific settings while keeping global defaults

59

const ruleTester3 = new RuleTester({

60

languageOptions: {

61

parserOptions: {

62

// This will be merged with global config

63

ecmaFeatures: { jsx: true },

64

},

65

},

66

});

67

68

// Check current global configuration

69

const currentConfig = RuleTester.getDefaultConfig();

70

console.log(currentConfig.defaultFilenames); // { ts: "test.ts", tsx: "test.tsx" }

71

72

// Reset to original defaults

73

RuleTester.resetDefaultConfig();

74

const resetConfig = RuleTester.getDefaultConfig();

75

console.log(resetConfig.defaultFilenames); // { ts: "file.ts", tsx: "react.tsx" }

76

```

77

78

### Test Execution Control

79

80

Static methods for controlling individual test case execution with debugging and selective testing support.

81

82

```typescript { .api }

83

class RuleTester {

84

/**

85

* Marks a valid test case to run exclusively (overloaded for string cases)

86

* @param item - Test case (string code) to mark as "only"

87

* @returns Test case object with only: true property

88

*/

89

static only<Options extends readonly unknown[]>(

90

item: string

91

): ValidTestCase<Options>;

92

93

/**

94

* Marks a valid test case to run exclusively (overloaded for object cases)

95

* @param item - Test case object to mark as "only"

96

* @returns Test case object with only: true property

97

*/

98

static only<Options extends readonly unknown[]>(

99

item: ValidTestCase<Options>

100

): ValidTestCase<Options>;

101

102

/**

103

* Marks an invalid test case to run exclusively

104

* @param item - Invalid test case object to mark as "only"

105

* @returns Test case object with only: true property

106

*/

107

static only<MessageIds extends string, Options extends readonly unknown[]>(

108

item: InvalidTestCase<MessageIds, Options>

109

): InvalidTestCase<MessageIds, Options>;

110

}

111

```

112

113

**Usage Examples:**

114

115

```typescript

116

import { RuleTester } from "@typescript-eslint/rule-tester";

117

118

const ruleTester = new RuleTester();

119

120

// Debug a specific test by running it exclusively

121

ruleTester.run("my-rule", myRule, {

122

valid: [

123

"const x = 1;", // Regular test

124

RuleTester.only("const y = 2;"), // Only this test will run

125

"const z = 3;", // This will be skipped

126

],

127

invalid: [

128

// All invalid tests will be skipped when valid tests have "only"

129

{

130

code: "var x = 1;",

131

errors: [{ messageId: "noVar" }],

132

},

133

],

134

});

135

136

// Debug a complex test case

137

const complexTest = RuleTester.only({

138

code: `

139

interface User {

140

name: string;

141

age: number;

142

}

143

`,

144

name: "complex interface test",

145

languageOptions: {

146

parserOptions: {

147

project: "./tsconfig.json",

148

},

149

},

150

});

151

152

ruleTester.run("interface-rule", interfaceRule, {

153

valid: [

154

"const x = 1;", // Skipped

155

complexTest, // Only this runs

156

],

157

invalid: [],

158

});

159

160

// Debug an invalid test case with autofix

161

const invalidTest = RuleTester.only({

162

code: "var x = 1;",

163

errors: [{ messageId: "noVar" }],

164

output: "const x = 1;",

165

name: "debug autofix behavior",

166

});

167

168

ruleTester.run("no-var-rule", noVarRule, {

169

valid: [], // All skipped

170

invalid: [

171

invalidTest, // Only this runs

172

{

173

code: "var y = 2;",

174

errors: [{ messageId: "noVar" }],

175

}, // Skipped

176

],

177

});

178

```

179

180

### Configuration Strategies

181

182

Practical patterns for managing global configuration across test suites.

183

184

**Project-Wide Configuration:**

185

186

```typescript

187

// setup-tests.ts - Global test setup file

188

import { RuleTester } from "@typescript-eslint/rule-tester";

189

190

// Set consistent defaults for entire project

191

RuleTester.setDefaultConfig({

192

defaultFilenames: {

193

ts: "test-file.ts",

194

tsx: "test-component.tsx",

195

},

196

languageOptions: {

197

parserOptions: {

198

project: "./tsconfig.test.json",

199

ecmaVersion: 2022,

200

sourceType: "module",

201

ecmaFeatures: {

202

jsx: true,

203

globalReturn: false,

204

},

205

},

206

globals: {

207

// Test environment globals

208

jest: "readonly",

209

describe: "readonly",

210

it: "readonly",

211

expect: "readonly",

212

},

213

},

214

dependencyConstraints: {

215

typescript: ">=4.5.0",

216

"@typescript-eslint/parser": ">=5.0.0",

217

},

218

});

219

```

220

221

**Environment-Specific Configuration:**

222

223

```typescript

224

// Configure for different environments

225

if (process.env.NODE_ENV === "test") {

226

RuleTester.setDefaultConfig({

227

languageOptions: {

228

env: {

229

jest: true,

230

node: true,

231

},

232

},

233

});

234

} else if (process.env.NODE_ENV === "browser-test") {

235

RuleTester.setDefaultConfig({

236

languageOptions: {

237

env: {

238

browser: true,

239

es2022: true,

240

},

241

globals: {

242

window: "readonly",

243

document: "readonly",

244

},

245

},

246

});

247

}

248

```

249

250

**Per-Suite Configuration:**

251

252

```typescript

253

// rule-tests/string-rules.test.ts

254

describe("String manipulation rules", () => {

255

beforeAll(() => {

256

RuleTester.setDefaultConfig({

257

// String-specific test configuration

258

languageOptions: {

259

parserOptions: {

260

ecmaVersion: 2020, // String methods available in ES2020

261

},

262

},

263

});

264

});

265

266

afterAll(() => {

267

RuleTester.resetDefaultConfig(); // Clean up for other test suites

268

});

269

270

const ruleTester = new RuleTester(); // Uses string-specific config

271

272

// Tests...

273

});

274

```

275

276

**Dynamic Configuration:**

277

278

```typescript

279

// Conditional configuration based on available packages

280

function setupRuleTester() {

281

const config: RuleTesterConfig = {

282

languageOptions: {

283

parserOptions: {

284

ecmaVersion: 2022,

285

sourceType: "module",

286

},

287

},

288

};

289

290

// Add TypeScript configuration if available

291

try {

292

require("typescript");

293

config.languageOptions!.parserOptions!.project = "./tsconfig.json";

294

config.dependencyConstraints = {

295

typescript: ">=4.0.0",

296

};

297

} catch {

298

// TypeScript not available, use JavaScript defaults

299

console.warn("TypeScript not found, using JavaScript-only configuration");

300

}

301

302

RuleTester.setDefaultConfig(config);

303

}

304

305

setupRuleTester();

306

```

307

308

### Configuration Merging Behavior

309

310

Understanding how configuration merging works with global defaults.

311

312

```typescript

313

// Global configuration

314

RuleTester.setDefaultConfig({

315

defaultFilenames: {

316

ts: "global.ts",

317

tsx: "global.tsx",

318

},

319

languageOptions: {

320

parserOptions: {

321

ecmaVersion: 2020,

322

sourceType: "module",

323

},

324

globals: {

325

globalVar: "readonly",

326

},

327

},

328

});

329

330

// Instance configuration merges deeply

331

const ruleTester = new RuleTester({

332

defaultFilenames: {

333

ts: "instance.ts", // Overrides global setting

334

// tsx inherits "global.tsx" from global config

335

},

336

languageOptions: {

337

parserOptions: {

338

// ecmaVersion: 2020 inherited from global

339

// sourceType: "module" inherited from global

340

project: "./tsconfig.json", // Added to global config

341

},

342

globals: {

343

// globalVar: "readonly" inherited from global

344

instanceVar: "readonly", // Added to global config

345

},

346

},

347

});

348

349

// Effective configuration:

350

// {

351

// defaultFilenames: { ts: "instance.ts", tsx: "global.tsx" },

352

// languageOptions: {

353

// parserOptions: {

354

// ecmaVersion: 2020,

355

// sourceType: "module",

356

// project: "./tsconfig.json"

357

// },

358

// globals: {

359

// globalVar: "readonly",

360

// instanceVar: "readonly"

361

// }

362

// }

363

// }

364

```