UNPKG

3.46 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const Joi = require('joi');
5const pkg = require('../package.json');
6const FormData = require('form-data');
7const FrisbySpec = require('./frisby/spec.js');
8const utils = require('./frisby/utils.js');
9
10
11/**
12 * Export Frisby version from package.json
13 */
14const version = pkg.version;
15
16const _globalSetupDefaults = {
17 request: {
18 credentials: 'include',
19 headers: {
20 'Content-Type': 'application/json',
21 'User-Agent': 'frisby/' + version + ' (+https://github.com/vlucas/frisby)',
22 },
23 rawBody: false,
24 inspectOnFailure: true,
25 timeout: 5000,
26 },
27};
28let _globalSetup = _.cloneDeep(_globalSetupDefaults);
29
30/**
31 * Set global base URL for all your frisby tests
32 */
33function baseUrl(url) {
34 _.merge(_globalSetup, {
35 request: {
36 baseUrl: url,
37 },
38 });
39}
40
41/**
42 * Throw custom error for those who didn't lock their 'frisby' versions, and get v2 instead of v0.8.5... tsk tsk
43 *
44 * @throws Error
45 */
46function create(name) {
47 throw new Error(`
48 [ERROR] frisby.create() has been removed from Frisby v2.x. If you want
49 to continue using the previous latest version of Frisby 0.x, lock your
50 package.json version of frisby to "0.8.5".
51
52 To install and continue using v0.8.5 (unsupported):
53 npm install frisby@0.8.5 --save-dev
54
55 If you would like to upgrade to v2, see:
56 https://github.com/vlucas/frisby/wiki/Upgrading
57 `);
58}
59
60/**
61 * Create a new FrisbySpec test with specified name
62 */
63function createWithAction(action, args) {
64 let frisby = new module.exports.FrisbySpec();
65
66 // Use current global setup options
67 frisby.setup(_globalSetup);
68
69 // Call action with given args
70 let params = Array.prototype.slice.call(args);
71 return frisby[action].apply(frisby, params);
72}
73
74/**
75 * Create and return new FormData instance
76 */
77function formData() {
78 return new FormData();
79}
80
81/**
82 * Global setup for Frisby
83 *
84 * @param {Object} opts
85 */
86function globalSetup(opts) {
87 _globalSetup = _.merge(_.cloneDeep(_globalSetupDefaults), opts);
88}
89
90/**
91 * HTTP helpers
92 */
93function del() {
94 return createWithAction('del', arguments);
95}
96function fetch() {
97 return createWithAction('fetch', arguments);
98}
99function fromJSON() {
100 return createWithAction('fromJSON', arguments);
101}
102function get() {
103 return createWithAction('get', arguments);
104}
105function head() {
106 return createWithAction('head', arguments);
107}
108function options() {
109 return createWithAction('options', arguments);
110}
111function patch() {
112 return createWithAction('patch', arguments);
113}
114function post() {
115 return createWithAction('post', arguments);
116}
117function put() {
118 return createWithAction('put', arguments);
119}
120function setup() {
121 return createWithAction('setup', arguments);
122}
123function timeout() {
124 return createWithAction('timeout', arguments);
125}
126function use() {
127 return createWithAction('use', arguments);
128}
129
130/**
131 * Global expect handlers for custom expectations
132 */
133function addExpectHandler(expectName, expectFn) {
134 return module.exports.FrisbySpec.addExpectHandler(expectName, expectFn);
135}
136function removeExpectHandler(expectName, expectFn) {
137 return module.exports.FrisbySpec.removeExpectHandler(expectName, expectFn);
138}
139
140module.exports = {
141 addExpectHandler,
142 baseUrl,
143 create,
144 del,
145 delete: del,
146 fetch,
147 FrisbySpec,
148 formData,
149 fromJSON,
150 get: get,
151 globalSetup,
152 head,
153 Joi,
154 options,
155 patch,
156 post,
157 put,
158 removeExpectHandler,
159 setup,
160 timeout,
161 use,
162 utils,
163 version,
164};