-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polish: add tests for more testUtils
adds tests for expectPromise and expectEqualPromisesOrValues
- Loading branch information
Showing
5 changed files
with
169 additions
and
45 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/__testUtils__/__tests__/expectEqualPromisesOrValues-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { expectEqualPromisesOrValues } from '../expectEqualPromisesOrValues'; | ||
import { expectPromise } from '../expectPromise'; | ||
|
||
describe('expectEqualPromisesOrValues', () => { | ||
it('throws when given unequal values', () => { | ||
expect(() => expectEqualPromisesOrValues([{}, {}, { test: 'test' }])).throw( | ||
"expected { test: 'test' } to deeply equal {}", | ||
); | ||
}); | ||
|
||
it('does not throw when given equal values', () => { | ||
const testValue = { test: 'test' }; | ||
expect(() => | ||
expectEqualPromisesOrValues([testValue, testValue, testValue]), | ||
).not.to.throw(); | ||
}); | ||
|
||
it('does not throw when given equal promises', async () => { | ||
const testValue = Promise.resolve({ test: 'test' }); | ||
|
||
await expectPromise( | ||
expectEqualPromisesOrValues([testValue, testValue, testValue]), | ||
).toResolve(); | ||
}); | ||
|
||
it('throws when given unequal promises', async () => { | ||
await expectPromise( | ||
expectEqualPromisesOrValues([ | ||
Promise.resolve({}), | ||
Promise.resolve({}), | ||
Promise.resolve({ test: 'test' }), | ||
]), | ||
).toRejectWith("expected { test: 'test' } to deeply equal {}"); | ||
}); | ||
|
||
it('throws when given equal values that are mixtures of values and promises', () => { | ||
const testValue = { test: 'test' }; | ||
expect(() => | ||
expectEqualPromisesOrValues([testValue, Promise.resolve(testValue)]), | ||
).to.throw('Received an invalid mixture of promises and values.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { expectPromise } from '../expectPromise'; | ||
|
||
describe('expectPromise', () => { | ||
it('throws if passed a value', () => { | ||
expect(() => expectPromise({})).to.throw( | ||
"Expected a promise, received '{}'", | ||
); | ||
}); | ||
|
||
it('toResolve returns the resolved value', async () => { | ||
const testValue = {}; | ||
const promise = Promise.resolve(testValue); | ||
expect(await expectPromise(promise).toResolve()).to.equal(testValue); | ||
}); | ||
|
||
it('toRejectWith throws if the promise does not reject', async () => { | ||
try { | ||
await expectPromise(Promise.resolve({})).toRejectWith( | ||
'foo', | ||
); /* c8 ignore start */ | ||
} /* c8 ignore stop */ catch (err) { | ||
expect(err.message).to.equal( | ||
"Promise should have rejected with message 'foo', but resolved as '{}'", | ||
); | ||
} | ||
}); | ||
|
||
it('toRejectWith throws if the promise rejects with the wrong reason', async () => { | ||
try { | ||
await expectPromise(Promise.reject(new Error('foo'))).toRejectWith('bar'); /* c8 ignore start */ | ||
} /* c8 ignore stop */ catch (err) { | ||
expect(err.message).to.equal( | ||
"expected Error: foo to have property 'message' of 'bar', but got 'foo'", | ||
); | ||
} | ||
}); | ||
|
||
it('toRejectWith does not throw if the promise rejects with the right reason', async () => { | ||
try { | ||
await expectPromise(Promise.reject(new Error('foo'))).toRejectWith( | ||
'foo', | ||
); /* c8 ignore start */ | ||
} catch (err) { | ||
// Not reached. | ||
expect.fail('promise threw unexpectedly'); | ||
} /* c8 ignore stop */ | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { assert } from 'chai'; | ||
|
||
import { isPromise } from '../jsutils/isPromise'; | ||
import type { PromiseOrValue } from '../jsutils/PromiseOrValue'; | ||
|
||
import { expectJSON } from './expectJSON'; | ||
|
||
export function expectEqualPromisesOrValues<T>( | ||
items: ReadonlyArray<PromiseOrValue<T>>, | ||
): PromiseOrValue<T> { | ||
const remainingItems = items.slice(); | ||
const firstItem = remainingItems.shift(); | ||
|
||
if (isPromise(firstItem)) { | ||
if (remainingItems.every(isPromise)) { | ||
return Promise.all(items).then(expectMatchingValues); | ||
} | ||
} else if (remainingItems.every((item) => !isPromise(item))) { | ||
return expectMatchingValues(items); | ||
} | ||
|
||
assert(false, 'Received an invalid mixture of promises and values.'); | ||
} | ||
|
||
function expectMatchingValues<T>(values: ReadonlyArray<T>): T { | ||
const remainingValues = values.slice(1); | ||
for (const value of remainingValues) { | ||
expectJSON(value).toDeepEqual(values[0]); | ||
} | ||
return values[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { assert, expect } from 'chai'; | ||
|
||
import { inspect } from '../jsutils/inspect'; | ||
import { isPromise } from '../jsutils/isPromise'; | ||
|
||
export function expectPromise(maybePromise: unknown) { | ||
assert( | ||
isPromise(maybePromise), | ||
`Expected a promise, received '${inspect(maybePromise)}'`, | ||
); | ||
|
||
return { | ||
toResolve() { | ||
return maybePromise; | ||
}, | ||
async toRejectWith(message: string) { | ||
let caughtError: Error | undefined; | ||
let resolved; | ||
let rejected = false; | ||
try { | ||
resolved = await maybePromise; | ||
} catch (error) { | ||
rejected = true; | ||
caughtError = error; | ||
} | ||
|
||
assert( | ||
rejected, | ||
`Promise should have rejected with message '${message}', but resolved as '${inspect( | ||
resolved, | ||
)}'`, | ||
); | ||
|
||
expect(caughtError).to.be.an.instanceOf(Error); | ||
expect(caughtError).to.have.property('message', message); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters