Skip to content

Commit

Permalink
test: use snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Dec 5, 2023
1 parent 03b3ab4 commit a5baf1f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
120 changes: 120 additions & 0 deletions test/snapshots/test.mjs.md
Original file line number Diff line number Diff line change
@@ -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: `␊
<script type="module">␊
'use strict';␊
</script>␊
`,
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: `␊
<script>␊
alert();␊
</script>␊
`,
suppressedMessages: [],
usedDeprecatedRules: [],
warningCount: 1,
}

## fix

> Snapshot 1
`␊
<script>␊
'use strict';␊
alert();␊
</script>␊
abc␊
`

> Snapshot 2
[]
Binary file added test/snapshots/test.mjs.snap
Binary file not shown.
41 changes: 6 additions & 35 deletions test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -81,15 +61,6 @@ test('fix', (t) => {
abc
`;

const expected = html`
<script>
'use strict';
alert();
</script>
abc
`;

const messages = linter.verifyAndFix(
code,
{
Expand All @@ -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);
});

0 comments on commit a5baf1f

Please sign in to comment.