From 1e7567c668a4b982f86f408545f6357764016885 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 16 Dec 2023 09:33:34 +0100 Subject: [PATCH] 100% code coverage --- borp.js | 1 - lib/run.js | 58 ++++++++++++++++--------------------- test/basic.test.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 35 deletions(-) diff --git a/borp.js b/borp.js index 7e207a0..70d0236 100755 --- a/borp.js +++ b/borp.js @@ -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, diff --git a/lib/run.js b/lib/run.js index 6627622..25cf2b0 100644 --- a/lib/run.js +++ b/lib/run.js @@ -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 @@ -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 = '' @@ -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)) @@ -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 @@ -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, @@ -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, @@ -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 { diff --git a/test/basic.test.js b/test/basic.test.js index bf45506..73e182e 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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 +})