All Projects → enova → givens

enova / givens

Licence: MIT license
Easy test setup without side effects.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to givens

jest-wrap
Fluent pluggable interface for easily wrapping `describe` and `it` blocks in Jest tests.
Stars: ✭ 35 (+59.09%)
Mutual labels:  mocha, javascript-tests, jest-tests
Nspec
A battle hardened testing framework for C# that's heavily inspired by Mocha and RSpec.
Stars: ✭ 242 (+1000%)
Mutual labels:  mocha, rspec
Teaspoon
Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
Stars: ✭ 1,443 (+6459.09%)
Mutual labels:  mocha, javascript-tests
mocha-wrap
Fluent pluggable interface for easily wrapping `describe` and `it` blocks in Mocha tests.
Stars: ✭ 54 (+145.45%)
Mutual labels:  mocha, javascript-tests
playwright-test
Run unit tests with several runners or benchmark inside real browsers with playwright.
Stars: ✭ 81 (+268.18%)
Mutual labels:  mocha
mocha-allure2-example
Allure 2 Mocha examples
Stars: ✭ 18 (-18.18%)
Mutual labels:  mocha
Approvals.NodeJS
Approval Tests implementation in NodeJS
Stars: ✭ 79 (+259.09%)
Mutual labels:  mocha
react-native-unit-tests
Example how to test React Native components
Stars: ✭ 79 (+259.09%)
Mutual labels:  mocha
react-typescript
React16 + HMR + typescript + webpack + tslint + tests
Stars: ✭ 21 (-4.55%)
Mutual labels:  jest-tests
react-testing-mocha-chai-enzyme
A solid test setup for React components with Mocha, Chai, Sinon, Enzyme in a Webpack/Babel application.
Stars: ✭ 48 (+118.18%)
Mutual labels:  mocha
mache
A library for writing cleaner and more expressive acceptance tests using page objects.
Stars: ✭ 40 (+81.82%)
Mutual labels:  rspec
rspec-tap-formatters
TAP Producer for RSpec-3
Stars: ✭ 20 (-9.09%)
Mutual labels:  rspec
vue-webpack-boilerplate
A webpack boilerplate with vue-loader, axios, vue-router and vuex
Stars: ✭ 51 (+131.82%)
Mutual labels:  mocha
react-typescript-jest-enzyme-testing
Testing React.JS + TypeScript component with Jest and Enzyme. A simple example for reference.
Stars: ✭ 53 (+140.91%)
Mutual labels:  jest-tests
template-server-nodejs
No description or website provided.
Stars: ✭ 20 (-9.09%)
Mutual labels:  mocha
currency-api
A demo project on how to test a node/express app with Mocha, Nock and proxyquire (MNP) and code coverage with nyc/istanbul.
Stars: ✭ 19 (-13.64%)
Mutual labels:  mocha
turbo tests
Run RSpec tests on multiple cores. Like parallel_tests but with incremental summarized output. Originally extracted from the Discourse and Rubygems source code.
Stars: ✭ 81 (+268.18%)
Mutual labels:  rspec
node-url-shortener
URL Shortener in Base58 using Node.js, Express, Sequelize, Mocha and Bootstrap
Stars: ✭ 21 (-4.55%)
Mutual labels:  mocha
saharspec
RSpec sugar to DRY your specs
Stars: ✭ 58 (+163.64%)
Mutual labels:  rspec
typeplate
REST API boilerplate with Typescript, Express.js, Typeorm and Mocha.
Stars: ✭ 268 (+1118.18%)
Mutual labels:  mocha

givens

npm GitHub Workflow Status GitHub Workflow Status MIT License

Easy test setup without side effects.

For use with jest, mocha, and jasmine. Behavior based on rspec, syntax inspired by given2


Why?

If you or your team have ruby and rspec experience, and are moving to a javascript based library, this will make the experience of writing javascript tests very similar to that of writing rspec tests.

Even if you don't come from a ruby background, givens can help you write maintainable tests quickly, while avoiding many of the side effects traditional JavaScript testing often has, such as:

  • testing different methods on the same object in multiple tests can result in cross-contamination.
  • tests can depend on order; and break when reordered.
  • Running only some tests in a single file (like via jest’s .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 beforeEach and afterEach.


What does it do?

when you call getGiven(), givens calls afterEach with a callback that clears the cache. Because this afterEach hook is declared first, it will run after every other afterEach in every scope.

when you call given(key, callback), givens calls the following lifecycle functions:

  • beforeAll, with a hook which pushes callback onto a stack of functions for key and makes sure there is an accessor present for key, which simply calls the topmost function on the stack and returns the result.
  • afterAll, with a hook which pops callback back off the stack of functions for key, and deletes the accessor if the stack is now empty.

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:

npm install --save-dev givens

or

yarn add --dev givens

global installation

import 'givens/setup';

or add to testing framework config, for example in jest:

{
  setupFilesAfterEnv: [ // this CANNOT be setupFiles
    'givens/setup.js',
  ],
}

then you can use the given keyword without importing.

local installation

in the test file, use the following import:

import getGiven from 'givens';
const given = getGiven();

or in 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, allowing you to override a given value for a set of tests. The cache is cleared after every test.

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).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].