Skip to content

Commit

Permalink
build: refactor to use deno (#30)
Browse files Browse the repository at this point in the history
* chore: move test/index.js to mod_test.ts

* chore: move test/typings.ts to typings_test.ts

* test: format typings_test.ts with deno

* test: refactor mod_test.ts to use Deno test

* fix(AssertionError): make stack argument optional

* build: use deno in build scripts

* test: drop test index.html

* build: add .tool-versions

* build: add coverage folder to gitignore

* build: add index.js to gitignore

* style: deno fmt

* build: drop dist files from package.json

* style: deno fmt md files

* style: deno fmt package*

* build: move dist/mod.d.ts to index.d.ts

* refactor: manually write index.d.ts for now

Refs: denoland/deno#3385

* build: fix npm files

* test: temporarily pass the typings tests

* build: add deno ci

* build: use denoland/setup-deno

* style: deno fmt

* build: remove tsconfig

Not needed for Deno

* build: remove broken coverage assertions

* build: remove node build

* style: fix all lint errors

* fix(AssertionError): captureStackTrace inside consturctor

* build: use deno lint

* style: deno fmt

* style: NAUGHTY DENO
  • Loading branch information
keithamus committed Oct 25, 2021
1 parent 6a0cf50 commit 08a1f16
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 436 deletions.
44 changes: 0 additions & 44 deletions .github/workflows/nodejs.yml

This file was deleted.

42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test Deno Module

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
deno-version: [1.15.2, canary]

steps:
- name: Git sources
uses: actions/checkout@v2

- name: Use Deno Version ${{ matrix.deno-version }}
uses: denoland/[email protected]
with:
deno-version: ${{ matrix.deno-version }}

- name: Deno Check Fmt
run: deno fmt --check

- name: Deno Lint
run: deno lint

- name: Build Deno Module
run: deno bundle --reload mod.ts > index.js

- name: Test Deno Module
run: deno test --coverage=coverage

# TODO: coverage broken see https://github.com/denoland/deno/issues/11875
# - name: Generate lcov
# run: deno coverage coverage --lcov > coverage/lcov.info

# - name: Assert coverage is good
# uses: VeryGoodOpenSource/[email protected]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ npm-debug.log

coverage.html
dist
coverage
index.js
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deno 1.15.2
31 changes: 14 additions & 17 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
1.1.0 / 2018-01-02
==================
# 1.1.0 / 2018-01-02

* Add type definitions ([#11](https://github.com/chaijs/assertion-error/pull/11))
- Add type definitions
([#11](https://github.com/chaijs/assertion-error/pull/11))

1.0.1 / 2015-03-04
==================
# 1.0.1 / 2015-03-04

* Merge pull request #2 from simonzack/master
* fixes `.stack` on firefox
- Merge pull request #2 from simonzack/master
- fixes `.stack` on firefox

1.0.0 / 2013-06-08
==================
# 1.0.0 / 2013-06-08

* readme: change travis and component urls
* refactor: [*] prepare for move to chaijs gh org
- readme: change travis and component urls
- refactor: [*] prepare for move to chaijs gh org

0.1.0 / 2013-04-07
==================
# 0.1.0 / 2013-04-07

* test: use vanilla test runner/assert
* pgk: remove unused deps
* lib: implement
* "Initial commit"
- test: use vanilla test runner/assert
- pgk: remove unused deps
- lib: implement
- "Initial commit"
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ Both `AssertionError` and `AssertionResult` implement the `Result` interface:

```typescript
interface Result {
name: 'AssertionError' | 'AssertionResult'
ok: boolean
toJSON(...args: unknown[]): Record<string, unknown>
name: "AssertionError" | "AssertionResult";
ok: boolean;
toJSON(...args: unknown[]): Record<string, unknown>;
}
```

So if a function returns `AssertionResult | AssertionError` it is easy to check
_which_ one is returned by checking either `.name` or `.ok`, or check `instanceof Error`.
_which_ one is returned by checking either `.name` or `.ok`, or check
`instanceof Error`.

## Installation

Expand All @@ -56,8 +57,12 @@ $ npm install --save assertion-error

### Deno

`assertion_error` is available on [Deno.land](https://deno.land/x/assertion_error)
`assertion_error` is available on
[Deno.land](https://deno.land/x/assertion_error)

```typescript
import {AssertionError, AssertionResult} from 'https://deno.land/x/[email protected]/mod.ts'
import {
AssertionError,
AssertionResult,
} from "https://deno.land/x/[email protected]/mod.ts";
```
27 changes: 27 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
interface Result {
name: "AssertionError" | "AssertionResult";
ok: boolean;
toJSON(...args: unknown[]): Record<string, unknown>;
}

declare class AssertionError<T> extends Error implements Result {
[key: string]: unknown
name: "AssertionError";
ok: false;
message: string;
// deno-lint-ignore ban-types
constructor(message: string, props?: T, ssf?: Function);
stack: string;
toJSON(stack?: boolean): Record<string, unknown>;
}

declare class AssertionResult<T> implements Result {
[key: string]: unknown
name: "AssertionResult";
ok: true;
message: string;
constructor(props?: T);
toJSON(): Record<string, unknown>;
}

export { AssertionError, AssertionResult, Result };
61 changes: 31 additions & 30 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
const canElideFrames = 'captureStackTrace' in Error
const startStackFrames = new WeakMap()
import { Result } from "./index.d.ts";

interface Result {
name: 'AssertionError' | 'AssertionResult'
ok: boolean
toJSON(...args: unknown[]): Record<string, unknown>
}
type V8Error = ErrorConstructor & {
// deno-lint-ignore ban-types
captureStackTrace(err: Error, ssf: Function): void;
};

const canElideFrames = "captureStackTrace" in Error;

export class AssertionError<T> extends Error implements Result {
[key: string]: unknown

get name(): 'AssertionError' {
return 'AssertionError'
get name(): "AssertionError" {
return "AssertionError";
}

get ok() {
return false
return false;
}

constructor(public message = 'Unspecified AssertionError', props?: T, ssf?: Function) {
super(message)
if (canElideFrames && ssf) startStackFrames.set(this, ssf)
constructor(
public message = "Unspecified AssertionError",
props?: T,
// deno-lint-ignore ban-types
ssf?: Function,
) {
super(message);
if (canElideFrames) {
(Error as V8Error).captureStackTrace(
this,
ssf || AssertionError,
);
}
for (const key in props) {
if (!(key in this)) {
// @ts-ignore
// @ts-ignore: allow arbitrary assignment of values onto class
this[key] = props[key];
}
}
}

get stack() {
if (canElideFrames) {
return (Error as any).captureStackTrace(this, startStackFrames.get(this) || AssertionError);
} else {
return super.stack
}
}

toJSON(stack: boolean): Record<string, unknown> {
toJSON(stack?: boolean): Record<string, unknown> {
return {
...this,
name: this.name,
message: this.message,
ok: false,
// include stack if exists and not turned off
stack: stack !== false ? this.stack : undefined,
}
};
}

}

export class AssertionResult<T> implements Result {
[key: string]: unknown

get name(): 'AssertionResult' {
return 'AssertionResult'
get name(): "AssertionResult" {
return "AssertionResult";
}

get ok() {
return true
return true;
}

constructor(props?: T) {
for (const key in props) {
if (!(key in this)) {
// @ts-ignore
// @ts-ignore: allow arbitrary assignment of values onto class
this[key] = props[key];
}
}
Expand All @@ -75,6 +76,6 @@ export class AssertionResult<T> implements Result {
...this,
name: this.name,
ok: this.ok,
}
};
}
}
Loading

0 comments on commit 08a1f16

Please sign in to comment.