Files
buuh/packages/migrate/test/transform.test.js
T

93 lines
3.1 KiB
JavaScript
Raw Normal View History

import { test } from 'node:test'
import assert from 'node:assert'
import transform from '../lib/transform.js'
test('converts a classic choo v7 header to v8 ESM', () => {
const src = [
"var choo = require('choo')",
"var html = require('choo/html')",
"var devtools = require('choo-devtools')",
'',
'var app = choo()',
"app.use(devtools())",
'module.exports = app'
].join('\n')
const { code, changed, notes } = transform(src)
assert.ok(changed)
assert.match(code, /import choo from '@choojs\/core'/)
assert.match(code, /import html from '@choojs\/html'/)
assert.match(code, /import devtools from '@choojs\/devtools'/)
assert.match(code, /export default app/)
assert.deepStrictEqual(notes, [])
})
test('destructured and property requires', () => {
const src = [
"const { render } = require('some-lib')",
"const thing = require('other-lib').thing",
"const dflt = require('third-lib').default",
"require('./side-effect')"
].join('\n')
const { code } = transform(src)
assert.match(code, /import { render } from 'some-lib'/)
assert.match(code, /import { thing as thing } from 'other-lib'/)
assert.match(code, /import dflt from 'third-lib'/)
assert.match(code, /import '\.\/side-effect'/)
})
test('already-ESM sources get specifiers remapped', () => {
const src = [
"import choo from 'choo'",
"import html from 'nanohtml'",
"import raw from 'nanohtml/raw'",
"import morph from 'nanomorph'",
"import Component from 'nanocomponent'"
].join('\n')
const { code } = transform(src)
assert.match(code, /from '@choojs\/core'/)
assert.match(code, /from '@choojs\/html'\n/)
assert.match(code, /from '@choojs\/html\/raw'/)
assert.match(code, /from '@choojs\/html\/morph'/)
assert.match(code, /from '@choojs\/component'/)
})
test('retired packages produce notes, not rewrites', () => {
const src = [
"var nanobus = require('nanobus')",
"var lazyRoute = require('choo-lazy-route')"
].join('\n')
const { code, notes } = transform(src)
assert.match(code, /from 'nanobus'/, 'retired specifier left for the human')
assert.ok(notes.some((n) => n.includes('nanobus')))
assert.ok(notes.some((n) => n.includes('lazy()')), 'choo-lazy-route points at lazy()')
})
test('dynamic import() specifiers are remapped too', () => {
const { code } = transform("const mod = await import('choo/html')")
assert.match(code, /import\('@choojs\/html'\)/)
})
test('unconvertible CJS is reported honestly', () => {
const src = [
"module.exports.helper = function () {}",
"function f () { const x = require('choo') }"
].join('\n')
const { code, notes } = transform(src)
assert.match(code, /require\('@choojs\/core'\)/, 'specifier remapped even inside functions')
assert.ok(notes.some((n) => n.includes('module.exports')))
assert.ok(notes.some((n) => n.includes('require() calls remain')))
})
test('non-choo sources pass through untouched', () => {
const src = "import fs from 'node:fs'\nexport const x = 1\n"
const { changed, notes } = transform(src)
assert.strictEqual(changed, false)
assert.deepStrictEqual(notes, [])
})