Skip to content

Commit

Permalink
formatting, use tap affordances
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 6, 2024
1 parent c8e0a5e commit 3ad79d6
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 69 deletions.
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ const getConfig = (
? pkg.tshy
: {}
let exportsConfig = tshy.exports
if (typeof exportsConfig === 'string' || Array.isArray(exportsConfig)) {
if (
typeof exportsConfig === 'string' ||
Array.isArray(exportsConfig)
) {
// Strip off the `./src` prefix and the extension
// exports: "src/**/*.ts" => exports: {"./foo": "./src/foo.ts"}
const exp: Exclude<TshyConfig['exports'], undefined> = {}
Expand Down
5 changes: 2 additions & 3 deletions src/write-package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { writeFileSync } from 'fs'
import {stringify} from 'polite-json'
import { stringify } from 'polite-json'
import pkg from './package.js'

export default () =>
writeFileSync('package.json', stringify(pkg))
export default () => writeFileSync('package.json', stringify(pkg))
9 changes: 4 additions & 5 deletions test/build-commonjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { readdirSync } from 'fs'
import t from 'tap'

const node = process.execPath
const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))

t.cleanSnapshot = s => s.split(node).join('{NODE}').replace(/\\/g, '/')
t.cleanSnapshot = s =>
s.split(node).join('{NODE}').replace(/\\/g, '/')

const spawnSuccess: SpawnSyncReturns<Buffer> = {
status: 0,
Expand Down Expand Up @@ -41,7 +40,7 @@ const output = () =>

t.test('basic commonjs build', async t => {
spawnResult = spawnSuccess
process.chdir(
t.chdir(
t.testdir({
'package.json': JSON.stringify({
tshy: {
Expand Down Expand Up @@ -89,7 +88,7 @@ t.test('basic commonjs build', async t => {

t.test('build failure', async t => {
spawnResult = spawnFail
process.chdir(
t.chdir(
t.testdir({
'package.json': JSON.stringify({
tshy: {
Expand Down
7 changes: 2 additions & 5 deletions test/build-esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { SpawnSyncReturns } from 'child_process'
import { readdirSync } from 'fs'
import t from 'tap'

const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))

t.cleanSnapshot = s => s.split(process.execPath).join('{NODE}')

const spawnSuccess: SpawnSyncReturns<Buffer> = {
Expand Down Expand Up @@ -40,7 +37,7 @@ const output = () =>

t.test('basic esm build', async t => {
spawnResult = spawnSuccess
process.chdir(
t.chdir(
t.testdir({
'package.json': JSON.stringify({
tshy: {
Expand Down Expand Up @@ -88,7 +85,7 @@ t.test('basic esm build', async t => {

t.test('build failure', async t => {
spawnResult = spawnFail
process.chdir(
t.chdir(
t.testdir({
'package.json': JSON.stringify({
tshy: {
Expand Down
5 changes: 1 addition & 4 deletions test/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,10 @@ t.test('build both', async t => {
})

t.test('imports linking', async t => {
const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))

// make sure one of them doesn't already have a scripts block
for (const i of ['imports', 'imports-with-star', 'basic']) {
t.test(i, async t => {
process.chdir(
t.chdir(
fileURLToPath(new URL('./fixtures/' + i, import.meta.url))
)
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
Expand Down
8 changes: 3 additions & 5 deletions test/clean-build-tmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { statSync } from 'fs'
import t from 'tap'
import cleanBuildTmp from '../src/clean-build-tmp.js'
import readTypescriptConfig from '../src/read-typescript-config.js'
const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))

t.test('no incremental build, just delete it', t => {
readTypescriptConfig().options.incremental = false
process.chdir(
t.chdir(
t.testdir({
'.tshy-build': {},
})
Expand All @@ -19,7 +17,7 @@ t.test('no incremental build, just delete it', t => {

t.test('no tsbuildinfo, just delete it', t => {
readTypescriptConfig().options.incremental = true
process.chdir(
t.chdir(
t.testdir({
'.tshy-build': {},
})
Expand All @@ -31,7 +29,7 @@ t.test('no tsbuildinfo, just delete it', t => {

t.test('remove files not found in src', t => {
readTypescriptConfig().options.incremental = true
process.chdir(
t.chdir(
t.testdir({
'.tshy-build': {
'.tshy': { 'esm.tsbuildinfo': '{}' },
Expand Down
39 changes: 23 additions & 16 deletions test/exports.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { ConditionalValue } from 'resolve-import'
import t from 'tap'
import { ConditionalValue, ExportsSubpaths } from 'resolve-import'
import t, { Test } from 'tap'
import { PolyfillSet } from '../src/polyfills.js'
import { TshyConfig } from '../src/types.js'
import { Package, TshyConfig } from '../src/types.js'

// order is relevant in the exports objects we're snapshotting here
t.compareOptions = { sort: false }

const { getImpTarget, getReqTarget } = (await t.mockImport(
const { getImpTarget, getReqTarget } = await t.mockImport<
typeof import('../src/exports.js')
>('../src/exports.js', {
'../src/dialects.js': { default: ['esm', 'commonjs'] },
})

const cjs = await t.mockImport<typeof import('../src/exports.js')>(
'../src/exports.js',
{
'../src/dialects.js': { default: ['esm', 'commonjs'] },
'../src/dialects.js': { default: ['commonjs'] },
}
)) as typeof import('../src/exports.js')

const cjs = (await t.mockImport('../src/exports.js', {
'../src/dialects.js': { default: ['commonjs'] },
})) as typeof import('../src/exports.js')
const esm = (await t.mockImport('../src/exports.js', {
'../src/dialects.js': { default: ['esm'] },
})) as typeof import('../src/exports.js')
)
const esm = await t.mockImport<typeof import('../src/exports.js')>(
'../src/exports.js',
{
'../src/dialects.js': { default: ['esm'] },
}
)

t.equal(getImpTarget(undefined), undefined)
t.equal(getImpTarget('foo.cts'), undefined)
Expand Down Expand Up @@ -241,16 +246,18 @@ t.test('setting top level main', async t => {

const exits = t.capture(process, 'exit', () => false).args
const fails: any[][] = []
const { setMain } = await t.mockImport('../dist/esm/exports.js', {
'../dist/esm/fail.js': {
const { setMain } = await t.mockImport<
typeof import('../src/exports.js')
>('../src/exports.js', {
'../src/fail.js': {
default: (...a: any[]) => fails.push(a),
},
})
for (const [name, pkg, expect, ok] of cases) {
t.test(name, t => {
const { tshy = {}, type } = pkg
const { main } = tshy
setMain(pkg.tshy, pkg)
setMain(pkg.tshy, pkg as Package & { exports: ExportsSubpaths })
if (ok) {
t.equal(pkg.main, expect.main)
t.equal(pkg.types, expect.types)
Expand Down
10 changes: 6 additions & 4 deletions test/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ t.test('load package successfully', async t => {

t.test('unsuccessfully fails build', async t => {
const exits = t.capture(process, 'exit').args
process.chdir(t.testdir())
t.chdir(t.testdir())
let failed = false
await t.mockImport('../dist/esm/package.js', {
'../dist/esm/fail.js': () => (failed = true),
Expand All @@ -21,9 +21,11 @@ t.test('unsuccessfully fails build', async t => {

t.test('fail if the package.json is not an object', async t => {
const exits = t.capture(process, 'exit').args
process.chdir(t.testdir({
'package.json': '[null, 1, "asdf"]'
}))
t.chdir(
t.testdir({
'package.json': '[null, 1, "asdf"]',
})
)
let failed = false
await t.mockImport('../dist/esm/package.js', {
'../dist/esm/fail.js': () => (failed = true),
Expand Down
4 changes: 1 addition & 3 deletions test/self-dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ t.test('already in node_modules, do not create link', t => {

t.plan(cases.length)

const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))
for (const [name, d] of cases) {
t.test(d, async t => {
// need a separate import for each test, because this gets cached
Expand All @@ -164,7 +162,7 @@ t.test('already in node_modules, do not create link', t => {
'../dist/esm/self-link.js',
{ mkdirp, fs, rimraf }
)) as typeof import('../dist/esm/self-link.js')
process.chdir(resolve(dir, d))
t.chdir(resolve(dir, d))
link({ name, version: '1.2.3' }, 'src')
unlink({ name, version: '1.2.3' }, 'src')
const rl = readlinkCalls()
Expand Down
4 changes: 1 addition & 3 deletions test/sources.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import t from 'tap'

const cwd = process.cwd()
t.after(() => process.chdir(cwd))
process.chdir(
t.chdir(
t.testdir({
src: {
dir: {
Expand Down
4 changes: 1 addition & 3 deletions test/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
import { resolve } from 'path'
import t from 'tap'

const cwd = process.cwd()
t.after(() => process.chdir(cwd))
const dir = t.testdir({
'package.json': JSON.stringify({
tshy: {
Expand All @@ -25,7 +23,7 @@ const dir = t.testdir({
'index-webpack.mts': '',
},
})
process.chdir(dir)
t.chdir(dir)

t.test('with tsconfig.json file', async t => {
await import('../dist/esm/tsconfig.js')
Expand Down
4 changes: 1 addition & 3 deletions test/unbuilt-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { rimrafSync } from 'rimraf'
import t from 'tap'

t.test('imports linking', async t => {
const cwd = process.cwd()
t.afterEach(() => process.chdir(cwd))
const require = createRequire(import.meta.url)

// make sure one of them doesn't already have a scripts block
Expand All @@ -32,7 +30,7 @@ t.test('imports linking', async t => {
rimrafSync(dist)
})

process.chdir(
t.chdir(
fileURLToPath(new URL('./fixtures/' + i, import.meta.url))
)
await t.mockImport('../dist/esm/index.js')
Expand Down
24 changes: 20 additions & 4 deletions test/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ t.test('no error', async t => {
link()
t.strictSame(errs(), [])
t.strictSame(exits(), [[0]])
t.matchSnapshot(logs().map(s => s.join('')).join('\n'))
t.matchSnapshot(
logs()
.map(s => s.join(''))
.join('\n')
)
})
t.test('no link', async t => {
noLink()
t.strictSame(errs(), [])
t.strictSame(exits(), [[0]])
t.matchSnapshot(logs().map(s => s.join('')).join('\n'))
t.matchSnapshot(
logs()
.map(s => s.join(''))
.join('\n')
)
})
})

Expand All @@ -41,12 +49,20 @@ t.test('with error', async t => {
link('error string')
t.strictSame(logs(), [])
t.strictSame(exits(), [[1]])
t.matchSnapshot(errs().map(s => s.join('')).join('\n'))
t.matchSnapshot(
errs()
.map(s => s.join(''))
.join('\n')
)
})
t.test('no link', async t => {
noLink('error string')
t.strictSame(logs(), [])
t.strictSame(exits(), [[1]])
t.matchSnapshot(errs().map(s => s.join('')).join('\n'))
t.matchSnapshot(
errs()
.map(s => s.join(''))
.join('\n')
)
})
})
13 changes: 8 additions & 5 deletions test/valid-exclude.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import t from 'tap'
const exits = t.capture(process, 'exit', () => false).args
const mf = t.captureFn(() => {})
const fails = (mf).args
const { default: validExclude } = await t.mockImport('../src/valid-exclude.js', {
'../src/fail.js': mf
}) as typeof import('../src/valid-exclude.js')
const fails = mf.args
const { default: validExclude } = (await t.mockImport(
'../src/valid-exclude.js',
{
'../src/fail.js': mf,
}
)) as typeof import('../src/valid-exclude.js')

t.equal(validExclude(['a','b']), true)
t.equal(validExclude(['a', 'b']), true)
t.equal(validExclude(['']), true)

t.equal(validExclude([]), false)
Expand Down
2 changes: 1 addition & 1 deletion test/valid-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const cases: [pkg: Omit<Package, 'name' | 'version'>, ok: boolean][] =
[{ imports: { x: 'y' } }, false],
[{ imports: { '#': 'y' } }, false],
[{ imports: { '#x': './src/x' } }, true],
[{ imports: { '#x': ['./src/x'] }}, false],
[{ imports: { '#x': ['./src/x'] } }, false],
]

for (const [pkg, ok] of cases) {
Expand Down
5 changes: 4 additions & 1 deletion test/which-tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import t from 'tap'
import tsc from '../src/which-tsc.js'
import { accessSync, constants } from 'node:fs'

t.doesNotThrow(() => accessSync(tsc, constants.R_OK), 'tsc is readable')
t.doesNotThrow(
() => accessSync(tsc, constants.R_OK),
'tsc is readable'
)
4 changes: 1 addition & 3 deletions test/write-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { readFileSync } from 'fs'
import { resolve } from 'path'
import t from 'tap'

const cwd = process.cwd()
t.after(() => process.chdir(cwd))
process.chdir(t.testdir({}))
t.chdir(t.testdir({}))
const { default: writePackage } = (await t.mockImport(
'../dist/esm/write-package.js',
{
Expand Down

0 comments on commit 3ad79d6

Please sign in to comment.