Skip to content

Commit

Permalink
chore: cover all tests (#1011)
Browse files Browse the repository at this point in the history
* chore: use Biome instead of prettier+eslint

* move biome to dev deps

* chore: cover all tests

* fix rebase changes

* chore: document utility

* remove want from printer tests

* do the same for json printer

* better explanation

* fix mergef
  • Loading branch information
ematipico authored Jul 30, 2024
1 parent f31a4ea commit 0ebfc96
Show file tree
Hide file tree
Showing 329 changed files with 600 additions and 1,686 deletions.
41 changes: 35 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ go test -v ./internal/printer
```
### Run a specific test case

Many of your test cases are designed like this:
Many of our test cases are designed like this:

```go
func TestPrintToJSON(t *testing.T) {
Expand All @@ -72,11 +72,7 @@ go test -v ./internal/... -run TestPrintToJSON/basic
go test -v ./internal/... -run TestPrintToJSON/Comment preserves whitespace
```

### Adding new tests

Adding tests for the tokenizer, scanner, and printer can be found in `internal/token_test.go`, `internal/js_scanner_test.go`, and `internal/printer/printer_test.go`, respectively.

### Snapshot testing
#### Snapshot testing

We use [go-snaps](https://github.com/gkampitakis/go-snaps) for snapshot testing. Visit their repository for more details on how to use it

Expand All @@ -94,6 +90,39 @@ Instead, if there are some **obsolete snapshots**, you can `UPDATE_SNAPS=clean`:
UPDATE_SNAPS=clean go test -v ./internal/...
```


### Adding new test cases

The printer tests emit only snapshots. Go to `printer_test.go` and add a new test case:

```go
{
name: "New name for this test"
code: "<div></div>"
}
```

Then run the below command, and a new snapshot named `new_name_for_this_test.snap` should appear in the snapshot folder.

```shell
go test -v ./internal/printer/printer_test.go
```

Other tests, like tokenizer and scanner be found in `internal/token_test.go`, `internal/js_scanner_test.go` and respectively.

Those tests don't emit any snapshot, and you'll have to add a `want` field:

```go
{
name: "New name for this test"
code: "<div></div>",
want: want{
code: "<div></div>"
}
}
```


[homebrew]: https://brew.sh/
[go]: https://golang.org/
[go-vscode]: https://marketplace.visualstudio.com/items?itemName=golang.go
Expand Down
1 change: 1 addition & 0 deletions internal/js_scanner/js_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ func TestGetObjectKeys(t *testing.T) {
if diff := test_utils.ANSIDiff(string(want), string(got)); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}

})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

[TestPrinterCSS/scopedStyleStrategy:_'attribute' - 1]
## Input

```
<style>
.title {
font-family: fantasy;
font-size: 28px;
}
.body {
font-size: 1em;
}
</style>
<h1 class="title">Page Title</h1>
<p class="body">I’m a page</p>
```

## Output

```css
.title[data-astro-cid-dpohflym]{font-family:fantasy;font-size:28px}.body[data-astro-cid-dpohflym]{font-size:1em}
```
---
26 changes: 26 additions & 0 deletions internal/printer/__printer_css__/scopedStyleStrategy___class_.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

[TestPrinterCSS/scopedStyleStrategy:_'class' - 1]
## Input

```
<style>
.title {
font-family: fantasy;
font-size: 28px;
}
.body {
font-size: 1em;
}
</style>
<h1 class="title">Page Title</h1>
<p class="body">I’m a page</p>
```

## Output

```css
.title.astro-dpohflym{font-family:fantasy;font-size:28px}.body.astro-dpohflym{font-size:1em}
```
---
26 changes: 26 additions & 0 deletions internal/printer/__printer_css__/styles__no_frontmatter_.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

[TestPrinterCSS/styles_(no_frontmatter) - 1]
## Input

```
<style>
.title {
font-family: fantasy;
font-size: 28px;
}
.body {
font-size: 1em;
}
</style>
<h1 class="title">Page Title</h1>
<p class="body">I’m a page</p>
```

## Output

```css
.title:where(.astro-dpohflym){font-family:fantasy;font-size:28px}.body:where(.astro-dpohflym){font-size:1em}
```
---
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions internal/printer/__printer_js__/scriptinline.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

[TestPrinter/scriptinline - 1]
## Input

```
<main><script is:inline type="module">console.log("Hello");</script>
```

## Output

```js
import {
Fragment,
render as $$render,
createAstro as $$createAstro,
createComponent as $$createComponent,
renderComponent as $$renderComponent,
renderHead as $$renderHead,
maybeRenderHead as $$maybeRenderHead,
unescapeHTML as $$unescapeHTML,
renderSlot as $$renderSlot,
mergeSlots as $$mergeSlots,
addAttribute as $$addAttribute,
spreadAttributes as $$spreadAttributes,
defineStyleVars as $$defineStyleVars,
defineScriptVars as $$defineScriptVars,
renderTransition as $$renderTransition,
createTransitionScope as $$createTransitionScope,
renderScript as $$renderScript,
createMetadata as $$createMetadata
} from "http://localhost:3000/";

export const $$metadata = $$createMetadata(import.meta.url, { modules: [], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });

const $$Component = $$createComponent(($$result, $$props, $$slots) => {

return $$render`${$$maybeRenderHead($$result)}<main><script type="module">console.log("Hello");</script></main>`;
}, undefined, undefined);
export default $$Component;
```
---
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0ebfc96

Please sign in to comment.