Skip to content

Commit

Permalink
chore: reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 20, 2025
1 parent 4beedba commit 7ac62e1
Show file tree
Hide file tree
Showing 26 changed files with 108 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
import js from '@shikijs/langs/javascript'
import nord from '@shikijs/themes/nord'
import { describe, expect, it } from 'vitest'
import { createJavaScriptRegexEngine } from '../../engine-javascript/src/engine-compile'
import { createHighlighterCoreSync } from '../src/core'
import js from '../src/langs/javascript.mjs'
import nord from '../src/themes/nord.mjs'
import { createHighlighterCoreSync } from '../src'

describe('should', () => {
const engine = createJavaScriptRegexEngine()
Expand All @@ -24,13 +24,13 @@ describe('should', () => {
langs: [
js,
// Load the grammar upfront (await outside of the function)
await import('../src/langs/c.mjs').then(r => r.default),
await import('@shikijs/langs/c').then(r => r.default),
],
engine,
})

shiki.loadLanguageSync(await import('../src/langs/python.mjs').then(m => m.default))
shiki.loadThemeSync(await import('../dist/themes/vitesse-light.mjs').then(m => m.default))
shiki.loadLanguageSync(await import('@shikijs/langs/python').then(m => m.default))
shiki.loadThemeSync(await import('@shikijs/themes/vitesse-light').then(m => m.default))

expect(shiki.getLoadedLanguages())
.toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createOnigurumaEngine } from '@shikijs/engine-oniguruma'
import { describe, expect, it } from 'vitest'

// eslint-disable-next-line antfu/no-import-dist
import { wasmBinary } from '../../engine-oniguruma/dist/wasm-inlined.mjs'
import { createHighlighterCore } from '../src/core'
import js from '../src/langs/javascript.mjs'
import ts from '../src/langs/typescript.mjs'
import mtp from '../src/themes/material-theme-palenight.mjs'
import nord from '../src/themes/nord.mjs'
import { wasmBinary } from '@shikijs/engine-oniguruma/wasm-inlined'

import js from '@shikijs/langs/javascript'
import ts from '@shikijs/langs/typescript'
import mtp from '@shikijs/themes/material-theme-palenight'
import nord from '@shikijs/themes/nord'
import { describe, expect, it } from 'vitest'
import { createHighlighterCore } from '../src'

describe('should', () => {
it('works', async () => {
Expand All @@ -28,16 +28,16 @@ describe('should', () => {
themes: [nord],
langs: [
js,
import('../src/langs/c.mjs'),
import('@shikijs/langs/c'),
],
engine: createOnigurumaEngine({
// https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
instantiator: obj => WebAssembly.instantiate(wasmBinary, obj).then(r => r.instance.exports),
}),
})

await shiki.loadLanguage(() => import('../src/langs/python.mjs'))
await shiki.loadTheme(() => import('../dist/themes/vitesse-light.mjs').then(m => m.default))
await shiki.loadLanguage(() => import('@shikijs/langs/python'))
await shiki.loadTheme(() => import('@shikijs/themes/vitesse-light').then(m => m.default))

expect(shiki.getLoadedLanguages())
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('should', () => {
using shiki = await createHighlighterCore({
themes: [nord],
langs: [
import('../src/langs/cpp.mjs'),
import('@shikijs/langs/cpp'),
],
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
import { codeToHtml } from 'shiki/bundle/full'
import { describe, expect, it } from 'vitest'
import { codeToHtml, createCssVariablesTheme, createHighlighter } from '../src'
import { createCssVariablesTheme, createHighlighterCore } from '../src'

describe('css-variables', () => {
it('theme auto-patch', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighterCore({
themes: [
Object.freeze({
name: 'my-css-variables',
Expand All @@ -27,7 +29,10 @@ describe('css-variables', () => {
],
}),
],
langs: ['javascript'],
langs: [
import('@shikijs/langs/js'),
],
engine: createJavaScriptRegexEngine(),
})

const theme = shiki.getTheme('my-css-variables')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { codeToHtml } from 'shiki/bundle/full'
import { describe, expect, it, vi } from 'vitest'
import { codeToHtml } from '../src'

const style = `
<style>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable style/no-tabs */
import { toHtml } from 'hast-util-to-html'
import { codeToHtml, createHighlighter } from 'shiki'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { codeToHtml, createHighlighter } from '../src'

afterEach(() => {
vi.restoreAllMocks()
})

describe('should', () => {
it('works', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
Expand All @@ -24,7 +24,7 @@ describe('should', () => {
})

it('structure inline', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import angularHtml from '@shikijs/langs/angular-html'
import angularTs from '@shikijs/langs/angular-ts'
import html from '@shikijs/langs/html'
import ts from '@shikijs/langs/typescript'
import vue from '@shikijs/langs/vue'
import vl from '@shikijs/themes/vitesse-light'
import { codeToHtml, createHighlighterCore } from 'shiki'
import { expect, it } from 'vitest'
import angularHtml from '../src/langs/angular-html.mjs'
import angularTs from '../src/langs/angular-ts.mjs'
import html from '../src/langs/html.mjs'
import ts from '../src/langs/typescript.mjs'
import vue from '../src/langs/vue.mjs'
import vl from '../src/themes/vitesse-light.mjs'

// Basically we need to make sure that the syntax inside `v-if` and `{{}}` is highlighted correctly.
// This is done by a `vue-injections` patch that injects extra grammar into HTML.
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions packages/core/test/tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createJavaScriptRegexEngine } from 'shiki'
import { expect, it } from 'vitest'
import { codeToTokensBase, createShikiInternal } from '../src'

it('includeExplanation', async () => {
using engine = await createShikiInternal({
themes: [
import('@shikijs/themes/vitesse-dark'),
],
langs: [
import('@shikijs/langs/javascript'),
],
engine: createJavaScriptRegexEngine(),
})

const code = 'console.log("hello")'
const caseFalse = await codeToTokensBase(engine, code, { lang: 'js', theme: 'vitesse-dark', includeExplanation: false })
const caseTrue = await codeToTokensBase(engine, code, { lang: 'js', theme: 'vitesse-dark', includeExplanation: true })
const caseScopeName = await codeToTokensBase(engine, code, { lang: 'js', theme: 'vitesse-dark', includeExplanation: 'scopeName' })

expect(caseFalse).toMatchSnapshot('false')
expect(caseTrue).toMatchSnapshot('true')
expect(caseScopeName).toMatchSnapshot('scopeName')
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
import { expect, it } from 'vitest'
import { createHighlighter } from '../src'
import { createHighlighterCore } from '../src'

it('transformers tokens', async () => {
const shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
using shiki = await createHighlighterCore({
themes: [
import('@shikijs/themes/vitesse-light'),
],
langs: [
import('@shikijs/langs/javascript'),
],
engine: createJavaScriptRegexEngine(),
})

expect(shiki.codeToHtml('console.log', {
Expand Down
36 changes: 18 additions & 18 deletions packages/engine-javascript/test/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export interface Cases {
const cases: Cases[] = [
{
name: 'beancount',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/beancount.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/beancount'),
cases: [
`2012-11-03 * "Transfer to pay credit card"
Assets:MyBank:Checking -400.00 USD
Expand All @@ -62,51 +62,51 @@ const cases: Cases[] = [
},
{
name: 'json-basic',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/json.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/json'),
cases: [
'{"foo":{"bar":1}}',
'[undefined, null, true, false, 0, 1, 1.1, "foo", [], {}]',
],
},
{
name: 'html-basic',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/html.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/html'),
cases: [
'<div class="foo">bar</div>',
'<!DOCTYPE html><html><head><title>foo</title></head><body>bar</body></html>',
],
},
{
name: 'ts-basic',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/typescript.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/typescript'),
cases: [
'const foo: string = "bar"',
],
},
{
name: 'jsonc',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/jsonc.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/jsonc'),
cases: [
'// comment\n{"foo":"bar"}',
],
},
{
name: 'vue',
theme: () => import('../../shiki/src/themes/vitesse-dark.mjs'),
lang: () => import('../../shiki/src/langs/vue.mjs'),
theme: () => import('@shikijs/themes/vitesse-dark'),
lang: () => import('@shikijs/langs/vue'),
cases: [
`<script setup>\nimport { ref } from 'vue'\n</script>`,
`<template>\n<div>{{ foo }}</div>\n</template>`,
],
},
{
name: 'toml',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/toml.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/toml'),
cases: [
[
`# This is a TOML document`,
Expand All @@ -120,8 +120,8 @@ const cases: Cases[] = [
},
{
name: 'sql',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/sql.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/sql'),
cases: [
'SELECT * FROM foo',
[
Expand All @@ -134,8 +134,8 @@ const cases: Cases[] = [
},
{
name: 'markdown',
theme: () => import('../../shiki/src/themes/nord.mjs'),
lang: () => import('../../shiki/src/langs/markdown.mjs'),
theme: () => import('@shikijs/themes/nord'),
lang: () => import('@shikijs/langs/markdown'),
cases: [
[
'# Header',
Expand Down
4 changes: 2 additions & 2 deletions packages/engine-javascript/test/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createJavaScriptRegexEngine } from '../src/engine-compile'

describe('should', () => {
it('works', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
engine: createJavaScriptRegexEngine(),
Expand All @@ -15,7 +15,7 @@ describe('should', () => {
})

it('dynamic load theme and lang', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript', 'ts'],
engine: createJavaScriptRegexEngine(),
Expand Down
2 changes: 1 addition & 1 deletion packages/markdown-it/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fromHighlighter } from '../src/core'

it('run for base', { timeout: 10_000 }, async () => {
const md = MarkdownIt()
const shiki = await createHighlighter({
using shiki = await createHighlighter({
langs: ['js'],
themes: ['vitesse-light', 'vitesse-dark'],
})
Expand Down
2 changes: 1 addition & 1 deletion packages/shiki/test/dist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, it } from 'vitest'
import { createHighlighter } from '../dist/index.mjs'

it('should works', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
Expand Down
12 changes: 6 additions & 6 deletions packages/shiki/test/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { codeToHtml, createHighlighter } from '../src'

describe('should', () => {
it('works', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript'],
})
Expand All @@ -13,7 +13,7 @@ describe('should', () => {
})

it('dynamic load theme and lang', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['vitesse-light'],
langs: ['javascript', 'ts'],
})
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('should', () => {
})

it('requires nested lang', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['nord'],
langs: [
'vue',
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('should', () => {

// https://github.com/shikijs/shiki/issues/35
it('dynamic load theme and lang with md', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
langs: [],
themes: [],
})
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('should', () => {
})

it('dynamic load lang with vue', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
langs: [],
themes: [],
})
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('should', () => {
})

it('should have correct offset', async () => {
const shiki = await createHighlighter({
using shiki = await createHighlighter({
themes: ['nord'],
langs: ['html'],
})
Expand Down
Loading

0 comments on commit 7ac62e1

Please sign in to comment.