test('resolves only once', async () => { const deferred = new Deferred<string>(); deferred.resolve('foo'); try { deferred.resolve('bar'); assert.fail(); } catch (e) { // pass } try { deferred.reject(new Error('bar')); assert.fail(); } catch (e) { // pass } });
it(`an inline or anonymous interface must be explicitly annotated with ObjectSchema()`, function () { try { class ClassToValidate { @Keys({ nestedProperty: Joi.number() }) public myProperty! : { nestedProperty : number; }; } assert.fail(); } catch (err) { if (!(err instanceof ConstraintDefinitionError)) { throw err; } assert.equal(err.message, "No validation schema exists, nor could it be inferred from the design:type metadata, for property \"myProperty\". Please decorate the property with a type schema."); } const validator = new Validator(); class ClassToValidate2 { @Keys({ nestedProperty: Joi.number() }) @ObjectSchema() public myProperty! : { nestedProperty : number; }; } const instance = new ClassToValidate2(); instance.myProperty = { nestedProperty: 123 }; isValid(validator, instance); });
it('should get the next game in a circular loop', () => { const countsPerGame: Dict<number> = {}; const firstGameName: GameName = 'piano-game'; let currentGameName: GameName = firstGameName; let loopCount = 0; while (true) { currentGameName = getNextGame(currentGameName); // Track the number of times we see each game so we can ensure that it circles back. countsPerGame[currentGameName] = (countsPerGame[currentGameName] || 0) + 1; // Break when we circle back to the first game if (currentGameName === firstGameName) { break; } // Sanity check the loop count in case the exit condition never hits. loopCount++; if (loopCount > 999) { assert.fail('Looped way too many times - getting the next game name should be circular.'); break; } } assert( values(countsPerGame).every((count: number) => count === 1), 'should have looped through each game once' ); assert.equal( Object.keys(countsPerGame).length, 3, 'expected to loop through exactly 3 game names' ); });
function checkNoOpHandler<T>(requestInfo: RequestInfo<T>): void { try { requestInfo.handler(fakeXhrIo({}), ''); } catch (e) { assert.fail('Remove handler threw'); } }
it('creates externs, adds type comments and rewrites imports', () => { const diagnostics: ts.Diagnostic[] = []; const closure = toClosureJS( {sourceMap: true, experimentalDecorators: true} as ts.CompilerOptions, ['test_files/underscore/export_underscore.ts', 'test_files/underscore/underscore.ts'], {isTyped: true}, diagnostics); if (!closure) { diagnostics.forEach(v => console.log(JSON.stringify(v))); assert.fail(); return; } expect(closure.externs).to.contain(`/** @const */ var __NS = {}; /** @type {number} */ __NS.__ns1; `); const underscoreDotJs = closure.jsFiles.get('test_files/underscore/underscore.js'); expect(underscoreDotJs).to.contain(`goog.module('test_files.underscore.underscore')`); expect(underscoreDotJs).to.contain(`/** @type {string} */`); const exportUnderscoreDotJs = closure.jsFiles.get('test_files/underscore/export_underscore.js'); expect(exportUnderscoreDotJs) .to.contain(`goog.module('test_files.underscore.export_underscore')`); });
it ('Can authenticate with BearerToken', async () => { var request = new Authenticate(); request.provider = "credentials"; request.userName = "******"; request.password = "******"; var authResponse = await testClient.post(request); var jwtToken = authResponse.bearerToken; chai.expect(jwtToken).not.empty; //Clear existing User Session var logout = new Authenticate(); logout.provider = "logout"; await testClient.post(logout); var newClient = new JsonServiceClient(TEST_URL); try { //New Client without BearerToken should fail await newClient.post(new Authenticate()); chai.assert.fail(0,1, "Should not be allowed to authenticate"); } catch (e) { if (e instanceof chai.AssertionError) throw e; const status = (e as ErrorResponse).responseStatus; chai.expect(status.errorCode).to.equal("401"); chai.expect(status.message).to.equal("Unauthorized"); //New Client with BearerToken newClient.bearerToken = jwtToken; var success = await newClient.post(new Authenticate()); chai.expect(success.userId).not.empty; chai.expect(success.sessionId).not.empty; } })
it("should verify ParseXml - invalid xml input", async function() { try { await CommandHelper.ParseXml("<?xml><<<<"); assert.fail("didn't throw!"); } catch (err) { assert.isTrue(err.message.startsWith("Unexpected end")); } });
export function assertError(fn: () => any, code: string) { try { fn(); } catch (e) { assert.equal(e.code, code); return; } assert.fail(); }
helper.dataDrivenTest(tests, function(data, expect) { try { parse(data); } catch (e) { assert.match(e.message, expect); return; } assert.fail(undefined, undefined, "No exception thrown."); });
it("should throw an error if the same actionNamespace is used twice", function () { new CachedDataReducer<Request, Response>(apiEndpointMock, "duplicatenamespace"); try { new CachedDataReducer<Request, Response>(apiEndpointMock, "duplicatenamespace"); } catch (e) { assert(_.isError(e)); return; } assert.fail("Expected to fail after registering a duplicate actionNamespace."); });