Skip to content

Commit

Permalink
Conditional exports
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Jun 19, 2024
1 parent acc94f7 commit 94caefe
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 122 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/jsr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Install dependencies
run: bun i --frozen-lockfile
- name: Build package
run: bun run build
- name: Publish package
run: npx jsr publish
run: npx jsr publish
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
/node_modules
/dist
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit",
"quickfix.biome": "explicit"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
49 changes: 49 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.1/schema.json",
"organizeImports": {
"enabled": true
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"semicolons": "asNeeded",
"bracketSpacing": false,
"trailingComma": "none",
"arrowParentheses": "asNeeded"
}
},
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 80,
"lineEnding": "lf",
"ignore": []
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noConstructorReturn": "off"
},
"style": {
"noNonNullAssertion": "off",
"useEnumInitializers": "off",
"noParameterAssign": "off",
"useShorthandFunctionType": "off"
},
"complexity": {
"noBannedTypes": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noConstEnum": "off",
"noAssignInExpressions": "off",
"noConfusingLabels": "off",
"noExportsInTest": "off"
}
}
}
}
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[install.scopes]
"@jsr" = "https://npm.jsr.io"
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"#suite": "./dist/suite.deno.js"
}
}
9 changes: 6 additions & 3 deletions jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "@benmerckx/suite",
"version": "0.2.0",
"exports": "./mod.ts"
}
"version": "0.3.0",
"exports": "./jsr.ts",
"publish": {
"exclude": ["!dist"]
}
}
10 changes: 10 additions & 0 deletions jsr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type {Suite} from './src/suite'

/** Define a test suite */
export const suite: Suite = (
await ('Bun' in globalThis
? import('./src/suite.bun.js')
: 'Deno' in globalThis
? import('./dist/suite.deno.js')
: import('./src/suite.node.js'))
).suite
5 changes: 0 additions & 5 deletions mod.ts

This file was deleted.

27 changes: 21 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
{
"version": "0.2.0",
"version": "0.3.0",
"name": "@benmerckx/suite",
"module": "suite.js",
"types": "suite.d.ts",
"type": "module",
"files": [
"suite.js",
"suite.d.ts"
],
"files": ["./dist", "./src"],
"devDependencies": {
"@jsr/std__assert": "^0.225.2",
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"scripts": {
"build": "bun build src/suite.deno.js --outdir=dist --minify",
"test": "node --test && deno test && bun test",
"deploy": "npm publish && npx jsr publish"
},
"publishConfig": {
"access": "public"
},
"exports": {
".": {
"deno": "./dist/suite.deno.js",
"bun": "./src/suite.bun.js",
"node": "./src/suite.node.js",
"types": "./src/suite.d.ts"
}
},
"imports": {
"#suite": {
"deno": "./dist/suite.deno.js",
"bun": "./src/suite.bun.js",
"node": "./src/suite.node.js",
"types": "./src/suite.d.ts"
}
}
}
}
22 changes: 22 additions & 0 deletions src/suite.bun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function setup(meta) {
// @ts-ignore
const native = Bun.jest(meta.path)
const test = native.test.bind()
test.skip = native.test.skip
test.only = native.test.only
test.ok = a => native.expect(a).toBeTruthy()
test.is = (a, b) => native.expect(a).toBe(b)
test.equal = (a, b) => native.expect(a).toEqual(b)
test.throws = (fn, err) =>
err === undefined
? native.expect(fn).toThrow()
: native.expect(fn).toThrow(err)
test.not = {
ok: a => native.expect(a).toBeFalsy(),
is: (a, b) => native.expect(a).not.toBe(b),
equal: (a, b) => native.expect(a).not.toEqual(b)
}
return test
}

export const suite = (meta, define) => define(setup(meta))
68 changes: 34 additions & 34 deletions suite.d.ts → src/suite.d.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
/** Describe a test */
export type Describe =
(name: string, run: ()=> void | Promise<void>) => void

/** Define a test suite */
export interface DefineTest extends Describe {
/** Skip the test */
skip: Describe
/** Only run this test */
only: Describe
/** Assert that actual is a truthy value */
ok(actual: any): void
/** Assert that actual strictly equals (===) the expects value */
is(actual: any, expects: any): void
/** Assert that actual is deeply equal to the expects value */
equal(actual: any, expects: any): void
/** Assert that the fn function throws an Error */
throws(fn: () => void, messageIncludes?: string): void
/** Assert inverse */
not: {
/** Assert that actual is a falsy value */
ok(actual: any): void
/** Assert that actual does not strictly equal (===) the expects value */
is(actual: any, expects: any): void
/** Assert that actual is not deeply equal to the expects value */
equal(actual: any, expects: any): void
}
}

/** Define a test suite */
export type Suite =
(meta: ImportMeta, define: (test: DefineTest) => void) => void

/** Define a test suite */
/** Describe a test */
export type Describe = (name: string, run: () => void | Promise<void>) => void

/** Define a test suite */
export interface DefineTest extends Describe {
/** Skip the test */
skip: Describe
/** Only run this test */
only: Describe
/** Assert that actual is a truthy value */
ok(actual: any): void
/** Assert that actual strictly equals (===) the expects value */
is(actual: any, expects: any): void
/** Assert that actual is deeply equal to the expects value */
equal(actual: any, expects: any): void
/** Assert that the fn function throws an Error */
throws(fn: () => void, messageIncludes?: string): void
/** Assert inverse */
not: {
/** Assert that actual is a falsy value */
ok(actual: any): void
/** Assert that actual does not strictly equal (===) the expects value */
is(actual: any, expects: any): void
/** Assert that actual is not deeply equal to the expects value */
equal(actual: any, expects: any): void
}
}

/** Define a test suite */
export interface Suite {
(meta: ImportMeta, define: (test: DefineTest) => void): void
// runtime: 'node' | 'deno' | 'bun'
}

export const suite: Suite
26 changes: 26 additions & 0 deletions src/suite.deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
assert,
assertEquals,
assertFalse,
assertNotEquals,
assertNotStrictEquals,
assertStrictEquals,
assertThrows
} from '../node_modules/@jsr/std__assert/mod.js'

// @ts-ignore
const native = Deno.test
const test = native.bind()
test.skip = native.ignore
test.only = native.only
test.ok = assert
test.is = assertStrictEquals
test.equal = assertEquals
test.throws = assertThrows
test.not = {
ok: assertFalse,
is: assertNotStrictEquals,
equal: assertNotEquals
}

export const suite = (meta, define) => define(test)
17 changes: 17 additions & 0 deletions src/suite.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as asserts from 'node:assert'
import * as native from 'node:test'

const test = native.test.bind()
test.skip = native.skip
test.only = native.only
test.ok = asserts.ok
test.is = asserts.strictEqual
test.equal = asserts.deepStrictEqual
test.throws = asserts.throws
test.not = {
ok: a => asserts.ok(!a),
is: asserts.notStrictEqual,
equal: asserts.notDeepStrictEqual
}

export const suite = (meta, define) => define(test)
68 changes: 0 additions & 68 deletions suite.js

This file was deleted.

6 changes: 3 additions & 3 deletions suite.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {suite} from './suite.js'
import {suite} from '#suite'

suite(import.meta, (test) => {
suite(import.meta, test => {
test('truthy', () => {
test.ok(true)
})
Expand Down Expand Up @@ -48,4 +48,4 @@ suite(import.meta, (test) => {
/*test.only('only', () => {
test.ok(true)
})*/
})
})

0 comments on commit 94caefe

Please sign in to comment.