Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cover all tests #1011

Merged
merged 10 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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}
```
---
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;
```
---
Loading