From deb922a90f2bff13c3fad640c6de9b3c2bb33d34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 22 Apr 2024 13:12:43 +0200 Subject: [PATCH] fix: drop jest (1/4): remove jest references from tests --- eslint.config.mjs | 12 ---- src/__tests__/TransformableString.js | 27 ++++++++- src/__tests__/extract.js | 61 ++++++++++++++++++- src/__tests__/getFileMode.js | 25 +++++++- src/__tests__/plugin.js | 89 ++++++++++++++++------------ 5 files changed, 158 insertions(+), 56 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 0372ea4..6ae83be 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -23,16 +23,4 @@ export default [ }, }, }, - - { - files: ["**/__tests__/**/*.js"], - languageOptions: { - globals: { - expect: "readonly", - test: "readonly", - it: "readonly", - describe: "readonly", - }, - }, - }, ] diff --git a/src/__tests__/TransformableString.js b/src/__tests__/TransformableString.js index fcd3d9c..72554a4 100644 --- a/src/__tests__/TransformableString.js +++ b/src/__tests__/TransformableString.js @@ -1,7 +1,30 @@ -"use strict" - const TransformableString = require("../TransformableString") +const { it, describe } = require("node:test") +const assert = require("assert") +function expect(actual) { + return { + toBe(expected) { + assert.strictEqual(actual, expected) + }, + toEqual(expected) { + assert.deepStrictEqual(actual, expected) + }, + toMatch(expected) { + assert.match(actual, expected) + }, + toThrow(callback) { + let error = null + try { + callback() + } catch (e) { + error = e + } + assert.notStrictEqual(error, null) + }, + } +} + it("should be a function", () => { expect(typeof TransformableString).toBe("function") }) diff --git a/src/__tests__/extract.js b/src/__tests__/extract.js index ba6a644..6e9d39d 100644 --- a/src/__tests__/extract.js +++ b/src/__tests__/extract.js @@ -1,9 +1,38 @@ -/*eslint-env es6*/ /*eslint no-sparse-arrays: 0*/ -"use strict" const extract = require("../extract") +const { it, describe, context } = require("node:test") +const assert = require("assert") +function expect(actual) { + return { + toBe(expected) { + assert.strictEqual(actual, expected) + }, + toEqual(expected) { + assert.deepStrictEqual(actual, expected) + }, + toMatch(expected) { + assert.match(actual, expected) + }, + toThrow(callback) { + let error = null + try { + callback() + } catch (e) { + error = e + } + assert.notStrictEqual(error, null) + }, + toMatchSnapshot() { + console.log(context.name) + if (typeof actual !== "string") { + throw new Error("toMatchSnapshot() only works with strings") + } + }, + } +} + function dedent(str) { if (str[0] === "\n") str = str.slice(1) @@ -27,7 +56,7 @@ function test(params) { isJavaScriptMIMEType: params.isJavaScriptMIMEType, ignoreTagsWithoutType: params.ignoreTagsWithoutType, }) - expect(infos.code.map((code) => code.toString())).toMatchSnapshot() + expect(infos.code.map((code) => code.toString())).toEqual(params.expected) expect(infos.badIndentationLines).toEqual(params.badIndentationLines || []) } @@ -38,6 +67,7 @@ it("extract simple javascript", () => { other `, + expected: ["var foo = 1;"], }) }) @@ -50,6 +80,7 @@ it("extract indented javascript", () => { other `, + expected: ["var foo = 1;\n"], }) }) @@ -62,6 +93,7 @@ it("extract javascript with first line next to the script tag", () => { other `, + expected: ["var foo = 1;\nvar baz = 1;\n"], }) }) @@ -74,6 +106,7 @@ it("extract javascript with last line next to the script tag", () => { var baz = 1; other `, + expected: ["var foo = 1;\nvar baz = 1;"], }) }) @@ -89,6 +122,7 @@ it("extract multiple script tags", () => { var bar = 1; `, + expected: ["var foo = 1;\n", "var bar = 1;\n"], }) }) @@ -101,6 +135,7 @@ it("trim last line spaces", () => { other `, + expected: ["var foo = 1;\n"], }) }) @@ -114,6 +149,7 @@ it("trim last line spaces ignoring CDATA", () => { other `, xmlMode: true, + expected: ["\nvar foo = 1;\n"], }) }) @@ -124,6 +160,7 @@ it("extract script containing 'lower than' characters correctly (#1)", () => { if (a < b) { doit(); } `, + expected: ["if (a < b) { doit(); }\n"], }) }) @@ -132,6 +169,7 @@ it("extract empty script tag (#7)", () => { input: ` `, + expected: [""], }) }) @@ -150,6 +188,7 @@ for (const prefix of prefixes) { other `, + expected: ["var foo = 1;"], }) }) } @@ -165,6 +204,7 @@ it("collects bad indentations", () => { `, badIndentationLines: [3, 4], + expected: ["a;\na;\n a;\n"], }) }) @@ -184,6 +224,7 @@ describe("indent option", () => { spaces: " ", }, badIndentationLines: [3, 5], + expected: ["\n a;\na;\na;\n"], }) }) @@ -203,6 +244,7 @@ describe("indent option", () => { relative: true, }, badIndentationLines: [4, 5], + expected: ["a;\n a;\na;\n"], }) }) @@ -221,6 +263,7 @@ describe("indent option", () => { spaces: "\t", }, badIndentationLines: [3, 5], + expected: ["\n\t\ta;\na;\na;\n"], }) }) @@ -240,6 +283,7 @@ describe("indent option", () => { relative: true, }, badIndentationLines: [4, 5], + expected: ["a;\n\ta;\na;\n"], }) }) }) @@ -249,6 +293,7 @@ it("works with crlf new lines", () => { input: "

\r\n

\r\n\r\n", badIndentationLines: [5], + expected: ["foo;\r\nbar;\r\n baz;\r\n"], }) }) @@ -263,6 +308,7 @@ it("works with CDATA", () => { c; `, xmlMode: true, + expected: ["a;\n\nb;\n\nc;\n"], }) }) @@ -284,6 +330,7 @@ it("handles the isJavaScriptMIMEType option", () => { isJavaScriptMIMEType(type) { return type === "foo/bar" }, + expected: ["a\n", "b\n"], }) }) @@ -296,12 +343,14 @@ it("keeps empty lines after the last html tags", () => { `, + expected: ["a\n"], }) }) it("handles empty input", () => { test({ input: "", + expected: [], }) }) @@ -309,12 +358,14 @@ it("handles self closing script tags in xhtml mode", () => { test({ input: "a ', + expected: [], }) }) @@ -322,6 +373,7 @@ it("skips script without type attribute", () => { test({ input: "", ignoreTagsWithoutType: true, + expected: [], }) }) @@ -338,6 +390,7 @@ it("extract multiple tags types", () => { `, javaScriptTagNames: ["script", "customscript"], + expected: ["var foo = 1;\n", "var bar = 1;\n"], }) }) @@ -354,6 +407,7 @@ describe("disable comments", () => { var bar = 2; `, + expected: ["var bar = 2;\n"], }) }) @@ -369,6 +423,7 @@ describe("disable comments", () => { var bar = 2; `, + expected: ["var bar = 2;\n"], }) }) }) diff --git a/src/__tests__/getFileMode.js b/src/__tests__/getFileMode.js index 91bcae6..e239482 100644 --- a/src/__tests__/getFileMode.js +++ b/src/__tests__/getFileMode.js @@ -1,4 +1,27 @@ -"use strict" +const { test } = require("node:test") +const assert = require("assert") +function expect(actual) { + return { + toBe(expected) { + assert.strictEqual(actual, expected) + }, + toEqual(expected) { + assert.deepStrictEqual(actual, expected) + }, + toMatch(expected) { + assert.match(actual, expected) + }, + toThrow(callback) { + let error = null + try { + callback() + } catch (e) { + error = e + } + assert.notStrictEqual(error, null) + }, + } +} const getFileMode = require("../getFileMode") diff --git a/src/__tests__/plugin.js b/src/__tests__/plugin.js index eb37d2b..c0f5f03 100644 --- a/src/__tests__/plugin.js +++ b/src/__tests__/plugin.js @@ -3,9 +3,25 @@ const path = require("path") const eslint = require("eslint") const semver = require("semver") +const { it, describe } = require("node:test") +const assert = require("assert") const eslintVersion = require("eslint/package.json").version const eslintPluginHtml = require("..") +function expect(actual) { + return { + toBe(expected) { + assert.strictEqual(actual, expected) + }, + toEqual(expected) { + assert.deepStrictEqual(actual, expected) + }, + toMatch(expected) { + assert.match(actual, expected) + }, + } +} + function matchVersion(versionSpec) { return semver.satisfies(eslintVersion, versionSpec, { includePrerelease: true, @@ -502,8 +518,8 @@ describe("xml support", () => { }) it("should support self closing script tags", async () => { - const messages = execute("self-closing-tags.xhtml") - await expect(messages).resolves.toEqual([]) + const messages = await execute("self-closing-tags.xhtml") + expect(messages).toEqual([]) }) }) @@ -901,35 +917,33 @@ ifVersion(">= 5", describe, "compatibility with external HTML plugins", () => { // versions of ESLint, we need to ignore these suggestions. suggestions: "(ignored)", })) - ).toMatchInlineSnapshot(` -[ - { - "column": 1, - "endColumn": 13, - "endLine": 1, - "line": 1, - "message": "Missing \`alt\` attribute at \`\` tag", - "messageId": "missingAlt", - "nodeType": null, - "ruleId": "@html-eslint/require-img-alt", - "severity": 2, - "suggestions": "(ignored)", - }, - { - "column": 3, - "endColumn": 14, - "endLine": 3, - "line": 3, - "message": "Unexpected console statement.", - "messageId": "unexpected", - "nodeType": "MemberExpression", - "ruleId": "no-console", - "severity": 2, - "source": " console.log("toto")", - "suggestions": "(ignored)", - }, -] -`) + ).toEqual([ + { + column: 1, + endColumn: 13, + endLine: 1, + line: 1, + message: "Missing `alt` attribute at `` tag", + messageId: "missingAlt", + nodeType: null, + ruleId: "@html-eslint/require-img-alt", + severity: 2, + suggestions: "(ignored)", + }, + { + column: 3, + endColumn: 14, + endLine: 3, + line: 3, + message: "Unexpected console statement.", + messageId: "unexpected", + nodeType: "MemberExpression", + ruleId: "no-console", + severity: 2, + source: ' console.log("toto")', + suggestions: "(ignored)", + }, + ]) }) it("fix", async () => { @@ -941,12 +955,11 @@ ifVersion(">= 5", describe, "compatibility with external HTML plugins", () => { }, fix: true, }) - expect(result.output).toMatchInlineSnapshot(` - " - - " - `) + expect(result.output).toEqual(`\ + + +`) }) })