@flint.fyi/rule-tester 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -12,17 +12,17 @@ interface TestCase<Options extends object | undefined = object | undefined> {
12
12
  files?: Record<string, string> | undefined;
13
13
  name?: string | undefined;
14
14
  /**
15
- * Run only this test case. Useful for debugging.
16
- *
17
- * Do not commit code with this flag set.
18
- */
15
+ * Run only this test case. Useful for debugging.
16
+ *
17
+ * Do not commit code with this flag set.
18
+ */
19
19
  only?: boolean;
20
20
  options?: Options | undefined;
21
21
  /**
22
- * Skip running this test case. Useful for work-in-progress tests.
23
- *
24
- * Do not commit code with this flag set.
25
- */
22
+ * Skip running this test case. Useful for work-in-progress tests.
23
+ *
24
+ * Do not commit code with this flag set.
25
+ */
26
26
  skip?: boolean;
27
27
  }
28
28
  type TestSuggestion = TestSuggestionForFile | TestSuggestionForFiles;
@@ -47,6 +47,7 @@ interface RuleTesterDefaults {
47
47
  files?: Record<string, string>;
48
48
  }
49
49
  interface RuleTesterOptions {
50
+ assertNoLanguageReports?: boolean;
50
51
  defaults?: RuleTesterDefaults;
51
52
  describe?: TesterSetupDescribe;
52
53
  diskBackedFSRoot?: string;
@@ -64,6 +65,7 @@ type TesterSetupIt = (description: string, setup: () => Promise<void>) => void;
64
65
  declare class RuleTester {
65
66
  #private;
66
67
  constructor({
68
+ assertNoLanguageReports,
67
69
  defaults,
68
70
  describe,
69
71
  diskBackedFSRoot,
package/dist/index.mjs CHANGED
@@ -123,7 +123,7 @@ function resolveReportedSuggestionForFiles(suggestionReported, testCaseNormalize
123
123
  }
124
124
  //#endregion
125
125
  //#region src/runTestCaseRule.ts
126
- async function runTestCaseRule(fileFactories, linterHost, { options, rule }, { code, fileName, files }) {
126
+ async function runTestCaseRule(fileFactories, linterHost, { options, rule }, { code, fileName, files }, { collectLanguageReports = false } = {}) {
127
127
  const filePathAbsolute = normalizePath(path.resolve(linterHost.getCurrentDirectory(), fileName));
128
128
  const caseSensitive = linterHost.isCaseSensitiveFS();
129
129
  const targetKey = pathKey(filePathAbsolute, caseSensitive);
@@ -152,7 +152,10 @@ async function runTestCaseRule(fileFactories, linterHost, { options, rule }, { c
152
152
  rule.language.runFileVisitors(file, options, ruleRuntime);
153
153
  await ruleRuntime.teardown?.();
154
154
  }
155
- return reports.toSorted((a, b) => a.range.begin.raw - b.range.begin.raw || a.range.end.raw - b.range.end.raw);
155
+ return {
156
+ languageReports: collectLanguageReports ? rule.language.getLanguageReports?.(file) ?? [] : [],
157
+ reports: reports.toSorted((a, b) => a.range.begin.raw - b.range.begin.raw || a.range.end.raw - b.range.end.raw)
158
+ };
156
159
  }
157
160
  //#endregion
158
161
  //#region src/RuleTester.ts
@@ -160,7 +163,7 @@ var RuleTester = class {
160
163
  #fileFactories;
161
164
  #linterHost;
162
165
  #testerOptions;
163
- constructor({ defaults = {}, describe, diskBackedFSRoot, it, only, scope = globalThis, skip } = {}) {
166
+ constructor({ assertNoLanguageReports = true, defaults = {}, describe, diskBackedFSRoot, it, only, scope = globalThis, skip } = {}) {
164
167
  let baseHost = diskBackedFSRoot != null ? createEphemeralLinterHost(createDiskBackedLinterHost(path.resolve(process.cwd(), diskBackedFSRoot, "_flint-rule-tester-virtual"))) : void 0;
165
168
  const { files: defaultFiles = {} } = defaults;
166
169
  if (Object.keys(defaultFiles).length) {
@@ -179,6 +182,7 @@ var RuleTester = class {
179
182
  if (!skip) throw new TypeError("RuleTester needs a `skip` function");
180
183
  if (!only) throw new TypeError("RuleTester needs a `only` function");
181
184
  this.#testerOptions = {
185
+ assertNoLanguageReports,
182
186
  defaults,
183
187
  describe: defaultTo(describe, scope, "describe"),
184
188
  it,
@@ -200,10 +204,11 @@ var RuleTester = class {
200
204
  #itInvalidCase(rule, testCase) {
201
205
  const testCaseNormalized = normalizeTestCase(testCase, this.#testerOptions.defaults.fileName);
202
206
  this.#itTestCase(testCaseNormalized, async () => {
203
- const reports = await runTestCaseRule(this.#fileFactories, this.#linterHost, {
207
+ const { languageReports, reports } = await runTestCaseRule(this.#fileFactories, this.#linterHost, {
204
208
  options: parseOptions(rule.options, testCase.options),
205
209
  rule
206
- }, testCaseNormalized);
210
+ }, testCaseNormalized, { collectLanguageReports: this.#testerOptions.assertNoLanguageReports });
211
+ assertNoLanguageReports(languageReports);
207
212
  const actualSnapshot = createReportSnapshot(testCase.code, reports);
208
213
  assert.equal(actualSnapshot, testCase.snapshot);
209
214
  const actualOutput = createOutput(reports, testCaseNormalized);
@@ -228,14 +233,18 @@ var RuleTester = class {
228
233
  const testCase = typeof testCaseRaw === "string" ? { code: testCaseRaw } : testCaseRaw;
229
234
  const testCaseNormalized = normalizeTestCase(testCase, this.#testerOptions.defaults.fileName);
230
235
  this.#itTestCase(testCaseNormalized, async () => {
231
- const reports = await runTestCaseRule(this.#fileFactories, this.#linterHost, {
236
+ const { languageReports, reports } = await runTestCaseRule(this.#fileFactories, this.#linterHost, {
232
237
  options: parseOptions(rule.options, testCase.options),
233
238
  rule
234
- }, testCaseNormalized);
239
+ }, testCaseNormalized, { collectLanguageReports: this.#testerOptions.assertNoLanguageReports });
240
+ assertNoLanguageReports(languageReports);
235
241
  if (reports.length) assert.deepStrictEqual(createReportSnapshot(testCaseNormalized.code, reports), testCaseNormalized.code);
236
242
  });
237
243
  }
238
244
  };
245
+ function assertNoLanguageReports(languageReports) {
246
+ if (languageReports.length) assert.fail([`Expected no language reports, but found ${languageReports.length}:`, ...languageReports.map((languageReport) => languageReport.text)].join("\n\n"));
247
+ }
239
248
  function defaultTo(provided, scope, scopeKey) {
240
249
  if (provided) return provided;
241
250
  if (scopeKey in scope && typeof scope[scopeKey] === "function") return scope[scopeKey];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flint.fyi/rule-tester",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "[Experimental] Rule unit tests for Flint.",
5
5
  "homepage": "https://flint.fyi",
6
6
  "repository": {