Skip to content

Commit

Permalink
node:test support (#308)
Browse files Browse the repository at this point in the history
* Update the ci to run in the maintained versions of node

* Remove unused stuff from the node:test example

* Update the node:test example with a better reporter

node:test's default reporter is really limted, and doesn't print the
errors that earl generates in a nice way. I replaced it with a better
one, which behaves like mocha.

* Fix stack trace test

* Use glob -c in the node:test example

* Fix file:// URLs handling

* Fix example plugin

* Add changeset
  • Loading branch information
alcuadrado authored Jun 10, 2024
1 parent de7b3bd commit 530bf06
Show file tree
Hide file tree
Showing 15 changed files with 737 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-ghosts-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"earl": minor
---

Add support for node:test
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
CI:
strategy:
matrix:
node: ["16.x", "18.x"]
node: ["18.x", "20.x", "22.x"]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}

Expand Down
7 changes: 0 additions & 7 deletions examples/example-node-runner/.mocharc.js

This file was deleted.

8 changes: 6 additions & 2 deletions examples/example-node-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
"lint": "biome check ./src",
"lint:fix": "biome check --apply ./src",
"typecheck": "tsc --noEmit",
"test": "node --test --loader ts-node/esm src/*.test.ts",
"test": "glob -c \"node --test --test-reporter=@voxpelli/node-test-pretty-reporter --loader ts-node/esm\" \"src/*.test.ts\"",
"test:fix": "pnpm lint:fix && pnpm format:fix && pnpm test && pnpm typecheck"
},
"dependencies": {
"earl": "workspace:^1.2.1",
"example-plugin": "workspace:^1.0.0"
"example-plugin": "workspace:^"
},
"devDependencies": {
"@voxpelli/node-test-pretty-reporter": "^1.1.1",
"glob": "^10.3.15"
}
}
23 changes: 23 additions & 0 deletions examples/example-node-runner/src/example-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it } from 'node:test'
import { expect } from 'earl'
import 'example-plugin'

describe('example-plugin', () => {
it('EvenNumberMatcher works', () => {
expect(2).toEqual(expect.evenNumber())
})

it('EvenNumberMatchers is type safe', () => {
// @ts-expect-error - type mismatch
expect('2').not.toEqual(expect.evenNumber())
})

it('toBeEven works', () => {
expect(2).toBeEven()
})

it('toBeEven is type safe', () => {
// @ts-expect-error - type mismatch
expect('foo').not.toBeEven()
})
})
1 change: 1 addition & 0 deletions examples/example-plugin/cjs-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
13 changes: 8 additions & 5 deletions examples/example-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"private": true,
"version": "1.0.0",
"license": "MIT",
"main": "./dist/index.js",
"module": "./dist/index.js",
"type": "module",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.js"
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
Expand All @@ -19,7 +20,9 @@
"lint:fix": "biome check --apply ./src",
"typecheck": "tsc --noEmit",
"clean": "rimraf dist tsconfig.tsbuildinfo",
"build": "pnpm clean && tsc --build",
"build": "pnpm run clean && pnpm run build:esm && pnpm run build:cjs",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json && cp cjs-package.json dist/cjs/package.json",
"test:fix": "pnpm lint:fix && pnpm format:fix && pnpm typecheck"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions examples/example-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import './evenNumber'
import './toBeEven'
import './evenNumber.js'
import './toBeEven.js'
10 changes: 10 additions & 0 deletions examples/example-plugin/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node",
"verbatimModuleSyntax": false,
"outDir": "dist/cjs"
},
"exclude": ["dist", "src/test", "src/**/*.test.ts"]
}
4 changes: 4 additions & 0 deletions examples/example-plugin/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["dist", "src/test", "src/**/*.test.ts"]
}
23 changes: 19 additions & 4 deletions examples/example-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true,

"strict": true,
"noImplicitOverride": true,
"noUncheckedIndexedAccess": true,

"sourceMap": true,
"declaration": true,
"declarationMap": true,

"skipLibCheck": true,

"outDir": "dist/esm"
},
"include": ["src"]
"exclude": ["dist"]
}
3 changes: 2 additions & 1 deletion packages/earl/src/errors/AssertionError.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from 'node:url'
import ErrorStackParser from 'error-stack-parser'

interface AssertionErrorOptions {
Expand Down Expand Up @@ -34,7 +35,7 @@ export class AssertionError extends Error {
parsed = parsed ?? ErrorStackParser.parse({ stack: cleaned } as Error)
const fileName = parsed[0]?.fileName
if (fileName?.startsWith('file://')) {
return fileName.slice(7)
return fileURLToPath(fileName)
}
return fileName
},
Expand Down
3 changes: 2 additions & 1 deletion packages/earl/src/errors/stack-traces.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fileURLToPath } from 'node:url'
import { expect } from 'chai'
import ErrorStackParser from 'error-stack-parser'

Expand Down Expand Up @@ -41,6 +42,6 @@ describe('stack traces for errors', () => {
return nestedGetControl()
}
const control = nestedValidator()
expect(control.file).to.equal(import.meta.url)
expect(control.file).to.equal(fileURLToPath(import.meta.url))
})
})
5 changes: 5 additions & 0 deletions packages/earl/src/validators/snapshots/TestContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fileURLToPath } from 'node:url'

export type TestContext = NodeTestContext | MochaTestContext | UvuTestContext

export interface NodeTestContext {
Expand All @@ -20,6 +22,9 @@ export function getTestFile(context: TestContext): string | undefined {
if ('test' in context) {
const file = context.test?.file
if (typeof file === 'string') {
if (file.startsWith('file://')) {
return fileURLToPath(file)
}
return file
}
}
Expand Down
Loading

0 comments on commit 530bf06

Please sign in to comment.