Skip to content

Commit

Permalink
Convert tests to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Jun 24, 2024
1 parent b2c8574 commit c0c5040
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
File renamed without changes.
13 changes: 7 additions & 6 deletions tests/format.test.js → tests/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, test } from 'vitest'
import type { TestEntry } from './utils.js'
import { format, no, t, yes } from './utils.js'

let html = [
let html: TestEntry[] = [
t`<div class="${yes}"></div>`,
t`<!-- <div class="${no}"></div> -->`,
t`<div class="${no} {{ 'p-0 sm:p-0 m-0' }}"></div>`,
Expand All @@ -26,7 +27,7 @@ let html = [
],
]

let css = [
let css: TestEntry[] = [
t`@apply ${yes};`,
t`/* @apply ${no}; */`,
t`@not-apply ${no};`,
Expand All @@ -37,7 +38,7 @@ let css = [
],
]

let javascript = [
let javascript: TestEntry[] = [
t`;<div class="${yes}" />`,
t`;<div ns:class="${no}" />`,
t`/* <div class="${no}" /> */`,
Expand Down Expand Up @@ -112,7 +113,7 @@ javascript = javascript.concat(
]),
)

let vue = [
let vue: TestEntry[] = [
...html,
t`<div :class="'${yes}'"></div>`,
t`<!-- <div :class="'${no}'"></div> -->`,
Expand Down Expand Up @@ -158,7 +159,7 @@ let vue = [
],
]

let glimmer = [
let glimmer: TestEntry[] = [
t`<div class='${yes}'></div>`,
t`<!-- <div class='${no}'></div> -->`,
t`<div class='${yes} {{"${yes}"}}'></div>`,
Expand Down Expand Up @@ -200,7 +201,7 @@ let glimmer = [
],
]

let tests = {
let tests: Record<string, TestEntry[]> = {
html,
glimmer,
lwc: html,
Expand Down
11 changes: 10 additions & 1 deletion tests/plugins.test.js → tests/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { createRequire } from 'node:module'
import { test } from 'vitest'
import type { TestEntry } from './utils.js'
import { format, no, pluginPath, t, yes } from './utils.js'

const require = createRequire(import.meta.url)

let tests = [
interface PluginTest {
versions: number[]
plugins: string[]
options?: Record<string, any>
tests: Record<string, TestEntry[]>
}

let tests: PluginTest[] = [
{
versions: [2, 3],
plugins: ['@trivago/prettier-plugin-sort-imports'],
Expand Down Expand Up @@ -479,6 +487,7 @@ for (const group of tests) {
// Hide logs from Pug's prettier plugin
if (parser === 'pug') {
let pug = await import('@prettier/plugin-pug')
// @ts-ignore
pug.logger.level = 'off'
}

Expand Down
16 changes: 14 additions & 2 deletions tests/utils.js → tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@ let testClassNameSorted = 'p-0 sm:p-0'
export let yes = '__YES__'
export let no = '__NO__'

export function t(strings, ...values) {
export type TestEntry = [
input: string,
output: string,
options?: {
tailwindPreserveWhitespace?: boolean
tailwindPreserveDuplicates?: boolean
},
]

export function t(
strings: TemplateStringsArray,
...values: string[]
): TestEntry {
let input = ''
strings.forEach((string, i) => {
input += string + (values[i] ? testClassName : '')
Expand All @@ -30,7 +42,7 @@ export function t(strings, ...values) {

export let pluginPath = path.resolve(__dirname, '../dist/index.mjs')

export async function format(str, options = {}) {
export async function format(str: string, options: prettier.Options = {}) {
let result = await prettier.format(str, {
pluginSearchDirs: [__dirname], // disable plugin autoload
semi: false,
Expand Down

0 comments on commit c0c5040

Please sign in to comment.