From a5baf1f34ebb1d152645e3478c0f4b4092ca369a Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 5 Dec 2023 11:46:22 +0800 Subject: [PATCH] test: use snapshots --- package.json | 1 + test/snapshots/test.mjs.md | 120 +++++++++++++++++++++++++++++++++++ test/snapshots/test.mjs.snap | Bin 0 -> 976 bytes test/test.mjs | 41 ++---------- 4 files changed, 127 insertions(+), 35 deletions(-) create mode 100644 test/snapshots/test.mjs.md create mode 100644 test/snapshots/test.mjs.snap diff --git a/package.json b/package.json index 3bbd8ce..3aa6ecd 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "lint:staged": "nice-move lint staged", "prepare": "git config core.hooksPath .githooks", "prepublishOnly": "npm run lint:staged && npm test", + "snapshot": "ava --fail-fast -u -w", "test": "ava --fail-fast" }, "dependencies": { diff --git a/test/snapshots/test.mjs.md b/test/snapshots/test.mjs.md new file mode 100644 index 0000000..ef05d50 --- /dev/null +++ b/test/snapshots/test.mjs.md @@ -0,0 +1,120 @@ +# Snapshot report for `test/test.mjs` + +The actual snapshot is saved in `test.mjs.snap`. + +Generated by [AVA](https://avajs.dev). + +## preprocess + +> Snapshot 1 + + { + filename: 'fake.htm/0/html-script-module.js', + text: `␊ + ␊ + 'use strict';␊ + `, + } + +> Snapshot 2 + + { + filename: 'fake.htm/1/html-script-legacy.js', + text: `␊ + ␊ + ␊ + ␊ + ␊ + alert();␊ + `, + } + +## module + +> Snapshot 1 + + { + errorCount: 0, + fatalErrorCount: 0, + filePath: 'D:\\open\\eslint-plugin-html\\fake.html', + fixableErrorCount: 0, + fixableWarningCount: 1, + messages: [ + { + column: 7, + endColumn: 20, + endLine: 3, + fix: { + range: [ + 34, + 47, + ], + text: '', + }, + line: 3, + message: '\'use strict\' is unnecessary inside of modules.', + messageId: 'module', + nodeType: 'ExpressionStatement', + ruleId: 'strict', + severity: 1, + }, + ], + source: `␊ + ␊ + `, + suppressedMessages: [], + usedDeprecatedRules: [], + warningCount: 1, + } + +## legacy + +> Snapshot 1 + + { + errorCount: 0, + fatalErrorCount: 0, + filePath: 'D:\\open\\eslint-plugin-html\\fake.htm', + fixableErrorCount: 0, + fixableWarningCount: 0, + messages: [ + { + column: 7, + endColumn: 15, + endLine: 3, + line: 3, + message: 'Use the global form of \'use strict\'.', + messageId: 'global', + nodeType: 'Program', + ruleId: 'strict', + severity: 1, + }, + ], + source: `␊ + ␊ + `, + suppressedMessages: [], + usedDeprecatedRules: [], + warningCount: 1, + } + +## fix + +> Snapshot 1 + + `␊ + ␊ + abc␊ + ` + +> Snapshot 2 + + [] diff --git a/test/snapshots/test.mjs.snap b/test/snapshots/test.mjs.snap new file mode 100644 index 0000000000000000000000000000000000000000..71cf0a2fff8e5e0d0f95bec495e32f0d7fae57ff GIT binary patch literal 976 zcmV;>126nRRzVx(%?9E9LcESoY?qQQh-Ad7ThUX}(= z55m8l69%`+GFNg;*QNstLJ1I;bV&v`=hFI)Ki|~v3aSgqEkpKSuFd%w{rvYCooUF( zrbS{@A~Kqb&S;mpbdwumQ!Y=Q@^bfEUGS>A)#j&OQDzbyp{lgqh3m9ml^1{wKs!e0 z0^kndHQ+N~6Gv7ptt6eMoI8BfahaET94QW&0_?{gkMOD$e~QTYV4`pA(yUXaY?cb! zVlr2@-J->E)jrGyeX*4w*@Cr1@-~fHtjqU%NI0`tG31Fc!bdPZ5ksiHvl5&mQlx6P zlfrSiQ76mQT3d!ais-UZrdN8r=Jbs0$l&2Y&%f$XTljit;iJMW!$}r`nz!m(0H3*# zPZxwV$KbS6gGg%Lh2_+Jwm)u~Webiyfk4YKzvGE(oRbQU1*1$R15ORLUgAGUm zOSECU8Ve}+1x8;1n}FjQLPLPtfCa!Cz&fBCIyDqmD5ynJFljipTVj50DaG1KnK>GE z=vEw3;BeDo)HiFjBG1IPimU-$)T3@UJkS`=CrofZ5bnEf3EgFkR>hg{if%DsnN)WQ zxldd8%X1Mz$D-~;5}gD|B=hyJ;>#ut0k&+wq^ffnG1{M zNVE6bHf7B1K^Z3wOBpEyTHJ>+#&@x8W2>i(ZlH|mw=ntuSO>r+kPa9IECXHvJ^_Ad zk*e7GcdD3)RK)`eVBZ7KKRHkJqV42~trr|#sw!Ss7xy50-qTUCmzv^EkrS2d2a`Hq yHVilc+rOlz>ynW`S$v}zr5oS)Q9K7z-bmNKYkRjOsew>9M5sUD4UClg3jhFI%F$o| literal 0 HcmV?d00001 diff --git a/test/test.mjs b/test/test.mjs index 843c3b9..8f284d0 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -20,14 +20,8 @@ test('preprocess', (t) => { const [first, second] = processor.preprocess(code, 'fake.htm'); - t.deepEqual(first, { - filename: 'fake.htm/0/html-script-module.js', - text: "\n \n 'use strict';\n ", - }); - t.deepEqual(second, { - filename: 'fake.htm/1/html-script-legacy.js', - text: '\n \n \n \n \n alert();\n ', - }); + t.snapshot(first); + t.snapshot(second); }); test('module', async (t) => { @@ -41,14 +35,7 @@ test('module', async (t) => { filePath: 'fake.html', }); - t.true( - result.messages.some( - (message) => - message.ruleId === 'strict' && - message.column === 7 && - message.severity === 1, - ), - ); + t.snapshot(result); }); test('legacy', async (t) => { @@ -62,14 +49,7 @@ test('legacy', async (t) => { filePath: 'fake.htm', }); - t.true( - result.messages.some( - (message) => - message.ruleId === 'strict' && - message.column === 7 && - message.severity === 1, - ), - ); + t.snapshot(result); }); test('fix', (t) => { @@ -81,15 +61,6 @@ test('fix', (t) => { abc `; - const expected = html` - - abc - `; - const messages = linter.verifyAndFix( code, { @@ -108,6 +79,6 @@ test('fix', (t) => { ); t.true(messages.fixed); - t.is(messages.output, expected); - t.deepEqual(messages.messages, []); + t.snapshot(messages.output); + t.snapshot(messages.messages); });