UNPKG

@copperjs/copper

Version:
57 lines 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const fastify_1 = require("fastify"); const errors_1 = require("./errors"); describe('errors', () => { it('should parse a CopperError to json', () => { const error = new errors_1.CopperError('message', 'code'); const json = JSON.stringify(error); chai_1.expect(json).to.equal('{"message":"message","error":"code"}'); }); it('SessionNotFound should have 404 error code', () => { const error = new errors_1.SessionNotFound('message'); chai_1.expect(error.statusCode).to.equal(404); }); it('CreateSessionError should have 500 error code', () => { const error = new errors_1.CreateSessionError('message'); chai_1.expect(error.statusCode).to.equal(500); }); it('UnsupportedActionError should have 501 error code', () => { const error = new errors_1.UnsupportedActionError('message'); chai_1.expect(error.statusCode).to.equal(501); }); it('NoMatchingNode should have 404 error code', () => { const error = new errors_1.NoMatchingNode('message'); chai_1.expect(error.statusCode).to.equal(404); }); it('WebdriverError should have 400 error code', () => { const error = new errors_1.WebdriverError('message'); chai_1.expect(error.statusCode).to.equal(400); }); it('should handle CopperError in a fastify error handler', async () => { const fastifyInstance = fastify_1.fastify(); errors_1.registerErrorHandler(fastifyInstance, {}, () => ({})); const err = new errors_1.CopperError('message', 'code', 403); fastifyInstance.get('/', () => { throw err; }); await fastifyInstance.ready(); const response = await fastifyInstance.inject({ method: 'GET', url: '/' }); chai_1.expect(response.statusCode).to.equal(403); chai_1.expect(response.body).to.equal(JSON.stringify(err)); await fastifyInstance.close(); }); it('should handle non Copper errors as 500 errors', async () => { const fastifyInstance = fastify_1.fastify(); errors_1.registerErrorHandler(fastifyInstance, {}, () => ({})); fastifyInstance.get('/', () => { throw new Error('message'); }); await fastifyInstance.ready(); const response = await fastifyInstance.inject({ method: 'GET', url: '/' }); chai_1.expect(response.statusCode).to.equal(500); await fastifyInstance.close(); }); }); //# sourceMappingURL=errors.test.js.map