Skip to content

Commit

Permalink
Support file URL, fix tape error formatting
Browse files Browse the repository at this point in the history
* Fix formatting of error message, location and call stack
* Cut relative path from file URL too, not only from absolute path
* Cut the JavaScript file extension from the suite name based on file path
  • Loading branch information
prantlf committed Jun 5, 2022
1 parent 0b1fd4c commit e35a41b
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 170 deletions.
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changes

## tape 1.0.1

* Fix formatting of error message, location and call stack

## tehanu 1.0.1

* Cut relative path from file URL too, not only from absolute path
* Cut the JavaScript file extension from the suite name based on file path

## tehanu 1.0.0, teru 1.0.0, teas 1.0.0, tape 1.0.0, coco 1.0.0, tenbo: 0.0.1

* Support ESM projects in Node.js and in the browser.
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/.ncurc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reject:
test
2 changes: 1 addition & 1 deletion benchmarks/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"devDependencies": {
"ava": "^4.2.0",
"ava": "^4.3.0",
"baretest": "^2.0.0",
"jasmine": "^4.1.0",
"jest": "^28.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/coco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"prepare": "rollup -c"
},
"devDependencies": {
"rollup": "^2.74.0"
"rollup": "^2.75.5"
},
"keywords": [
"console",
Expand Down
5 changes: 2 additions & 3 deletions packages/index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ is a test suite factory function:
tehanu(name: string): (...) => void
```

If the `name` of the suite is an absolute path and it starts with the current process path (`process.cwd()`), usually supplied as `__filename`, the leading current process path will be trimmed and only the relative path to the current process path will be used. If you want to trim all the path and leave just the file name, you can use the expression `require('path').basename(__filename)` instead of just `__filename`.
If the `name` of the suite is an absolute path and it starts with the current process path (`process.cwd()`), usually supplied as `__filename`, the leading current process path will be trimmed and only the relative path to the current process path will be used. If you want to trim all the path and leave just the file name, you can use the expression `require('path').basename(__filename)` instead of just `__filename`. The same will work with `import.meta.url` in ES modules.

Calling test suite factory function will create a new test suite:

Expand Down Expand Up @@ -108,9 +108,8 @@ ESM modules are supported too:

```js
import tehanu from 'tehanu'
import { fileURLToPath } from 'url'

const test = tehanu(fileURLToPath(import.meta.url))
const test = tehanu(import.meta.url)

test('one number', () => ...)
```
Expand Down
9 changes: 6 additions & 3 deletions packages/index/lib/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ function schedule({
/* c8 ignore stop */

function suite(name) {
/* c8 ignore start */
/* c8 ignore next 8 */
if (typeof name === 'string') {
const cwd = process.cwd()
if (name.startsWith(cwd)) name = name.substring(cwd.length + 1)
const start = name.indexOf(cwd)
if (start >= 0) {
const end = name.endsWith('.js') ? 3 : name.endsWith('.mjs') || name.endsWith('.cjs') ? 4 : 0
name = name.substring(start + cwd.length + 1, name.length - end)
}
}
/* c8 ignore stop */
const tests = [], only = [], before = [], after = [], beforeEach = [], afterEach = [],
suite = (name, fn) => tests.push({ name, fn })
let bail, parallel
Expand Down
6 changes: 5 additions & 1 deletion packages/index/lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ function schedule({
function suite(name) {
if (typeof window === 'undefined' && typeof name === 'string') {
const cwd = process.cwd()
if (name.startsWith(cwd)) name = name.substring(cwd.length + 1)
const start = name.indexOf(cwd)
if (start >= 0) {
const end = name.endsWith('.js') ? 3 : name.endsWith('.mjs') || name.endsWith('.cjs') ? 4 : 0
name = name.substring(start + cwd.length + 1, name.length - end)
}
}
const tests = [], only = [], before = [], after = [], beforeEach = [], afterEach = [],
suite = (name, fn) => tests.push({ name, fn })
Expand Down
4 changes: 2 additions & 2 deletions packages/index/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tehanu",
"version": "1.0.0",
"version": "1.0.1",
"description": "Blazingly fast, tiny and simple JavaScript test harness for Node.js and browser.",
"homepage": "https://github.com/prantlf/tehanu/tree/master/packages/index#readme",
"author": {
Expand Down Expand Up @@ -59,7 +59,7 @@
},
"devDependencies": {
"c8": "^7.11.3",
"esbuild": "^0.14.39",
"esbuild": "^0.14.42",
"tehanu-repo-tape": "workspace:*"
},
"keywords": [
Expand Down
13 changes: 10 additions & 3 deletions packages/tape/lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ export function ok({ name }) {
console.log(`ok ${++count} - ${name}`)
}

export function fail({ name }, { stack }) {
export function fail({ name }, { message, stack }) {
let lines = stack.split('\n');
let at = lines.findIndex(line => line.startsWith(' at '));
stack = lines.map((line, index) => ` ${line}`).join('\n');
console.log(`not ok ${++count} - ${name}
---
message: ${message}
at: ${at > 0 && lines[at].substring(7)}
stack: |-
${stack}
`)
...
`);
}

export function bail() {
Expand Down
6 changes: 3 additions & 3 deletions packages/tape/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tehanu-repo-tape",
"version": "1.0.0",
"version": "1.0.1",
"description": "Reports the test progress of tests written with tehanu on the console using the TAP protocol format.",
"homepage": "https://github.com/prantlf/tehanu/tree/master/packages/tape#readme",
"author": {
Expand Down Expand Up @@ -40,8 +40,8 @@
"prepare": "rollup -c && cp lib/index.d.ts dist"
},
"devDependencies": {
"rollup": "^2.74.0",
"rollup-plugin-swc-minify": "^1.0.1"
"rollup": "^2.75.5",
"rollup-plugin-swc-minify": "^1.0.2"
},
"keywords": [
"tap",
Expand Down
4 changes: 2 additions & 2 deletions packages/teas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
},
"devDependencies": {
"c8": "^7.11.3",
"rollup": "^2.74.0",
"rollup-plugin-swc-minify": "^1.0.1",
"rollup": "^2.75.5",
"rollup-plugin-swc-minify": "^1.0.2",
"tehanu": "workspace:*",
"tehanu-repo-coco": "workspace:*",
"tehanu-teru": "workspace:*"
Expand Down
4 changes: 2 additions & 2 deletions packages/tenbo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"test": "node lib/index.mjs test/index.js && node lib/index.mjs -o 8013 test/index.html"
},
"dependencies": {
"colorette": "^2.0.16",
"colorette": "^2.0.17",
"polka": "^0.5.2",
"sirv": "^2.0.2",
"tiny-glob": "^0.2.9"
},
"devDependencies": {
"puppeteer": "^14.1.1",
"puppeteer": "^14.2.1",
"tehanu": "workspace:*",
"tehanu-repo-tape": "workspace:*"
},
Expand Down
Loading

0 comments on commit e35a41b

Please sign in to comment.