test-fns
Version:
write usecase driven tests systematically for simpler, safer, and more readable code
63 lines • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const helpful_errors_1 = require("helpful-errors");
const givenWhenThen_1 = require("./givenWhenThen");
const usePrep_1 = require("./usePrep");
let callCount = 0;
describe('usePrep', () => {
(0, givenWhenThen_1.given)('a setup function that returns a test object', () => {
const setup = async () => {
callCount++;
return { value: `hello-${callCount}` };
};
describe('mode: beforeAll', () => {
(0, givenWhenThen_1.when)('registered with usePrep', () => {
const result = (0, usePrep_1.usePrep)(setup, { mode: 'beforeAll' });
(0, givenWhenThen_1.then)('value should be resolved once before all tests', async () => {
expect(result.value).toBeDefined();
expect(result.value).toMatch(/hello-1/);
});
(0, givenWhenThen_1.then)('subsequent tests still see the same value', async () => {
expect(result.value).toBe(`hello-1`);
});
});
});
describe('mode: beforeEach', () => {
(0, givenWhenThen_1.when)('registered with usePrep', () => {
const capturedValues = [];
const result = (0, usePrep_1.usePrep)(setup, { mode: 'beforeEach' });
(0, givenWhenThen_1.then)('each test gets a fresh value', async () => {
capturedValues.push(result.value);
expect(result.value).toMatch(/hello-\d+/);
});
(0, givenWhenThen_1.then)('value is recomputed between tests', async () => {
capturedValues.push(result.value);
expect(new Set(capturedValues).size).toBeGreaterThan(1);
});
});
});
});
(0, givenWhenThen_1.given)('accessing the proxy too early', () => {
(0, givenWhenThen_1.when)('getting a property before beforeAll runs', () => {
const value = (0, usePrep_1.usePrep)(async () => ({ foo: 'bar' }), {
mode: 'beforeAll',
});
const err = (0, helpful_errors_1.getError)(() => value.foo);
(0, givenWhenThen_1.then)('it throws an error', async () => {
expect(err.message).toMatch(/before setup completed/);
});
});
});
(0, givenWhenThen_1.given)('return value is accidentally called as function', () => {
(0, givenWhenThen_1.when)('accessing as if it were a callable', () => {
const value = (0, usePrep_1.usePrep)(async () => ({ foo: 'bar' }), {
mode: 'beforeAll',
});
const err = (0, helpful_errors_1.getError)(() => value());
(0, givenWhenThen_1.then)('it throws an error', async () => {
expect(err.message).toMatch(/value is not a function/);
});
});
});
});
//# sourceMappingURL=usePrep.test.js.map