Skip to content

Commit

Permalink
100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Dec 16, 2023
1 parent 7e0362c commit 1e7567c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 35 deletions.
1 change: 0 additions & 1 deletion borp.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ try {
const localPrefix = relative(process.cwd(), config.prefix)
exclude = exclude.map((file) => posix.join(localPrefix, file))
}
console.log('>> Excluding from coverage:', exclude)
const report = Report({
reporter: ['text'],
tempDirectory: covDir,
Expand Down
58 changes: 24 additions & 34 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ import { run } from 'node:test'
import { glob } from 'glob'
import { findUp } from 'find-up'
import { createRequire } from 'node:module'
import { resolve, join, dirname } from 'node:path'
import { join, dirname } from 'node:path'
import { access, readFile } from 'node:fs/promises'
import { execa } from 'execa'

async function isFileAccessible (filename, directory) {
try {
const filePath = directory ? resolve(directory, filename) : filename
await access(filePath)
return true
} catch (err) {
return false
}
}

function deferred () {
let resolve
let reject
Expand All @@ -28,7 +18,7 @@ function deferred () {

export default async function runWithTypeScript (config) {
const { cwd } = config
const chunks = []
let pushable = []
const tsconfigPath = await findUp('tsconfig.json', { cwd })

let prefix = ''
Expand All @@ -39,22 +29,20 @@ export default async function runWithTypeScript (config) {
const typescriptPathCWD = _require.resolve('typescript')
tscPath = join(typescriptPathCWD, '..', '..', 'bin', 'tsc')
if (tscPath) {
const isAccessible = await isFileAccessible(tscPath)
if (isAccessible) {
// Watch is handled aftterwards
if (!config.watch) {
const start = Date.now()
await execa('node', [tscPath], { cwd: dirname(tsconfigPath) })
chunks.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
}
})
}
} else {
throw new Error('Could not find tsc')
// This will throw if we cannot find the `tsc` binary
await access(tscPath)

// Watch is handled aftterwards
if (!config.watch) {
const start = Date.now()
await execa('node', [tscPath], { cwd: dirname(tsconfigPath) })
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
message: `TypeScript compilation complete (${Date.now() - start}ms)`
}
})
}
}
const tsconfig = JSON.parse(await readFile(tsconfigPath))
Expand All @@ -65,9 +53,10 @@ export default async function runWithTypeScript (config) {
}
config.prefix = prefix
config.setup = (test) => {
for (const chunk of chunks) {
for (const chunk of pushable) {
test.reporter.push(chunk)
}
pushable = test.reporter
}

let tscChild
Expand All @@ -82,7 +71,7 @@ export default async function runWithTypeScript (config) {
tscChild.stdout.setEncoding('utf8')
tscChild.stdout.on('data', (data) => {
if (data.includes('Watching for file changes')) {
chunks.push({
pushable.push({
type: 'test:diagnostic',
data: {
nesting: 0,
Expand All @@ -93,8 +82,7 @@ export default async function runWithTypeScript (config) {
p.resolve()
}
if (data.includes('error TS')) {
const toPush = stream || chunks
toPush.push({
pushable.push({
type: 'test:fail',
data: {
nesting: 0,
Expand All @@ -121,10 +109,12 @@ export default async function runWithTypeScript (config) {
files = files.map((file) => join(prefix, file.replace(/ts$/, 'js')))
}
} else if (config.pattern) {
let pattern = config.pattern
if (prefix) {
config.pattern = join(prefix, config.pattern)
pattern = join(prefix, pattern)
pattern = pattern.replace(/ts$/, 'js')
}
files = await glob(config.pattern, { ignore, cwd, windowsPathsNoEscape: true })
files = await glob(pattern, { ignore, cwd, windowsPathsNoEscape: true })
} else if (prefix) {
files = await glob(join(prefix, join('test', '**', '*.test.{cjs,mjs,js}')), { ignore, cwd, windowsPathsNoEscape: true })
} else {
Expand Down
72 changes: 72 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,75 @@ test('ts-cjs', async (t) => {

await completed
})

test('ts-esm with named failes', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 3 })
const config = {
files: ['test/add.test.ts'],
cwd: join(import.meta.url, '..', 'fixtures', 'ts-esm')
}

const stream = await runWithTypeScript(config)

const names = new Set(['add'])

stream.once('data', (test) => {
strictEqual(test.type, 'test:diagnostic')
match(test.data.message, /TypeScript compilation complete \(\d+ms\)/)
})

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

await completed
})

test('pattern', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 3 })
const config = {
files: [],
pattern: 'test/*2.test.ts',
cwd: join(import.meta.url, '..', 'fixtures', 'ts-esm')
}

const stream = await runWithTypeScript(config)

const names = new Set(['add2'])

stream.once('data', (test) => {
strictEqual(test.type, 'test:diagnostic')
match(test.data.message, /TypeScript compilation complete \(\d+ms\)/)
})

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

await completed
})

test('no files', async (t) => {
const { strictEqual, completed, match } = tspl(t, { plan: 4 })
const config = {
cwd: join(import.meta.url, '..', 'fixtures', 'ts-esm')
}

const stream = await runWithTypeScript(config)

const names = new Set(['add', 'add2'])

stream.once('data', (test) => {
strictEqual(test.type, 'test:diagnostic')
match(test.data.message, /TypeScript compilation complete \(\d+ms\)/)
})

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

await completed
})

0 comments on commit 1e7567c

Please sign in to comment.