Comparing version 1.1.3 to 1.2.0
@@ -1,1 +0,10 @@ | ||
export default function getContextInfo(): 'normal' | 'lifecycle' | 'test'; | ||
interface AllowedContextInfo { | ||
allowed: true; | ||
} | ||
interface DisallowedContextInfo { | ||
allowed: false; | ||
message: string; | ||
} | ||
declare type contextInfo = AllowedContextInfo | DisallowedContextInfo; | ||
export default function getContextInfo(ssf: Function): contextInfo; | ||
export {}; |
@@ -5,22 +5,56 @@ "use strict"; | ||
if (/Object\.asyncJestLifecycle/.test(rawStack)) { | ||
return 'lifecycle'; | ||
return { | ||
allowed: false, | ||
message: 'cannot call givens from a lifecycle hook', | ||
}; | ||
} | ||
if (/Object\.asyncJestTest/.test(rawStack)) { | ||
return 'test'; | ||
return { | ||
allowed: false, | ||
message: 'cannot call givens from a test', | ||
}; | ||
} | ||
return 'defer'; | ||
if (/jest-jasmine2/.test(rawStack)) { | ||
return { allowed: true }; | ||
} | ||
return undefined; | ||
} | ||
function mochaContextMatcher(rawStack) { | ||
if (/Test\.Runnable\.run/.test(rawStack)) { | ||
return 'test'; | ||
return { | ||
allowed: false, | ||
message: 'cannot call givens from a test', | ||
}; | ||
} | ||
if (/Hook\.Runnable\.run/.test(rawStack)) { | ||
return 'lifecycle'; | ||
return { | ||
allowed: false, | ||
message: 'cannot call givens from a lifecycle hook', | ||
}; | ||
} | ||
return 'defer'; | ||
if (/Mocha/.test(rawStack)) { | ||
return { allowed: true }; | ||
} | ||
return undefined; | ||
} | ||
function getContextInfo() { | ||
function jasmineContextMatcher(rawStack) { | ||
if (/jasmine\.js/.test(rawStack)) { | ||
if (!/Env\.describe/.test(rawStack)) { | ||
return { | ||
allowed: false, | ||
message: 'given must be called inside a describe', | ||
}; | ||
} | ||
return { allowed: true }; | ||
} | ||
return undefined; | ||
} | ||
function getContextInfo(ssf) { | ||
let rawStack; | ||
try { | ||
throw new Error(); | ||
const err = new Error(); | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(err, ssf); | ||
} | ||
throw err; | ||
} | ||
@@ -32,11 +66,15 @@ catch (e) { | ||
context = jestContextMatcher(rawStack); | ||
if (context !== 'defer') { | ||
if (context !== undefined) { | ||
return context; | ||
} | ||
context = mochaContextMatcher(rawStack); | ||
if (context !== 'defer') { | ||
if (context !== undefined) { | ||
return context; | ||
} | ||
return 'normal'; | ||
context = jasmineContextMatcher(rawStack); | ||
if (context !== undefined) { | ||
return context; | ||
} | ||
return { allowed: true }; | ||
} | ||
exports.default = getContextInfo; |
@@ -9,5 +9,5 @@ "use strict"; | ||
const given = (key, func) => { | ||
const contextInfo = getContextInfo_1.default(); | ||
if (contextInfo === 'lifecycle' || contextInfo === 'test') { | ||
throw new givenError_1.default('cannot call given from test or lifecycle method', given); | ||
const contextInfo = getContextInfo_1.default(given); | ||
if (!contextInfo.allowed) { | ||
throw new givenError_1.default(contextInfo.message, given); | ||
} | ||
@@ -14,0 +14,0 @@ if (!isValid_1.default(key)) { |
{ | ||
"name": "givens", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Easy test setup without side effects", | ||
@@ -17,2 +17,3 @@ "main": "dist/getGiven.js", | ||
"install:tests:mocha": "cd integration-tests/mocha && npm i --no-package-lock", | ||
"install:tests:jasmine": "cd integration-tests/jasmine && npm i --no-package-lock", | ||
"build": "tsc --removeComments", | ||
@@ -24,2 +25,3 @@ "build:tests": "tsc --inlineSourceMap", | ||
"lint:mocha": "cd integration-tests/mocha && npm run lint", | ||
"lint:jasmine": "cd integration-tests/jasmine && npm run lint", | ||
"test": "npm-run-all --continue-on-error test:**", | ||
@@ -29,2 +31,3 @@ "test:unit": "jest", | ||
"test:mocha": "cd integration-tests/mocha && npm test", | ||
"test:jasmine": "cd integration-tests/jasmine && npm test", | ||
"clean": "./clean.sh" | ||
@@ -31,0 +34,0 @@ }, |
@@ -5,7 +5,7 @@ <p align="center"> | ||
<br /> | ||
<img alt="npm" src="https://img.shields.io/npm/v/givens"> | ||
<a href='https://www.npmjs.com/package/givens'><img alt="npm" src="https://img.shields.io/npm/v/givens"></a> | ||
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/enova/givens/CD?label=build"> | ||
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/enova/givens/CI?label=tests"> | ||
<a href='https://coveralls.io/github/enova/givens?branch=master'><img src='https://coveralls.io/repos/github/enova/givens/badge.svg?branch=master' alt='Coverage Status' /></a> | ||
<img alt="GitHub" src="https://img.shields.io/github/license/enova/givens"> | ||
<a href='https://github.com/enova/givens/blob/master/LICENSE'><img alt="MIT License" src="https://img.shields.io/github/license/enova/givens"></a> | ||
</p> | ||
@@ -15,3 +15,3 @@ | ||
For use with [jest](https://github.com/facebook/jest) and [mocha](https://github.com/mochajs/mocha). | ||
For use with [jest](https://github.com/facebook/jest), [mocha](https://github.com/mochajs/mocha), and [jasmine](https://github.com/jasmine/jasmine.github.io). | ||
Behavior based on [rspec](https://github.com/enova/givens/blob/master), syntax inspired by [given2](https://github.com/tatyshev/given2) | ||
@@ -18,0 +18,0 @@ |
16055
252