Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mason Boeman committed Jan 16, 2020
0 parents commit a020804
Show file tree
Hide file tree
Showing 33 changed files with 7,032 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# don't ever lint node_modules
node_modules
# don't lint build output (make sure it's set to your correct build folder name)
dist
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
17 changes: 17 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": [
"hbenl.vscode-test-explorer",
"connorshea.vscode-test-explorer-status-bar",
"hbenl.vscode-mocha-test-adapter",
"rtbenfield.vscode-jest-test-adapter",
"dbaeumer.vscode-eslint"
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": [

]
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"editor.tabSize": 2,
"mochaExplorer.files": [
"test/integration/mocha/**.test.js"
],
"eslint.enable": true,
"eslint.validate": [
"javascript",
"typescript"
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Mason Boeman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<p align="center">
<img src="/documentation/logo-light.png" alt="givens" width="300">
</p>

Easy test setup without side effects.

For use with [jest](https://github.com/facebook/jest) and [mocha](https://github.com/mochajs/mocha).
Behavior based on [rspec](), syntax inspired by [given2](https://github.com/tatyshev/given2)

Common testing side effects include but are not limited to:
- testing different methods on the same object in multiple tests can result in cross-contamination.
- tests can depend on order; and break when reordered.
- appending `.skip` or `.only` can make tests behave unpredictably.

Givens, when used correctly solves all of these, dries up your tests, and might even make them more readable, while still letting you use standard lifecycle methods like `beforeEach`.

---
## Getting Started
### Prerequisites
This package should work fine for any javascript or typescript project that uses jest or mocha tests. Non Node environments are untested, and may not work.

### Installing
first, install from npm:
```bash
npm install --save-dev givens
```
or
```bash
yarn add --dev givens
```
#### global installation
```javascript
import 'givens/setup';
```
or add to testing framework config, for example in jest:
```javascript
{
setupFiles: [
'givens/setup.js',
],
}
```
then you can use the `given` keyword without importing.
#### local installation
in the test file, use the following import:
```javascript
import getGiven from 'givens';
const given = getGiven();
```
or in typescript
```typescript
import getGiven from 'givens';
interface myVars {
var1: number; // the keys you intend to use
var2: string; // and their types
}
const given = getGiven<myVars>();
```

---
## Usage

When you call `given(myKey, callback)`, givens stores the callback function you give it. When you go to retrieve the key (via `given.myKey`) givens will execute the most recent callback you have given for the key, and cache the value. Additionally, if you call `given()` inside a describe, the callback will revert to the previous one given for `myKey` after executing all tests in the describe. The cache is cleared after every test.

```javascript
describe('basic overriding behavior', () => {
given('var1', () => 'initial value');
given('var2', () => {
return 'the value is: ' + given.var1;
});

it('has initial value', () => {
expect(given.var2).toEqual('the value is: initial value');
});

describe('with new value', () => {
given('var1', () => 'overridden value');

it('has overridden value', () => {
expect(given.var2).toEqual('the value is: overridden value');
});
});

it('has initial value again', () => {
expect(given.var2).toEqual('the value is: initial value');
});
});
```

For more examples of usage for your particular setup, read through the integration tests. They are examples of how to use the library as intended (and in some cases how it's not supposed to be used).
Binary file added documentation/logo-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/logo-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/logo.psd
Binary file not shown.
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
verbose: true,
roots: [
'<rootDir>/test/unit/jest',
'<rootDir>/test/integration/jest',
],
setupFiles: [
'./setup.js',
],
};
Loading

0 comments on commit a020804

Please sign in to comment.