Skip to content

Commit

Permalink
build: add vitest and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j4w8n committed Oct 7, 2024
1 parent 8e58d33 commit 61d6760
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 84 deletions.
80 changes: 0 additions & 80 deletions index.js

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "HTML formatter yo!. Prettify, minify, and more!",
"scripts": {
"check": "tsc",
"test": "bun index.js"
"test": "vitest"
},
"exports": {
".": {
Expand All @@ -18,15 +18,16 @@
"types"
],
"devDependencies": {
"typescript": "^5.0.0"
"typescript": "^5.0.0",
"vitest": "^2.1.2"
},
"types": "types/index.d.ts",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/j4w8n/htmlfy.git"
},
"keywords": [ "HTML", "format", "formatter", "prettier", "minifier", "prettify", "minify" ],
"keywords": ["HTML", "format", "formatter", "prettier", "minifier", "prettify", "minify"],
"license": "MIT",
"bugs": {
"url": "https://github.com/j4w8n/htmlfy/issues"
Expand Down
165 changes: 165 additions & 0 deletions src/tests/all.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { closify } from '../closify.js'
import { entify } from '../entify.js'
import { minify } from '../minify.js'
import { prettify } from '../prettify.js'
import { expect, test } from 'vitest'

const ugly_html = `<form id="3" > <!--
This is a comment. --><!-- This is a second comment. --><label for="email-0">
What's your email?</label><input id="email-0" type="email"
title="We need your email for verification." name="email" required><!-- This is another comment. -->
<label for="1">What fruits do you like?</label><fieldset id="1">
<input id="fruits-1-0" type="checkbox" name="fruits" value="apples">
<label for="fruits-1-0" >Apples< /label><br><div><!-- This is an embedded comment. --></div>
<input id="fruits-1-1" type="checkbox" name="fruits" value="grapes">
<label for="fruits-1-1">Grapes</label><br></fieldset>
<textarea >
Did you know that 3 > 2?
This is another paragraph.
</textarea><textarea class=" more stuff "> </textarea>
<br>
</form>`

const entify_html = `<textarea >
Did you know that 3 > 2?
This is another paragraph.
</textarea><textarea class=" more stuff "> </textarea>`

const pretty_html = `<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. -->
<label for="email-0">What's your email?</label>
<input id="email-0" type="email" title="We need your email for verification." name="email" required />
<!-- This is another comment. -->
<label for="1">What fruits do you like?</label>
<fieldset id="1">
<input id="fruits-1-0" type="checkbox" name="fruits" value="apples" />
<label for="fruits-1-0">Apples</label>
<br />
<div>
<!-- This is an embedded comment. -->
</div>
<input id="fruits-1-1" type="checkbox" name="fruits" value="grapes" />
<label for="fruits-1-1">Grapes</label>
<br />
</fieldset>
<textarea>Did you know that 3 &gt; 2?&#13;&#13;This is another paragraph.</textarea>
<textarea class="more stuff"></textarea>
<br>
</form>`

const closify_html = `<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. --><br><input><br><input></form>`

const config_html = `<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. --><div><br /><input><br /><input><div></form>`


test('Prettify', () => {
expect(prettify(ugly_html)).toBe(
`<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. -->
<label for="email-0">What's your email?</label>
<input id="email-0" type="email" title="We need your email for verification." name="email" required />
<!-- This is another comment. -->
<label for="1">What fruits do you like?</label>
<fieldset id="1">
<input id="fruits-1-0" type="checkbox" name="fruits" value="apples" />
<label for="fruits-1-0">Apples</label>
<br />
<div>
<!-- This is an embedded comment. -->
</div>
<input id="fruits-1-1" type="checkbox" name="fruits" value="grapes" />
<label for="fruits-1-1">Grapes</label>
<br />
</fieldset>
<textarea>Did you know that 3 &gt; 2?&#13;&#13;This is another paragraph.</textarea>
<textarea class="more stuff"></textarea>
<br />
</form>`
)
})

test('Prettify with HTML check', () => {
expect(prettify('No HTML')).toBe('No HTML')
})

test('Minify', () => {
expect(minify(pretty_html)).toBe(
`<form id="3"><!-- This is a comment. --><!-- This is a second comment. --><label for="email-0">What's your email?</label><input id="email-0" type="email" title="We need your email for verification." name="email" required /><!-- This is another comment. --><label for="1">What fruits do you like?</label><fieldset id="1"><input id="fruits-1-0" type="checkbox" name="fruits" value="apples" /><label for="fruits-1-0">Apples</label><br /><div><!-- This is an embedded comment. --></div><input id="fruits-1-1" type="checkbox" name="fruits" value="grapes" /><label for="fruits-1-1">Grapes</label><br /></fieldset><textarea>Did you know that 3 &gt; 2?&#13;&#13;This is another paragraph.</textarea><textarea class="more stuff"></textarea><br></form>`
)
})

test('Minify with HTML check', () => {
expect(minify('No HTML', true)).toBe('No HTML')
})

test('Entify', () => {
expect(entify(entify_html)).toBe(
`<textarea >Did you know that 3 &gt; 2?&#13;&#13;This is another paragraph.</textarea><textarea class=" more stuff "></textarea>`
)
})

test('Entify with plain text', () => {
expect(entify('Plain text')).toBe('Plain text')
})

test('Entify with minify', () => {
expect(entify(entify_html, true)).toBe(
`<textarea>Did you know that 3 &gt; 2?&#13;&#13;This is another paragraph.</textarea><textarea class="more stuff"></textarea>`
)
})

test('Closify', () => {
expect(closify(closify_html)).toBe(
`<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. --><br /><input /><br /><input /></form>`
)
})

test('Closify with HTML check', () => {
expect(closify('No HTML', true)).toBe('No HTML')
})

test('Strict config', () => {
expect(prettify(config_html, { strict: true })).toBe(
`<form id="3">
<div>
<br>
<input>
<br>
<input>
</div>
</form>`
)
})

test('Tab size config', () => {
expect(prettify(config_html, { tab_size: 4 })).toBe(
`<form id="3">
<!-- This is a comment. -->
<!-- This is a second comment. -->
<div>
<br />
<input />
<br />
<input />
</div>
</form>`
)
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"strict": true,
"target": "esnext"
},
"include": [ "src/**/*", "index.js", "types/index.d.ts" ]
"include": [ "src/**/*", "src/tests/all.spec.js", "types/index.d.ts" ]
}

0 comments on commit 61d6760

Please sign in to comment.