This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ceb-inversion-testing-mocha): add an implementation for Mocha
- Loading branch information
Showing
9 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,22 @@ | ||
# @tmorin/ceb-inversion-testing-core | ||
|
||
[![npm version](https://badge.fury.io/js/%40tmorin%2Fceb-inversion.svg)](https://badge.fury.io/js/%40tmorin%2Fceb-inversion-testing-core) | ||
[![api](https://img.shields.io/badge/-api-informational.svg)](https://tmorin.github.io/ceb/api/modules/_tmorin_ceb_inversion_testing_core.html) | ||
|
||
> The package is part of the `<ceb/>` library. | ||
> It provides a model to test IOC containers. | ||
## Install | ||
|
||
The NPM package is compliant [CommonJs](https://flaviocopes.com/commonjs) and [ES Module](https://flaviocopes.com/es-modules). | ||
|
||
```bash | ||
npm install @tmorin/ceb-inversion-testing-core | ||
``` | ||
|
||
## License | ||
|
||
Released under the [MIT license]. | ||
|
||
[Custom Elements (v1)]: https://html.spec.whatwg.org/multipage/custom-elements.html | ||
[MIT license]: http://opensource.org/licenses/MIT |
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,62 @@ | ||
{ | ||
"name": "@tmorin/ceb-inversion-testing-mocha", | ||
"version": "5.0.0", | ||
"license": "MIT", | ||
"description": "The package is part of the `<ceb/>` library. It provides an implementation of the testing model for IOC containers based on mocha.", | ||
"keywords": [ | ||
"custom-element-builder", | ||
"custom-elements-v1", | ||
"custom-elements", | ||
"custom-element", | ||
"typescript", | ||
"typescript-library", | ||
"javascript-library", | ||
"inversion-of-control", | ||
"ioc", | ||
"injection", | ||
"testing", | ||
"mocha" | ||
], | ||
"homepage": "https://tmorin.github.io/ceb", | ||
"bugs": { | ||
"url": "https://github.com/tmorin/ceb/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:tmorin/ceb.git" | ||
}, | ||
"author": { | ||
"name": "Thibault Morin", | ||
"url": "https://tmorin.github.io" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/mjs/index.js", | ||
"require": "./dist/cjs/index.js" | ||
} | ||
}, | ||
"types": "dist/types/index.d.ts", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/mjs/index.js", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npm run build:lib && npm run build:module && npm run build:fix", | ||
"build:fix": "../../scripts/fix-dist.js", | ||
"build:lib": "tsc -p tsconfig.build.json --module CommonJS --outDir dist/cjs --declaration --declarationDir dist/types", | ||
"build:module": "tsc -p tsconfig.build.json --module ESNext --outDir dist/mjs", | ||
"test": "mocha --require ts-node/register src/**/*.spec.ts", | ||
"test:watch": "npm test -- --watch --watch-files '**/*.ts'" | ||
}, | ||
"dependencies": { | ||
"@tmorin/ceb-inversion": "^5.0.0", | ||
"@tmorin/ceb-inversion-testing-core": "^5.0.0" | ||
}, | ||
"peerDependencies": { | ||
"mocha": "*" | ||
} | ||
} |
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,35 @@ | ||
import {SinonSpy, spy} from "sinon"; | ||
import {TestScenarioBuilder, TestSuiteBuilder} from "@tmorin/ceb-inversion-testing-core"; | ||
import {MochaTestSuiteExecutorBuilder} from "./index"; | ||
import assert from "assert"; | ||
|
||
describe("inversion/testing/mocha", function () { | ||
const spyList = new Set<SinonSpy>() | ||
const createSyp = () => { | ||
const aSpy: SinonSpy = spy() | ||
spyList.add(aSpy) | ||
return aSpy | ||
} | ||
|
||
const scenario = TestScenarioBuilder.get("test scenario") | ||
.configure(createSyp()) | ||
.before(createSyp()) | ||
.execute(createSyp()) | ||
.after(createSyp()) | ||
.build() | ||
|
||
const suite = TestSuiteBuilder.get("test suite") | ||
.scenario(scenario) | ||
.build() | ||
|
||
MochaTestSuiteExecutorBuilder.get(suite) | ||
.before(createSyp()) | ||
.configure(createSyp()) | ||
.after(createSyp()) | ||
.test() | ||
.afterAll(() => { | ||
for (const aSpy of spyList) { | ||
assert.ok(aSpy) | ||
} | ||
}) | ||
}) |
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,127 @@ | ||
import {Suite} from "mocha" | ||
import { | ||
AbstractTestSuiteExecutor, | ||
AbstractTestSuiteExecutorBuilder, | ||
Callback, | ||
CallbackWithContainerBuilder, | ||
ContainerBuilderProvider, | ||
TestSuite, | ||
TestSuiteBuilder, | ||
TestSuiteExecutor | ||
} from "@tmorin/ceb-inversion-testing-core" | ||
import {Container, ContainerBuilder} from "@tmorin/ceb-inversion"; | ||
|
||
/** | ||
* An implementation of {@link AbstractTestSuiteExecutor} dedicated for Mocha. | ||
* | ||
* Instances should be created using the builder {@link MochaTestSuiteExecutorBuilder}. | ||
*/ | ||
export class MochaTestSuiteExecutor extends AbstractTestSuiteExecutor<Suite> { | ||
constructor( | ||
suite: TestSuite, | ||
before?: Callback, | ||
provider?: ContainerBuilderProvider, | ||
configurer?: CallbackWithContainerBuilder, | ||
after?: Callback, | ||
) { | ||
super(suite, before, provider, configurer, after) | ||
} | ||
|
||
execute(): Suite { | ||
return describe(this.suite.name, () => { | ||
for (const scenario of this.suite.scenarios) { | ||
describe(scenario.name, () => { | ||
let containerBuilder: ContainerBuilder | ||
let container: Container | ||
before("before scenario", async () => { | ||
if (this.before) { | ||
await this.before() | ||
} | ||
}) | ||
before("create the container builder", async () => { | ||
containerBuilder = this.provide() | ||
}) | ||
before("configure the builder with executor configuration", async () => { | ||
if (this.configure) { | ||
await Promise.resolve(this.configure(containerBuilder)) | ||
} | ||
}) | ||
before("configure the builder with scenario configuration", async () => { | ||
if (scenario.configure) { | ||
await Promise.resolve(scenario.configure(containerBuilder)) | ||
} | ||
}) | ||
before("initialize the container", async () => { | ||
container = await containerBuilder.build().initialize() | ||
}) | ||
before("before execution", async () => { | ||
if (scenario.before) { | ||
await scenario.before(container) | ||
} | ||
}) | ||
it("execution", async () => { | ||
await scenario.execute(container) | ||
}) | ||
after("after execution", async () => { | ||
if (scenario.after) { | ||
await scenario.after(container) | ||
} | ||
}) | ||
after("dispose the container", async () => { | ||
await container.dispose() | ||
}) | ||
after("after scenario", async () => { | ||
if (this.after) { | ||
await this.after() | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* An extension of {@link AbstractTestSuiteExecutorBuilder} dedicated for Mocha. | ||
* | ||
* @example run a test suite with an dummy scenario | ||
* ```typescript | ||
* import {TestScenarioBuilder, TestSuiteBuilder} from "@tmorin/ceb-inversion-testing-core"; | ||
* import {MochaTestSuiteExecutor} from "@tmorin/ceb-inversion-testing-mocha" | ||
* MochaTestSuiteExecutorBuilder.get( | ||
* TestSuiteBuilder.get("test suite").scenario( | ||
* TestScenarioBuilder.get("test scenario") | ||
* ) | ||
* ).test() | ||
* ``` | ||
*/ | ||
export class MochaTestSuiteExecutorBuilder extends AbstractTestSuiteExecutorBuilder<Suite> { | ||
|
||
protected constructor(suite: TestSuite) { | ||
super(suite); | ||
} | ||
|
||
/** | ||
* Create a builder. | ||
* @param suite the suite | ||
*/ | ||
static get(suite: TestSuite | TestSuiteBuilder) { | ||
if (suite instanceof TestSuiteBuilder) { | ||
return new MochaTestSuiteExecutorBuilder(suite.build()) | ||
} | ||
return new MochaTestSuiteExecutorBuilder(suite) | ||
} | ||
|
||
/** | ||
* Build the executor. | ||
*/ | ||
build(): TestSuiteExecutor<Suite> { | ||
return new MochaTestSuiteExecutor( | ||
this._suite, | ||
this._before, | ||
this._provider, | ||
this._configurer, | ||
this._after, | ||
); | ||
} | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"include": [ | ||
"src" | ||
] | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"src" | ||
] | ||
} |