Best JavaScript code snippet using root
server-integration.test.js
Source:server-integration.test.js
...33 ['tester']34 ])('%j connects first, and then disconnects', async (role) => {35 const webSocket = new FakeWebSocket({ remotePort: 8081 });36 sessionManager.registerConnection(webSocket, webSocket.socket);37 expect(getLoggerMsg('debug', 0, 1)).toMatchSnapshot();38 expect(sessionManager.getSession(webSocket)).toBe(null);39 webSocket.mockLogin({ messageId: 100500, role });40 expect(sessionManager.registerSession).toHaveBeenCalledWith(expect.any(DetoxConnection), {41 role,42 sessionId: 'aSession',43 });44 const [[detoxConnection]] = sessionManager.registerSession.mock.calls;45 const detoxSession = sessionManager.getSession(detoxConnection);46 expect(detoxSession).toBeInstanceOf(DetoxSession);47 expect(webSocket.send).toHaveBeenCalledWith(aMessage({48 type: 'loginSuccess',49 messageId: 100500,50 params: {51 testerConnected: false,52 appConnected: false,53 [role + 'Connected']: true,54 },55 }));56 webSocket.mockClose();57 expect(sessionManager.unregisterConnection).toHaveBeenCalledWith(webSocket);58 expect(sessionManager.getSession(detoxConnection)).toBe(null);59 });60 test('tester and app interconnect and then disconnect', async () => {61 let testerSocket = new FakeWebSocket({ remotePort: 0x7 });62 let appSocket = new FakeWebSocket({ remotePort: 0xA });63 sessionManager.registerConnection(testerSocket, testerSocket.socket);64 testerSocket.mockLogin({ role: 'tester' });65 expect(testerSocket.send).toHaveBeenCalledWith(aMessage({66 type: 'loginSuccess',67 params: {68 testerConnected: true,69 appConnected: false,70 },71 }));72 sessionManager.registerConnection(appSocket, appSocket.socket);73 appSocket.mockLogin({ role: 'app' });74 expect(appSocket.send).toHaveBeenCalledWith(aMessage({75 type: 'loginSuccess',76 params: {77 testerConnected: true,78 appConnected: true,79 },80 }));81 expect(testerSocket.send).toHaveBeenCalledWith(aMessage({ type: 'appConnected' }));82 // app will disconnect83 const [[testerConnection], [appConnection]] = sessionManager.registerSession.mock.calls;84 const detoxSession = sessionManager.getSession(appConnection);85 expect(detoxSession).toBeInstanceOf(DetoxSession);86 appSocket.mockClose();87 expect(testerSocket.send).toHaveBeenCalledWith(aMessage({ type: 'appDisconnected' }));88 expect(sessionManager.getSession(appConnection)).toBe(null); // because the app is disconnected89 expect(sessionManager.getSession(testerConnection)).toBe(detoxSession); // because the tester is still connected90 testerSocket.mockMessage({91 type: 'cleanup',92 messageId: 100,93 });94 // assert tester get a stub cleanup message if the app is not connected95 expect(testerSocket.send).toHaveBeenCalledWith(aMessage({96 type: 'cleanupDone',97 messageId: 10098 }));99 testerSocket.mockMessage({100 type: 'reactNativeReload',101 messageId: 101,102 });103 // assert tester get a serverError explaining the app is unreachable104 expect(testerSocket.send).toHaveBeenCalledWith(expect.stringContaining('"type":"serverError"'));105 expect(testerSocket.send).toHaveBeenCalledWith(expect.stringContaining('Detox can\'t seem to connect to the test app'));106 testerSocket.send.mockReset();107 // app reconnects108 appSocket = new FakeWebSocket({ remotePort: 0xB });109 sessionManager.registerConnection(appSocket, appSocket.socket);110 appSocket.mockLogin({ role: 'app' });111 expect(testerSocket.send).toHaveBeenCalledWith(aMessage({ type: 'appConnected' }));112 // tester sends to app113 const reloadAction = { type: 'reactNativeReload', messageId: 1000 };114 testerSocket.mockMessage(reloadAction);115 expect(appSocket.send).toHaveBeenCalledWith(aMessage(reloadAction));116 // app sends back to tester117 const readyAction = { type: 'ready', messageId: 1000 };118 appSocket.mockMessage(readyAction);119 expect(testerSocket.send).toHaveBeenCalledWith(aMessage(readyAction));120 // tester disconnects121 testerSocket.mockClose();122 expect(appSocket.send).toHaveBeenCalledWith(aMessage({ type: 'testerDisconnected', messageId: -1 }));123 expect(sessionManager.getSession(testerConnection)).toBe(null);124 appSocket.mockMessage({125 type: 'currentStatus',126 messageId: 200,127 params: {128 status: 'I am fine',129 }130 });131 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });132 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot('CANNOT_FORWARD');133 // app disconnects134 appSocket.mockClose();135 });136 describe('edge cases', () => {137 let webSocket;138 beforeEach(() => {139 webSocket = new FakeWebSocket({ remotePort: 0xA });140 });141 test('attempt to register the same connection twice', () => {142 sessionManager.registerConnection(webSocket, webSocket.socket);143 expect(logger.error).not.toHaveBeenCalled();144 sessionManager.registerConnection(webSocket, webSocket.socket);145 expect(getLoggerMsg('error')).toMatchSnapshot();146 });147 test('attempt to unregister an unknown connection', () => {148 sessionManager.unregisterConnection(webSocket);149 expect(getLoggerMsg('error')).toMatchSnapshot();150 });151 test('unregistering an anomymous connection', () => {152 sessionManager.registerConnection(webSocket, webSocket.socket);153 sessionManager.unregisterConnection(webSocket);154 expect(logger.error).not.toHaveBeenCalled();155 sessionManager.unregisterConnection(webSocket);156 expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Cannot unregister'));157 });158 test('on(message) - malformed data', () => {159 sessionManager.registerConnection(webSocket, webSocket.socket);160 webSocket.mockMessage(Buffer.alloc(0));161 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });162 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();163 });164 test('on(message) - no .type', () => {165 sessionManager.registerConnection(webSocket, webSocket.socket);166 webSocket.mockMessage({ some: 'message' });167 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });168 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();169 });170 test('login - empty .params', () => {171 sessionManager.registerConnection(webSocket, webSocket.socket);172 webSocket.mockMessage({ type: 'login' });173 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });174 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();175 });176 test('login - invalid .role', () => {177 sessionManager.registerConnection(webSocket, webSocket.socket);178 webSocket.mockMessage({ type: 'login', params: { sessionId: 'Session', role: 'Meteora' } });179 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });180 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();181 });182 test('login - missing .sessionId', () => {183 sessionManager.registerConnection(webSocket, webSocket.socket);184 webSocket.mockMessage({ type: 'login', params: { sessionId: '', role: 'tester' } });185 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });186 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();187 });188 test('login - non-string .sessionId', () => {189 sessionManager.registerConnection(webSocket, webSocket.socket);190 webSocket.mockMessage({ type: 'login', params: { sessionId: { 0: 2 }, role: 'tester' } });191 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });192 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();193 });194 test('login twice (as tester)', () => {195 sessionManager.registerConnection(webSocket, webSocket.socket);196 webSocket.mockLogin({ role: 'tester' });197 webSocket.mockLogin({ role: 'tester' });198 expect(webSocket.send).toHaveBeenCalledWith(expect.stringContaining('"type":"serverError"'));199 expect(webSocket.send).toHaveBeenCalledWith(expect.stringContaining('Cannot log in twice'));200 });201 test('login twice (as tester) + socket send error', () => {202 sessionManager.registerConnection(webSocket, webSocket.socket);203 webSocket.mockLogin({ role: 'tester' });204 webSocket.send.mockImplementationOnce(() => {205 throw Object.assign(new Error('TestError'), { stack: '' });206 });207 webSocket.mockLogin({ role: 'tester' });208 expect(webSocket.send).toHaveBeenCalledWith(expect.stringContaining('"type":"serverError"'));209 expect(getLoggerMsg('error', 0)).toMatchSnapshot();210 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();211 });212 test('login twice (as app)', () => {213 sessionManager.registerConnection(webSocket, webSocket.socket);214 webSocket.mockLogin({ role: 'app' });215 webSocket.mockLogin({ role: 'app' });216 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });217 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();218 });219 test('.registerSession - calling twice', () => {220 sessionManager.registerConnection(webSocket, webSocket.socket);221 webSocket.mockLogin({ role: 'app' });222 const [[detoxConnection]] = sessionManager.registerSession.mock.calls;223 const priorDetoxSession = sessionManager.getSession(detoxConnection);224 const newDetoxSession = sessionManager.registerSession(detoxConnection, { role: 'app', sessionId: '10101' });225 expect(priorDetoxSession === newDetoxSession).toBe(true); // assert no new sessions were created226 expect(getLoggerMsg('error', 0)).toMatchSnapshot();227 });228 test('receiving an action before we login', () => {229 sessionManager.registerConnection(webSocket, webSocket.socket);230 webSocket.mockMessage({ type: 'reloadReactNative', messageId: -1000 });231 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'ERROR' });232 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();233 });234 test('app dispatches "ready" action before login', async () => {235 sessionManager.registerConnection(webSocket, webSocket.socket);236 webSocket.mockMessage({ type: 'ready', messageId: -1000 });237 expect(getLoggerMsg('debug', 1)).toMatchSnapshot();238 });239 test('socket error', () => {240 sessionManager.registerConnection(webSocket, webSocket.socket);241 const err = new Error('Test error');242 delete err.stack;243 webSocket.mockError(err);244 expect(getLoggerMsg('warn', 0, 0)).toEqual({ event: 'WSS_SOCKET_ERROR' });245 expect(getLoggerMsg('warn', 0, 1)).toMatchSnapshot();246 });247 });248 function getLoggerMsg(level, callIndex = 0, argIndex = 0) {249 return logger[level].mock.calls[callIndex][argIndex];250 }251 function aMessage(obj) {252 return expect.stringContaining(JSON.stringify(obj));253 }...
middlewares.spec.js
Source:middlewares.spec.js
...78 test('should obfuscate full body of any endpoint', done => {79 makeRequest(server({ obfuscateBody: true }), 'post')80 .send({ secure: 'secretValue' })81 .end(() => {82 expect(getLoggerMsg()).toContain('[SECURE]');83 expect(getLoggerMsg()).not.toContain('secretValue');84 done();85 });86 });87 test('should obfuscate full body of specific endpoint', done => {88 makeRequest(server({ obfuscateBody: { [testUrl]: { POST: true } } }), 'post')89 .send({ secure: 'secretValue' })90 .end(() => {91 expect(getLoggerMsg()).toContain('[SECURE]');92 expect(getLoggerMsg()).not.toContain('secretValue');93 done();94 });95 });96 });97 describe('express id middleware', () => {98 const server = createServer(expressRequestIdMiddleware());99 test('should assign fresh requestId', done => {100 makeRequest(server).end((_, res) => {101 expect(res.header['x-request-id']).not.toBeUndefined();102 done();103 });104 });105 test('should assign requestId sent in header', done => {106 makeRequest(server)...
Using AI Code Generation
1var logger = require('./logger').getLoggerMsg("test");2logger.debug("this is a debug message");3logger.info("this is a info message");4logger.warn("this is a warn message");5logger.error("this is a error message");6logger.fatal("this is a fatal message");7var logger = require('./logger').getLoggerMsg("test","childLogger");8logger.debug("this is a debug message");9logger.info("this is a info message");10logger.warn("this is a warn message");11logger.error("this is a error message");12logger.fatal("this is a fatal message");13var logger = require('./logger').getLoggerObj("test");14logger.debug("this is a debug message");15logger.info("this is a info message");16logger.warn("this is a warn message");17logger.error("this is a error message");18logger.fatal("this is a fatal message");19var logger = require('./logger').getLoggerObj("test","childLogger");20logger.debug("this is a debug message");21logger.info("this is a info message");22logger.warn("this is a warn message");23logger.error("this is a error message");24logger.fatal("this is a fatal message");
Using AI Code Generation
1const logger = require('./logger');2logger.getLoggerMsg('info','This is an info message from test.js');3logger.getLoggerMsg('error','This is an error message from test.js');4logger.getLoggerMsg('warn','This is a warn message from test.js');5logger.getLoggerMsg('debug','This is a debug message from test.js');6var log4js = require('log4js');7log4js.configure({8 appenders: { cheese: { type: 'file', filename: 'logs/cheese.log' } },9 categories: { default: { appenders: ['cheese'], level: 'error' } }10});11const logger = log4js.getLogger('cheese');12function getLoggerMsg(level,message){13 logger[level](message);14}15module.exports.getLoggerMsg = getLoggerMsg;16const logger = require('./logger');17logger.getLoggerMsg('info','This is an info message from test.js');18logger.getLoggerMsg('error','This is an error message from test.js');19logger.getLoggerMsg('warn','This is a warn message from test.js');20logger.getLoggerMsg('debug','This is a debug message from test.js');21var log4js = require('log4js');22log4js.configure({23 appenders: { cheese: { type: 'file', filename: 'logs/cheese.log' } },24 categories: { default: { appenders: ['cheese'], level: 'error' } }25});26const logger = log4js.getLogger('cheese');27function getLoggerMsg(level,message){28 logger[level](message);29}
Using AI Code Generation
1const rootLogger = require('./rootLogger');2rootLogger.getLoggerMsg('test.js', 'Test message');3const childLogger = require('./childLogger');4childLogger.getLoggerMsg('test.js', 'Test message');5const customLogger = require('./customLogger');6customLogger.getLoggerMsg('test.js', 'Test message');
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!!