Skip to content

Commit

Permalink
Remove path, use fs/promises in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 17, 2023
1 parent 998ba08 commit 69583d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
38 changes: 20 additions & 18 deletions test/loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {promises as fs} from 'node:fs'
import path from 'node:path'
import {URL, fileURLToPath} from 'node:url'
import fs from 'node:fs/promises'
import {fileURLToPath} from 'node:url'
import {transform, transformSync} from 'esbuild'

const {load, getFormat, transformSource} = createLoader()
Expand All @@ -15,20 +14,19 @@ export function createLoader() {

// Node version 17.
/**
* @param {string} url
* @param {string} href
* @param {unknown} context
* @param {Function} defaultLoad
*/
async function load(url, context, defaultLoad) {
if (path.extname(url) !== '.jsx') {
return defaultLoad(url, context, defaultLoad)
}
async function load(href, context, defaultLoad) {
const url = new URL(href)

const fp = fileURLToPath(new URL(url))
const value = await fs.readFile(fp)
if (!url.pathname.endsWith('.jsx')) {
return defaultLoad(href, context, defaultLoad)
}

const {code, warnings} = await transform(String(value), {
sourcefile: fp,
const {code, warnings} = await transform(String(await fs.readFile(url)), {
sourcefile: fileURLToPath(url),
sourcemap: 'both',
loader: 'jsx',
target: 'esnext',
Expand All @@ -47,14 +45,16 @@ export function createLoader() {

// Pre version 17.
/**
* @param {string} url
* @param {string} href
* @param {unknown} context
* @param {Function} defaultGetFormat
*/
function getFormat(url, context, defaultGetFormat) {
return path.extname(url) === '.jsx'
function getFormat(href, context, defaultGetFormat) {
const url = new URL(href)

return url.pathname.endsWith('.jsx')
? {format: 'module'}
: defaultGetFormat(url, context, defaultGetFormat)
: defaultGetFormat(href, context, defaultGetFormat)
}

/**
Expand All @@ -63,12 +63,14 @@ export function createLoader() {
* @param {Function} defaultTransformSource
*/
async function transformSource(value, context, defaultTransformSource) {
if (path.extname(context.url) !== '.jsx') {
const url = new URL(context.url)

if (!url.pathname.endsWith('.jsx')) {
return defaultTransformSource(value, context, defaultTransformSource)
}

const {code, warnings} = transformSync(String(value), {
sourcefile: fileURLToPath(context.url),
sourcefile: fileURLToPath(url),
sourcemap: 'both',
loader: 'jsx',
target: 'esnext',
Expand Down
19 changes: 6 additions & 13 deletions test/test.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Position} Position
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
* @typedef {import('hast').Text} Text
* @typedef {import('react').ReactNode} ReactNode
* @typedef {import('../index.js').Components} Components
*/

import fs from 'node:fs'
import path from 'node:path'
import fs from 'node:fs/promises'
import {test} from 'uvu'
import * as assert from 'uvu/assert'
import React from 'react'
Expand Down Expand Up @@ -1057,13 +1052,11 @@ test('should throw on invalid component', () => {
)
})

test('can render the whole spectrum of markdown within a single run', () => {
const input = String(
fs.readFileSync(path.join('test', 'fixtures', 'runthrough.md'))
)
const expected = String(
fs.readFileSync(path.join('test', 'fixtures', 'runthrough.html'))
)
test('can render the whole spectrum of markdown within a single run', async () => {
const inputUrl = new URL('fixtures/runthrough.md', import.meta.url)
const expectedUrl = new URL('fixtures/runthrough.html', import.meta.url)
const input = String(await fs.readFile(inputUrl))
const expected = String(await fs.readFile(expectedUrl))

const actual = asHtml(
<Markdown children={input} remarkPlugins={[gfm]} rehypePlugins={[raw]} />
Expand Down

0 comments on commit 69583d2

Please sign in to comment.