Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node:test support #308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the change is that 16 is EOL'ed, and I believe it doesn't support node:test.

Also added 20 and 22 to make sure it works with them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node:test is much better in 22. It was more experimental in the previous versions.

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
Loading