vitest
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,4 +0,3 @@ | ||
import { Suite, Task } from './types'; | ||
import { TaskResult } from '.'; | ||
export declare const beforeHook: { | ||
import { File, Suite, Task } from './types'; | ||
export declare const beforeAllHook: { | ||
on(fn: (...args: any[]) => void | Promise<void>): void; | ||
@@ -8,3 +7,3 @@ fire(...args: any[]): Promise<void>; | ||
}; | ||
export declare const afterHook: { | ||
export declare const afterAllHook: { | ||
on(fn: (...args: any[]) => void | Promise<void>): void; | ||
@@ -20,14 +19,14 @@ fire(...args: any[]): Promise<void>; | ||
export declare const afterEachHook: { | ||
on(fn: (args_0: Task, args_1: TaskResult) => void | Promise<void>): void; | ||
fire(args_0: Task, args_1: TaskResult): Promise<void>; | ||
on(fn: (args_0: Task) => void | Promise<void>): void; | ||
fire(args_0: Task): Promise<void>; | ||
clear(): void; | ||
}; | ||
export declare const beforeFileHook: { | ||
on(fn: (args_0: string) => void | Promise<void>): void; | ||
fire(args_0: string): Promise<void>; | ||
on(fn: (args_0: File) => void | Promise<void>): void; | ||
fire(args_0: File): Promise<void>; | ||
clear(): void; | ||
}; | ||
export declare const afterFileHook: { | ||
on(fn: (args_0: string) => void | Promise<void>): void; | ||
fire(args_0: string): Promise<void>; | ||
on(fn: (args_0: File) => void | Promise<void>): void; | ||
fire(args_0: File): Promise<void>; | ||
clear(): void; | ||
@@ -45,9 +44,9 @@ }; | ||
}; | ||
export declare const before: (fn: (...args: any[]) => void | Promise<void>) => void; | ||
export declare const after: (fn: (...args: any[]) => void | Promise<void>) => void; | ||
export declare const beforeAll: (fn: (...args: any[]) => void | Promise<void>) => void; | ||
export declare const afterAll: (fn: (...args: any[]) => void | Promise<void>) => void; | ||
export declare const beforeEach: (fn: (args_0: Task) => void | Promise<void>) => void; | ||
export declare const afterEach: (fn: (args_0: Task, args_1: TaskResult) => void | Promise<void>) => void; | ||
export declare const beforeFile: (fn: (args_0: string) => void | Promise<void>) => void; | ||
export declare const afterFile: (fn: (args_0: string) => void | Promise<void>) => void; | ||
export declare const afterEach: (fn: (args_0: Task) => void | Promise<void>) => void; | ||
export declare const beforeFile: (fn: (args_0: File) => void | Promise<void>) => void; | ||
export declare const afterFile: (fn: (args_0: File) => void | Promise<void>) => void; | ||
export declare const beforeSuite: (fn: (args_0: Suite) => void | Promise<void>) => void; | ||
export declare const afterSuite: (fn: (args_0: Suite) => void | Promise<void>) => void; |
import { createHook } from './utils/hook'; | ||
export const beforeHook = createHook(); | ||
export const afterHook = createHook(); | ||
export const beforeAllHook = createHook(); | ||
export const afterAllHook = createHook(); | ||
export const beforeEachHook = createHook(); | ||
@@ -10,4 +10,4 @@ export const afterEachHook = createHook(); | ||
export const afterSuiteHook = createHook(); | ||
export const before = beforeHook.on; | ||
export const after = afterHook.on; | ||
export const beforeAll = beforeAllHook.on; | ||
export const afterAll = afterAllHook.on; | ||
export const beforeEach = beforeEachHook.on; | ||
@@ -14,0 +14,0 @@ export const afterEach = afterEachHook.on; |
@@ -5,1 +5,2 @@ export * from './types'; | ||
export * from './chai'; | ||
export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks'; |
@@ -5,1 +5,2 @@ export * from './types'; | ||
export * from './chai'; | ||
export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks'; |
import { File, Options, Task, TaskResult } from './types'; | ||
export declare function runTasks(tasks: Task[]): Promise<TaskResult[]>; | ||
export declare function parseFile(filepath: string): Promise<File>; | ||
export declare function runFile(filepath: string): Promise<void>; | ||
export declare function collectFiles(files: string[]): Promise<File[]>; | ||
export declare function runFile(file: File): Promise<void>; | ||
export declare function run(options?: Options): Promise<void>; |
107
dist/run.js
import { relative } from 'path'; | ||
import { performance } from 'perf_hooks'; | ||
import c from 'picocolors'; | ||
@@ -7,3 +8,3 @@ import chai from 'chai'; | ||
import { context } from './context'; | ||
import { afterEachHook, afterFileHook, afterHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeHook, beforeSuiteHook } from './hooks'; | ||
import { afterEachHook, afterFileHook, afterAllHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeAllHook, beforeSuiteHook } from './hooks'; | ||
import { SnapshotPlugin } from './snapshot/index'; | ||
@@ -13,4 +14,4 @@ export async function runTasks(tasks) { | ||
for (const task of tasks) { | ||
const result = { task }; | ||
await beforeEachHook.fire(task); | ||
task.result = {}; | ||
try { | ||
@@ -20,6 +21,5 @@ await task.fn(); | ||
catch (e) { | ||
result.error = e; | ||
task.result.error = e; | ||
} | ||
results.push(result); | ||
await afterEachHook.fire(task, result); | ||
await afterEachHook.fire(task); | ||
} | ||
@@ -30,23 +30,28 @@ return results; | ||
const { log } = console; | ||
export async function parseFile(filepath) { | ||
clearContext(); | ||
await import(filepath); | ||
const suites = [defaultSuite, ...context.suites]; | ||
const tasks = await Promise.all(suites.map(async (suite) => { | ||
export async function collectFiles(files) { | ||
const result = []; | ||
for (const filepath of files) { | ||
clearContext(); | ||
await import(filepath); | ||
const suites = [defaultSuite, ...context.suites]; | ||
const collected = []; | ||
for (const suite of suites) { | ||
context.currentSuite = suite; | ||
const tasks = await suite.collect(); | ||
collected.push([suite, tasks]); | ||
} | ||
const file = { | ||
filepath, | ||
suites, | ||
collected, | ||
}; | ||
file.collected.forEach(([, tasks]) => tasks.forEach(task => task.file = file)); | ||
result.push(file); | ||
} | ||
return result; | ||
} | ||
export async function runFile(file) { | ||
await beforeFileHook.fire(file); | ||
for (const [suite, tasks] of file.collected) { | ||
await beforeSuiteHook.fire(suite); | ||
context.currentSuite = suite; | ||
return [suite, await suite.collect()]; | ||
})); | ||
const file = { | ||
filepath, | ||
suites, | ||
tasks, | ||
}; | ||
file.tasks.forEach(([, tasks]) => tasks.forEach(task => task.file = file)); | ||
return file; | ||
} | ||
export async function runFile(filepath) { | ||
await beforeFileHook.fire(filepath); | ||
const file = await parseFile(filepath); | ||
for (const [suite, tasks] of file.tasks) { | ||
let indent = 1; | ||
@@ -57,13 +62,23 @@ if (suite.name) { | ||
} | ||
const result = await runTasks(tasks); | ||
for (const r of result) { | ||
if (r.error === undefined) { | ||
log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(r.task.name)}`); | ||
if (suite.mode === 'run' || suite.mode === 'only') { | ||
// TODO: If there is a task with 'only', skip all others | ||
await runTasks(tasks); | ||
for (const t of tasks) { | ||
if (t.result && t.result.error === undefined) { | ||
log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(t.name)}`); | ||
} | ||
else { | ||
console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(t.name)}`); | ||
console.error(' '.repeat((indent + 2) * 2) + c.red(String(t.result.error))); | ||
process.exitCode = 1; | ||
} | ||
} | ||
else { | ||
console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(r.task.name)}`); | ||
console.error(' '.repeat((indent + 2) * 2) + c.red(String(r.error))); | ||
process.exitCode = 1; | ||
} | ||
} | ||
else if (suite.mode === 'skip') { | ||
log(`${' '.repeat(indent * 2)}${c.inverse(c.gray(' SKIP '))}`); | ||
} | ||
else if (suite.mode === 'todo') { | ||
// TODO: In Jest, these suites are collected and printed together at the end of the report | ||
log(`${' '.repeat(indent * 2)}${c.inverse(c.gray(' TODO '))}`); | ||
} | ||
if (suite.name) | ||
@@ -73,11 +88,11 @@ indent -= 1; | ||
} | ||
await afterFileHook.fire(filepath); | ||
await afterFileHook.fire(file); | ||
} | ||
export async function run(options = {}) { | ||
const { rootDir = process.cwd() } = options; | ||
chai.use(SnapshotPlugin({ | ||
chai.use(await SnapshotPlugin({ | ||
rootDir, | ||
update: options.updateSnapshot, | ||
})); | ||
const files = await fg(options.includes || ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], { | ||
const paths = await fg(options.includes || ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], { | ||
absolute: true, | ||
@@ -87,3 +102,3 @@ cwd: options.rootDir, | ||
}); | ||
if (!files.length) { | ||
if (!paths.length) { | ||
console.error('No test files found'); | ||
@@ -93,10 +108,20 @@ process.exitCode = 1; | ||
} | ||
await beforeHook.fire(); | ||
const files = await collectFiles(paths); | ||
await beforeAllHook.fire(); | ||
const start = performance.now(); | ||
for (const file of files) { | ||
log(`${relative(process.cwd(), file)}`); | ||
log(`${relative(process.cwd(), file.filepath)}`); | ||
await runFile(file); | ||
log(); | ||
} | ||
await afterHook.fire(); | ||
const end = performance.now(); | ||
await afterAllHook.fire(); | ||
const tasks = files.reduce((acc, file) => acc.concat(file.collected.flatMap(([, tasks]) => tasks)), []); | ||
const passed = tasks.filter(i => { var _a; return !((_a = i.result) === null || _a === void 0 ? void 0 : _a.error); }); | ||
const failed = tasks.filter(i => { var _a; return (_a = i.result) === null || _a === void 0 ? void 0 : _a.error; }); | ||
log(`Passed ${passed.length} / ${tasks.length}`); | ||
if (failed.length) | ||
log(`Failed ${failed.length} / ${tasks.length}`); | ||
log(`Time ${(end - start).toFixed(2)}ms`); | ||
log(); | ||
} |
@@ -8,3 +8,3 @@ import { use as chaiUse } from 'chai'; | ||
} | ||
export declare function SnapshotPlugin(options: SnapshotOptions): ChaiPlugin; | ||
export declare function SnapshotPlugin(options: SnapshotOptions): Promise<ChaiPlugin>; | ||
export {}; |
import Snap from 'jest-snapshot'; | ||
import { after, before, beforeEach } from '../hooks'; | ||
import { afterAll, beforeEach } from '../hooks'; | ||
import { SnapshotManager } from './manager'; | ||
const { addSerializer } = Snap; | ||
let _manager; | ||
export function SnapshotPlugin(options) { | ||
export async function SnapshotPlugin(options) { | ||
const { rootDir } = options; | ||
@@ -12,9 +12,7 @@ _manager = new SnapshotManager({ | ||
}); | ||
_manager.snapshotResolver = await Snap.buildSnapshotResolver({ | ||
transform: [], | ||
rootDir, | ||
}); | ||
return function (chai, utils) { | ||
before(async () => { | ||
_manager.snapshotResolver = await Snap.buildSnapshotResolver({ | ||
transform: [], | ||
rootDir, | ||
}); | ||
}); | ||
beforeEach((task) => { | ||
@@ -28,3 +26,3 @@ var _a; | ||
}); | ||
after(() => { | ||
afterAll(() => { | ||
_manager.saveSnap(); | ||
@@ -31,0 +29,0 @@ _manager.report(); |
@@ -1,7 +0,12 @@ | ||
import { Suite } from './types'; | ||
import { Suite, TestFactory } from './types'; | ||
export declare const defaultSuite: Suite; | ||
export declare const test: (name: string, fn: () => Promise<void> | void) => void; | ||
export declare function clearContext(): void; | ||
export declare function suite(suiteName: string, factory?: (test: Suite['test']) => Promise<void> | void): Suite; | ||
export declare function suite(suiteName: string, factory?: TestFactory): Suite; | ||
export declare namespace suite { | ||
var skip: (suiteName: string, factory?: TestFactory | undefined) => Suite; | ||
var only: (suiteName: string, factory?: TestFactory | undefined) => Suite; | ||
var todo: (suiteName: string) => Suite; | ||
} | ||
export declare const describe: typeof suite; | ||
export declare const it: (name: string, fn: () => Promise<void> | void) => void; |
@@ -7,4 +7,5 @@ import { context } from './context'; | ||
defaultSuite.clear(); | ||
context.currentSuite = defaultSuite; | ||
} | ||
export function suite(suiteName, factory) { | ||
function processSuite(mode, suiteName, factory) { | ||
const queue = []; | ||
@@ -14,2 +15,3 @@ const factoryQueue = []; | ||
name: suiteName, | ||
mode, | ||
test, | ||
@@ -41,4 +43,16 @@ collect, | ||
} | ||
export function suite(suiteName, factory) { | ||
return processSuite('run', suiteName, factory); | ||
} | ||
suite.skip = function skip(suiteName, factory) { | ||
return processSuite('skip', suiteName, factory); | ||
}; | ||
suite.only = function skip(suiteName, factory) { | ||
return processSuite('only', suiteName, factory); | ||
}; | ||
suite.todo = function skip(suiteName) { | ||
return processSuite('todo', suiteName); | ||
}; | ||
// alias | ||
export const describe = suite; | ||
export const it = test; |
@@ -9,2 +9,5 @@ export interface UserOptions { | ||
} | ||
export interface TaskResult { | ||
error?: unknown; | ||
} | ||
export interface Task { | ||
@@ -15,9 +18,8 @@ name: string; | ||
file?: File; | ||
result?: TaskResult; | ||
} | ||
export interface TaskResult { | ||
task: Task; | ||
error?: unknown; | ||
} | ||
export declare type SuiteMode = 'run' | 'skip' | 'only' | 'todo'; | ||
export interface Suite { | ||
name: string; | ||
mode: SuiteMode; | ||
test: (name: string, fn: () => Promise<void> | void) => void; | ||
@@ -27,6 +29,7 @@ collect: () => Promise<Task[]>; | ||
} | ||
export declare type TestFactory = (test: Suite['test']) => Promise<void> | void; | ||
export interface File { | ||
filepath: string; | ||
suites: Suite[]; | ||
tasks: [Suite, Task[]][]; | ||
collected: [Suite, Task[]][]; | ||
} | ||
@@ -33,0 +36,0 @@ export interface GlobalContext { |
{ | ||
"name": "vitest", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"type": "module", | ||
@@ -35,11 +35,2 @@ "description": "", | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc --watch", | ||
"lint": "eslint \"{src,test}/**/*.ts\"", | ||
"prepublishOnly": "nr build", | ||
"release": "bumpp --commit --push --tag && pnpm publish", | ||
"test": "node bin/vitest.mjs --dev", | ||
"test:update": "nr test -u" | ||
}, | ||
"devDependencies": { | ||
@@ -67,3 +58,11 @@ "@antfu/eslint-config": "^0.11.1", | ||
"vite-node": "^0.1.9" | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"watch": "tsc --watch", | ||
"lint": "eslint \"{src,test}/**/*.ts\"", | ||
"release": "bumpp --commit --push --tag && pnpm publish", | ||
"test": "node bin/vitest.mjs --dev", | ||
"test:update": "nr test -u" | ||
} | ||
} | ||
} |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
32631
787
2