-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Extension mechanism (#81)
- Loading branch information
Showing
25 changed files
with
320 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { type Config } from "jest"; | ||
|
||
const jestConfig: Config = { | ||
preset: "ts-jest", | ||
setupFilesAfterEnv: ["./test/setup.ts"], | ||
testEnvironment: "node", | ||
testRegex: "test/.*\\.test\\.ts$" | ||
}; | ||
|
||
export default jestConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
{ | ||
"name": "@examples/jest", | ||
"packageManager": "yarn@3.2.2", | ||
"private": true, | ||
"scripts": { | ||
"compile": "tsc", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@examples/symbol-plugin": "workspace:^", | ||
"@stackbuilders/assertive-ts": "workspace:^", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.7.11", | ||
"jest": "^29.0.3", | ||
"ts-jest": "^29.0.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.4" | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect } from "@stackbuilders/assertive-ts"; | ||
|
||
describe("plugins", () => { | ||
it("can use the symbol plugin", () => { | ||
const foo = Symbol("foo"); | ||
|
||
expect(foo) | ||
.toBeSymbol() | ||
.toHaveDescription("foo"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SymbolPlugin } from "@examples/symbol-plugin"; | ||
import { usePlugin } from "@stackbuilders/assertive-ts"; | ||
|
||
beforeAll(() => { | ||
usePlugin(SymbolPlugin); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"extension": ["ts"], | ||
"spec": "tests/**/*.test.ts", | ||
"require": ["ts-node/register"] | ||
"spec": "test/**/*.test.ts", | ||
"require": [ | ||
"ts-node/register", | ||
"test/hooks.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { SymbolPlugin } from "@examples/symbol-plugin"; | ||
import { usePlugin } from "@stackbuilders/assertive-ts"; | ||
import { RootHookObject } from "mocha"; | ||
|
||
export function mochaHooks(): RootHookObject { | ||
return { | ||
beforeAll() { | ||
usePlugin(SymbolPlugin); | ||
} | ||
}; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect } from "@stackbuilders/assertive-ts"; | ||
|
||
describe("plugins", () => { | ||
it("can use the symbol plugin", () => { | ||
const foo = Symbol("foo"); | ||
|
||
expect(foo) | ||
.toBeSymbol() | ||
.toHaveDescription("foo"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@examples/symbol-plugin", | ||
"private": true, | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.prod.json", | ||
"compile": "tsc" | ||
}, | ||
"devDependencies": { | ||
"@stackbuilders/assertive-ts": "workspace:^", | ||
"typescript": "^4.8.4" | ||
}, | ||
"peerDependencies": { | ||
"@stackbuilders/assertive-ts": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Plugin } from "@stackbuilders/assertive-ts"; | ||
|
||
import { SymbolAssertion } from "./lib/SymbolAssertion"; | ||
|
||
declare module "@stackbuilders/assertive-ts" { | ||
|
||
export interface Expect { | ||
// tslint:disable-next-line: callable-types | ||
(actual: symbol): SymbolAssertion; | ||
} | ||
} | ||
|
||
export const SymbolPlugin: Plugin<symbol, SymbolAssertion> = { | ||
Assertion: SymbolAssertion, | ||
insertAt: "top", | ||
predicate: (actual): actual is symbol => typeof actual === "symbol" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Assertion, AssertionError } from "@stackbuilders/assertive-ts"; | ||
|
||
export class SymbolAssertion extends Assertion<symbol> { | ||
|
||
public constructor(actual: symbol) { | ||
super(actual); | ||
} | ||
|
||
public toBeSymbol(): this { | ||
const error = new AssertionError({ | ||
actual: this.actual, | ||
message: `Expected ${String(this.actual)} to be a symbol` | ||
}); | ||
const invertedError = new AssertionError({ | ||
actual: this.actual, | ||
message: `Expected ${String(this.actual)} not to be a symbol` | ||
}); | ||
|
||
return this.execute({ | ||
assertWhen: typeof this.actual === "symbol", | ||
error, | ||
invertedError | ||
}); | ||
} | ||
|
||
public toHaveDescription(expected: string): this { | ||
const error = new AssertionError({ | ||
actual: this.actual.description, | ||
expected, | ||
message: `Expected ${String(this.actual)} to have the description: ${expected}` | ||
}); | ||
const invertedError = new AssertionError({ | ||
actual: this.actual.description, | ||
message: `Expected ${String(this.actual)} not to have the description: ${expected}` | ||
}); | ||
|
||
return this.execute({ | ||
assertWhen: this.actual.description === expected, | ||
error, | ||
invertedError | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./build", | ||
}, | ||
"exclude": [ | ||
"build/*", | ||
"dist/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"incremental": false, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
import { expect } from "./lib/expect"; | ||
import { Assertion } from "./lib/Assertion"; | ||
import { config, Plugin } from "./lib/config/Config"; | ||
import { expect, Expect } from "./lib/expect"; | ||
|
||
export { expect }; | ||
export { expect as assert }; | ||
export { expect as assertThat }; | ||
export { AssertionError } from "assert/strict"; | ||
export { | ||
type AssertionFactory, | ||
type StaticTypeFactories, | ||
type TypeFactory, | ||
AssertionFactory, | ||
StaticTypeFactories, | ||
TypeFactory, | ||
TypeFactories | ||
} from "./lib/helpers/TypeFactories"; | ||
|
||
export { | ||
Assertion, | ||
Expect, | ||
Plugin, | ||
expect, | ||
expect as assert, | ||
expect as assertThat | ||
}; | ||
|
||
/** | ||
* Extends `@stackbuilders/assertive-ts` with a local or 3rd-party plugin. | ||
* | ||
* @param plugin the plugin to use to extend assertive-ts | ||
* @see {@link Plugin Plugin} | ||
*/ | ||
export function usePlugin<T, A extends Assertion<T>>(plugin: Plugin<T, A>): void { | ||
config.addPlugin(plugin); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Assertion } from "../Assertion"; | ||
|
||
export interface Plugin<T, A extends Assertion<T>> { | ||
/** | ||
* The `Assertion<T>` instance the plugin adds | ||
*/ | ||
Assertion: new(actual: T) => A; | ||
/** | ||
* The position were the predicate will test to return the `Assertion` or not: | ||
* - `top`: Test before all primitives and object-related types. | ||
* - `bottom`: Test after all primitives and object-related types. | ||
*/ | ||
insertAt: "top" | "bottom"; | ||
/** | ||
* The predicate that tests if the actual value should returnt and instance of | ||
* the plugin's `Assertion`. | ||
*/ | ||
predicate: (actual: unknown) => actual is T; | ||
} | ||
|
||
/** | ||
* A configuration class used to share a `config` instance. Useful to expose | ||
* methods that can change global settings. | ||
*/ | ||
export class Config { | ||
|
||
private pluginSet: Set<Plugin<any, Assertion<any>>>; | ||
|
||
public constructor() { | ||
this.pluginSet = new Set(); | ||
} | ||
|
||
public plugins(): ReadonlyArray<Plugin<any, Assertion<any>>> { | ||
return Array.from(this.pluginSet.values()); | ||
} | ||
|
||
public addPlugin<T, A extends Assertion<T>>(plugin: Plugin<T, A>): void { | ||
this.pluginSet.add(plugin); | ||
} | ||
} | ||
|
||
export const config = new Config(); |
Oops, something went wrong.