Best JavaScript code snippet using frisby
frisby_spec.js
Source:frisby_spec.js
1'use strict';2const assert = require('assert');3const frisby = require('../src/frisby');4const mocks = require('./fixtures/http_mocks');5const testHost = 'http://api.example.com';6describe('Frisby', function() {7 it('Test expectStatus works as... well, expected', function(doneFn) {8 mocks.use(['getUser1']);9 frisby.fetch(testHost + '/users/1')10 .expect('status', 200)11 .done(doneFn);12 });13 it('should handle a 204 response with no content', function(doneFn) {14 mocks.use(['noContent']);15 frisby.fetch(testHost + '/contents/none')16 .expect('status', 204)17 .done(doneFn);18 });19 it('should handle a 204 response with no content and then()', function(doneFn) {20 mocks.use(['noContent']);21 frisby.fetch(testHost + '/contents/none')22 .expect('status', 204)23 .then((res) => {24 expect(res.body).toEqual('');25 })26 .done(doneFn);27 });28 it('should support JSON natively', function (doneFn) {29 mocks.use(['createUser2']);30 frisby.post(testHost + '/users', {31 body: {32 email: 'user@example.com',33 password: 'password'34 }35 })36 .expect('status', 201)37 .done(doneFn);38 });39 it('should allow custom expect handlers to be registered and used', function (doneFn) {40 mocks.use(['getUser1']);41 // Add our custom expect handler42 frisby.addExpectHandler('customUserResponse', function(response) {43 let json = response.json;44 expect(json.id).toBe(1);45 expect(json.email).toBe('joe.schmoe@example.com');46 });47 // Use it!48 frisby.get(testHost + '/users/1')49 .expect('customUserResponse')50 .done(doneFn);51 // Remove said custom handler52 frisby.removeExpectHandler('customUserResponse');53 });54 it('should allow custom expect functions to be used without registering them', function (doneFn) {55 mocks.use(['getUser1']);56 frisby.get(testHost + '/users/1')57 .then(function (res) {58 let json = res.json;59 expect(json.id).toBe(1);60 expect(json.email).toBe('joe.schmoe@example.com');61 })62 .done(doneFn);63 });64 it('should allow POST with empty request body', function (doneFn) {65 mocks.use(['postError']);66 frisby.post(testHost + '/error')67 .expect('status', 400)68 .expect('json', {69 result: 'error'70 })71 .done(doneFn);72 });73 it('should allow DELETE with request body', function (doneFn) {74 mocks.use(['deleteUsers']);75 frisby.delete(testHost + '/users', {76 body: {77 data: [78 {id: 2},79 {id: 3}80 ]81 }82 })83 .expect('status', 200)84 .expect('json', {85 data: [86 {id: 2},87 {id: 3}88 ]89 })90 .done(doneFn);91 });92 it('should allow DELETE with text request body', function (doneFn) {93 mocks.use(['deleteContent']);94 frisby.delete(testHost + '/contents/1', {95 headers: {96 'Content-Type': 'application/x-www-form-urlencoded'97 },98 body: 'something something'99 })100 .expect('status', 200)101 .expect('bodyContains', 'something something')102 .done(doneFn);103 });104 it('should call Frisby spec delete()', function(doneFn) {105 mocks.use(['deleteUser1']);106 frisby.setup({ request: { inspectOnFailure: false } })107 .delete(testHost + '/users/1')108 .expect('status', 204)109 .done(doneFn);110 });111 it('should use new responseBody when returning another Frisby spec from then()', function (doneFn) {112 mocks.use(['getUser1', 'getUser2WithDelay']);113 frisby.get(testHost + '/users/1')114 .expect('json', { id: 1 })115 .then(frisby.get(testHost + '/users/2')116 .expect('json', { id: 2 })117 )118 .then(function (res) {119 expect(res.json.id).toBe(2);120 })121 .done(doneFn);122 });123 it('should use new responseBody when returning another Frisby spec inside then()', function (doneFn) {124 mocks.use(['getUser1', 'getUser2WithDelay']);125 frisby.get(testHost + '/users/1')126 .expect('json', { id: 1 })127 .then(function () {128 return frisby.get(testHost + '/users/2')129 .expect('json', { id: 2 });130 })131 .then(function (res) {132 expect(res.json.id).toBe(2);133 })134 .done(doneFn);135 });136 it('should use new responseBody when returning another Frisby spec inside then() with multiple specs chained', function (doneFn) {137 mocks.use(['getUser1', 'getUser2WithDelay']);138 frisby.get(testHost + '/users/1')139 .expect('json', { id: 1 })140 .then(function () {141 mocks.use(['getUser1WithDelay']);142 return frisby.get(testHost + '/users/1')143 .expect('json', { id: 1 });144 })145 .then(function (res) {146 expect(res.json.id).toBe(1);147 })148 .then(function () {149 return frisby.get(testHost + '/users/2')150 .expect('json', { id: 2 });151 })152 .then(function (res) {153 expect(res.json.id).toBe(2);154 })155 .done(doneFn);156 });157 it('use function allows modifications for current Frisby spec', function(doneFn) {158 mocks.use(['getUser1WithAuth']);159 let withAuthHeader = function (spec) {160 spec.setup({161 request: {162 headers: { 'authorization': 'Basic Auth' }163 }164 });165 };166 frisby.use(withAuthHeader)167 .fetch(testHost + '/users/1/auth')168 .expect('status', 200)169 .done(doneFn);170 });171 it('frisby setup merges options with previous options already set', function(doneFn) {172 mocks.use(['twoHeaders']);173 // Should merge headers so both are present174 frisby.setup({175 request: {176 headers: { 'One': 'one' }177 }178 })179 .setup({180 request: {181 headers: { 'Two': 'two' }182 }183 })184 .fetch(testHost + '/two-headers')185 .expect('status', 200)186 .done(doneFn);187 });188 it('frisby setup second parameter replaces setup options instead of merging them', function(doneFn) {189 mocks.use(['getUser1WithAuth']);190 // Second call uses 'true' as 2nd argument, so it should overwrite options191 frisby.setup({192 request: {193 headers: { 'authorizationX': 'Basic AuthX' }194 }195 })196 .setup({197 request: {198 headers: { 'authorization': 'Basic Auth' }199 }200 }, true)201 .fetch(testHost + '/users/1/auth')202 .expect('status', 200)203 .done(doneFn);204 });205 it('frisby timeout is configurable per spec', function(doneFn) {206 mocks.use(['timeout']);207 // Test timeout by catching timeout error and running assertions on it208 frisby.timeout(10)209 .use(function (spec) {210 expect(spec.timeout()).toBe(10);211 })212 .fetch(testHost + '/timeout')213 .catch(function (err) {214 expect(err.name).toBe('FetchError');215 })216 .done(doneFn);217 });218 it('should allow custom headers to be set for future requests', function(doneFn) {219 mocks.use(['setCookie', 'requireCookie']);220 // Call path only221 frisby.get(testHost + '/cookies/set')222 .expect('status', 200)223 .expect('header', 'Set-Cookie')224 .then((res) => {225 let cookie1 = res.headers.get('Set-Cookie');226 return frisby.get(testHost + '/cookies/check', {227 headers: {228 'Cookie': cookie1229 }230 })231 .expect('status', 200);232 })233 .done(doneFn);234 });235 it('baseUrl sets global baseUrl to be used with all relative URLs', function(doneFn) {236 mocks.use(['getUser1']);237 // Set baseUrl238 frisby.baseUrl(testHost);239 // Call path only240 frisby.fetch('/users/1')241 .expect('status', 200)242 .done(doneFn);243 });244 it('should accept urls which include multibyte characters', function(doneFn) {245 mocks.use(['multibyte']);246 frisby.fetch(testHost + '/ããã«ã¡ã¯')247 .expect('status', 200)248 .done(doneFn);249 });250 it('should auto encode URIs that do not use fetch() with the urlEncode: false option set', function(doneFn) {251 mocks.use(['urlEncoded']);252 frisby.get(testHost + '/ftp//etc/os-release%00.md')253 .expect('status', 200)254 .done(doneFn);255 });256 it('should not encode URIs that use fetch() with the urlEncode: false option set', function(doneFn) {257 mocks.use(['notUrlEncoded']);258 frisby.fetch(testHost + '/ftp//etc/os-release%00.md', {}, { urlEncode: false })259 .expect('status', 200)260 .done(doneFn);261 });262 it('should throw an error and a deprecation warning if you try to call v0.x frisby.create()', function() {263 assert.throws(function(err) {264 // OLD style of Frisby - will not work (throws error)265 frisby.create('this will surely throw an error!')266 .expectStatus(200)267 .toss();268 }, /ERROR/i);269 });270 it('should be able to extend FrisbySpec with a custom class', function() {271 let OriginalFrisbySpec = frisby.FrisbySpec;272 class FrisbySpecExtended extends OriginalFrisbySpec {273 customMethod() {274 return true;275 }276 }277 // Have frisby use our class278 frisby.FrisbySpec = FrisbySpecExtended;279 let actual = frisby.fromJSON({}).customMethod();280 let expected = true;281 assert.equal(actual, expected);282 // Restore original FrisbySpec class283 frisby.FrisbySpec = OriginalFrisbySpec;284 });285 it('should use new responseBody when returning another Frisby spec inside catch()', function (doneFn) {286 mocks.use(['getUser1', 'getUser2WithDelay']);287 frisby.get(testHost + '/users/10')288 .expect('json', { id: 10 })289 .then(function (res) {290 fail('this function will never be called.');291 })292 .catch(function (err) {293 expect(err.name).toContain('FetchError');294 return frisby.setup({ request: { inspectOnFailure: false } })295 .get(testHost + '/users/1')296 .expect('json', { id: 10 });297 })298 .then(function (res) {299 fail('this function will never be called.');300 })301 .catch(function (err) {302 expect(err.name).toContain('AssertionError');303 return frisby.get(testHost + '/users/2')304 .expect('json', { id: 2 });305 })306 .then(function (res) {307 expect(res.json.id).toBe(2);308 })309 .done(doneFn);310 });311 it('should output invalid body and reason in error message', function(doneFn) {312 mocks.use(['invalidJSON']);313 frisby.setup({ request: { inspectOnFailure: false } })314 .get(testHost + '/res/invalid')315 .then(function (res) {316 fail('this function will never be called.');317 })318 .catch(function (err) {319 expect(err.message).toMatch(/^Invalid json response /);320 expect(err.message).toMatch(/body: '.*'/);321 expect(err.message).toMatch(/reason: '.+'/);322 })323 .done(doneFn);324 });325 it('receive response body as raw buffer', function(doneFn) {326 mocks.use(['getUser1']);327 frisby.setup({ request: { rawBody: true } })328 .get(testHost + '/users/1')329 .expect('status', 200)330 .expect('header', 'Content-Type', /json/)331 .then(res => {332 expect(res.json).toBeUndefined();333 expect(res.body).not.toBeInstanceOf(String);334 return String.fromCharCode.apply(null, new Uint8Array(res.body));335 })336 .then(text => {337 expect(text).toContain('joe.schmoe@example.com');338 })339 .done(doneFn);340 });341 it('should support http method OPTIONS', function(doneFn) {342 mocks.use(['options']);343 frisby.options(testHost + '/')344 .expect('status', 204)345 .expect('header', 'Access-Control-Allow-Methods', /GET/)346 .done(doneFn);347 });348 it('Test FrisbySpec finally', function(doneFn) {349 mocks.use(['getUser1']);350 frisby.fetch(testHost + '/users/1')351 .expect('status', 200)352 .finally(() => {353 })354 .done(doneFn);355 });...
http_mocks.js
Source:http_mocks.js
...63 id: 1,64 email: 'joe.schmoe@example.com'65 });66 },67 getUser1WithDelay() {68 return nock(mockHost)69 .get('/users/1')70 .delay(500)71 .reply(200, {72 id: 1,73 email: 'joe.schmoe@example.com'74 });75 },76 getUser2() {77 return nock(mockHost)78 .get('/users/2')79 .reply(200, {80 id: 2,81 email: 'testy.mctestface@example.com'...
Using AI Code Generation
1var frisby = require('frisby');2frisby.create('getUser1WithDelay')3 .expectStatus(200)4 .expectHeaderContains('content-type', 'application/json')5 .expectJSON({6 })7 .toss();8var frisby = require('frisby');9frisby.create('getUser2WithDelay')10 .expectStatus(200)11 .expectHeaderContains('content-type', 'application/json')12 .expectJSON({13 })14 .toss();15var frisby = require('frisby');16frisby.create('getUser3WithDelay')17 .expectStatus(200)18 .expectHeaderContains('content-type', 'application/json')19 .expectJSON({20 })21 .toss();22var frisby = require('frisby');23frisby.create('getUser4WithDelay')24 .expectStatus(200)25 .expectHeaderContains('content-type', 'application/json')26 .expectJSON({27 })28 .toss();29var frisby = require('frisby');30frisby.create('getUser5WithDelay')31 .expectStatus(200)32 .expectHeaderContains('content-type', 'application/json')33 .expectJSON({34 })35 .toss();
Using AI Code Generation
1var frisby = require('frisby');2frisby.create('Test GET Request')3 .expectStatus(200)4 .expectHeaderContains('content-type', 'application/json')5 .expectJSONTypes({6 })7 .toss();8var frisby = require('frisby');9frisby.create('Test GET Request')10 .expectStatus(200)11 .expectHeaderContains('content-type', 'application/json')12 .expectJSONTypes({13 })14 .toss();15- [Jasmine-node](
Using AI Code Generation
1describe('GET User', function() {2 it('should return a user with a delay', function (done) {3 .expect('status', 200)4 .expect('json', 'id', 1)5 .expect('json', 'firstName', 'John')6 .expect('json', 'lastName', 'Doe')7 .expect('json', 'email', '
Using AI Code Generation
1var frisby = require('frisby');2var config = require('./config.js');3var getUser1WithDelay = require('./getUser1WithDelay.js');4frisby.create('GET request to /api/users/1')5 .get(config.baseURL + '/api/users/1')6 .expectStatus(200)7 .expectHeaderContains('content-type', 'application/json')8 .expectJSONTypes({9 data: {10 }11 })12 .expectJSON({13 data: {14 }15 })16.toss();17module.exports = {18}19module.exports = function (frisby) {20 var config = require('./config.js');21 return frisby.create('GET request to /api/users/1 with delay')22 .get(config.baseURL + '/api/users/1', {23 })24 .expectStatus(200)25 .expectHeaderContains('content-type', 'application/json')26 .expectJSONTypes({27 data: {28 }29 })30 .expectJSON({31 data: {32 }33 })34 .toss();35};
Using AI Code Generation
1frisby.create('getUser1WithDelay')2 .afterJSON(function(response){3 console.log(response);4 })5.toss();6frisby.create('getUser1WithDelay')7 .afterJSON(function(response){8 console.log(response);9 })10.toss();11frisby.create('getUser1WithDelay')12 .afterJSON(function(response){13 console.log(response);14 })15.toss();16frisby.create('getUser1WithDelay')17 .afterJSON(function(response){18 console.log(response);19 })20.toss();21frisby.create('getUser1WithDelay')22 .afterJSON(function(response){23 console.log(response);24 })25.toss();26frisby.create('getUser1WithDelay')27 .afterJSON(function(response){28 console.log(response);29 })30.toss();31frisby.create('getUser1WithDelay')
Using AI Code Generation
1var frisby = require('frisby');2frisby.create('Get User')3 .get(api_url + '/1')4 .expectStatus(200)5 .expectHeaderContains('content-type', 'application/json')6 .expectJSON({7 })8 .toss();9frisby.create('Get All Users')10 .get(api_url)11 .expectStatus(200)12 .expectHeaderContains('content-type', 'application/json')13 .expectJSONTypes('?', {14 })15 .toss();16frisby.create('Get User with delay')17 .get(api_url + '/1/delay')18 .expectStatus(200)19 .expectHeaderContains('content-type', 'application/json')20 .expectJSON({21 })22 .toss();23frisby.create('Get All Users with delay')24 .get(api_url + '/delay')25 .expectStatus(200)26 .expectHeaderContains('content-type', 'application/json')27 .expectJSONTypes('?', {28 })29 .toss();30frisby.create('Get User with error')31 .get(api_url + '/1/error')32 .expectStatus(500)33 .expectHeaderContains('content-type', 'application/json')34 .expectJSON({35 })36 .toss();37frisby.create('Get All Users with error')38 .get(api_url + '/error')39 .expectStatus(500)40 .expectHeaderContains('content-type', 'application/json')41 .expectJSON({42 })43 .toss();44frisby.create('Get User with error and delay')45 .get(api_url + '/1/error/delay')46 .expectStatus(500)47 .expectHeaderContains('
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!