![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@fgv/ts-utils-jest
Advanced tools
A collection of additional Jest matchers primarily intended for code that uses ts-utils' Result type.
Also includes a handful of custom matchers to simplify the testing of other custom matchers. Who will test the testers?
With npm:
npm install --save-dev @fgv/ts-utils-jest
Note that snapshot testing for Jest itself can be tricky because different environments (e.g. CLI vs IDE vs CICD) might generate slightly different output due to e.g. differences in color display of diffs. To facilitate snapshot testing across multiple environments, this library also provides an extensible set of snapshot resolvers that can be used to capture environment-specific snapshots.
Use '.toFail' to verify that a Result<T> is a failure result.
test('passes with a failure result', () => {
expect(fail('oops')).toFail();
});
test('fails with a success result', () => {
expect(succeed('hello')).not.toFail();
});
Use '.toFailWith' to verify that a Result<T> is a failure result with a message that matches a supplied string or regular expression.
test('passes with a failure result and matching string or RegExp', () => {
expect(fail('oops')).toFailWith('oops');
expect(fail('oops')).toFailWith(/o.*/i);
});
test('fails with a success result', () => {
expect(succeed('hello')).not.toFailWith('hello');
});
test('fails with a failure result but non-matching string or RegExp', () => {
expect(fail('oops')).not.toFailWith('error');
expect(fail('oops')).not.toFailWith(/x.*/i);
});
Use '.toFailWithDetail' to verify that a DetailedResult<T> is a failure result that matches both a supplied expected failure message (string, RegExp or undefined) and a supplied failure detail.
test('passes with a failure result and matching string or RegExp', () => {
expect(failWithDetail('oops', 'detail')).toFailWithDetail('oops', 'detail');
expect(failWithDetail('oops', { detail: 'detail' })).toFailWithDetail(/o.*/i, { detail: 'detail' });
});
test('fails with a success result', () => {
expect(succeed('hello')).not.toFailWithDetail('hello', 'detail');
});
test('fails with a failure result but non-matching string or RegExp, or with a non-matching detail', () => {
expect(failWithDetail('oops', 'detail')).not.toFailWithDetail('error', 'detail');
expect(failWithDetail('oops', 'detail')).not.toFailWithDetail(/x.*/i, 'detail');
expect(failWithDetail('error', 'other detail')).not.toFailWithDetail('error', 'detail');
});
Use '.toSucceed' to verify that a Result<T> is a success result.
test('passes with a success result', () => {
expect(succeed('hello')).toSucceed();
});
test('fails with a failure result', () => {
expect(fail('oops')).not.toSucceed();
});
Use '.toSucceedWith' to verify that a Result<T> is a success and that the result value matches the supplied value. Works with asymmetric matchers.
test('succeeds with a success result that matches expected', () => {
expect(succeed('hello')).toSucceedWith('hello');
expect(succeed('hello')).toSucceedWith(expect.stringMatching(/h.*/i));
});
test('fails with a failure result', () => {
expect(fail('oops')).not.toSucceedWith('oops');
});
test('fails with a success result but a non-matching value', () => {
expect(succeed('hello')).not.toSucceedWith('goodbye');
});
test('passes with a matching asymmetric match', () => {
expect(succeed({
title: 'A title string',
subtitles: ['subtitle 1', 'subtitle 2'],
})).toSucceedWith(expect.objectContaining({
title: expect.stringMatching(/.*title*/),
subtitles: expect.arrayContaining([
'subtitle 1',
expect.stringContaining('2'),
]),
}));
});
test('fails with a non-matching asymmetric match', () => {
expect(succeed({
title: 'A title string',
subtitles: ['subtitle 1', 'subtitle 2'],
})).not.toSucceedWith(expect.objectContaining({
title: expect.stringMatching(/.*title*/),
subtitles: expect.arrayContaining([
'subtitle 1',
expect.stringContaining('3'),
]),
}));
});
Use '.toSucceedAndSatisfy' to verify that a Result<T> is a success and that the result matches the supplied predicate. Handles predicates that also use 'expect' to test the result object.
test('passes with a success value and a callback that returns true', () => {
expect(succeed('hello')).toSucceedAndSatisfy((value: string) => value === 'hello');
});
test('fails with a success value but a callback that returns false', () => {
expect(succeed('hello')).not.toSucceedAndSatisfy((value: string) => value !== 'hello');
});
test('fails with a success value but a callback that fails an expectation', () => {
expect(succeed('hello')).not.toSucceedAndSatisfy((value: string) => {
expect(value).toBe('goodbye');
return true;
});
});
test('fails with a success value but a callback that throws an exception', () => {
expect(succeed('hello')).not.toSucceedAndSatisfy((_value: string) => {
throw new Error('UH OH AN ERRROR');
});
});
test('fails with a failure value', () => {
expect(fail('oops')).not.toSucceedAndSatisfy((value: string) => value === 'oops');
});
Use .toSucceedAndMatchSnapshot to verify that a Result is a success and that the result value matches a stored snapshot.
test('passes for a success result that matches the snapshot', () => {
expect(succeed({
someField: 'this is a value',
nestedObject: {
anArray: ['element 1', 'element 2'],
},
})).toSucceedAndMatchSnapshot();
});
test('fails for a failure result', () => {
expect(fail('oops')).not.toSucceedAndMatchSnapshot();
});
Use .toSucceedAndMatchInlineSnapshot to verify that a Result is a success and that the result value matches an inline snapshot.
test('passes for a success result that matches the snapshot', () => {
expect(
succeed({
someField: 'this is a value',
nestedObject: {
anArray: ['element 1', 'element 2'],
},
})
).toSucceedAndMatchInlineSnapshot(`
Object {
"nestedObject": Object {
"anArray": Array [
"element 1",
"element 2",
],
},
"someField": "this is a value",
}
`);
});
Use '.toFailTest' to test a custom matcher by verifying that a test case fails.
test('passes for a callback that fails', () => {
expect(() => {
expect(true).toBe(false);
}).toFailTest();
});
test('fails for a callback that succeeds', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
expect(() => {}).not.toFailTest();
});
Use '.toFailTestWith' to test a custom matcher by verifying that a test case fails as expected and reports an error matching a supplied value.
test('fails for a callback that succeeds', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
expect(() => {}).not.toFailTestWith('whatever');
});
test('passes for a callback that fails with an error matching a supplied RegExp', () => {
expect(() => {
expect('hello').toEqual('goodbye');
}).toFailTestWith(/expect/i);
});
test('passes for a callback that fails with an error matching a supplied string', () => {
expect(() => {
throw new Error('This is an error');
}).toFailTestWith('This is an error');
});
test('passes for a callback that fails with an error matching a supplied array of strings', () => {
expect(() => {
throw new Error('This is an error\n that spills over to a second line');
}).toFailTestWith([
'This is an error',
' that spills over to a second line',
]);
});
test('passes for a callback that fails with an error matching a supplied array of matchers', () => {
expect(() => {
throw new Error('This is an error\n that spills over to a second line');
}).toFailTestWith([
expect.stringMatching(/error/i),
expect.stringMatching(/spills/i),
]);
});
test('fails for a callback that fails with an unexpected value', () => {
expect(() => {
expect('hello').toBe('goodbye');
}).not.toFailTestWith(/random text/i);
});
Use '.toFailTestAndMatchSnapshot' to test a custom matcher by verifying that a test case fails as expected and reports an error matching a stored snapshot.
test('passes for a test that fails with a result matching the snapshot', () => {
expect(() => {
expect(true).toBe(false);
}).toFailTestAndMatchSnapshot();
});
test('fails for a test that does not fail', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
expect(() => {}).not.toFailTestAndMatchSnapshot();
});
Note that snapshot testing for Jest itself can be tricky because different environments (e.g. CLI vs IDE vs CICD) might generate slightly different output due to e.g. differences in color display of diffs. To facilitate snapshot testing across multiple environments, this library also provides an extensible set of snapshot resolvers that can be used to capture environment-specific snapshots.
FAQs
Custom matchers for ts-utils result class
The npm package @fgv/ts-utils-jest receives a total of 49 weekly downloads. As such, @fgv/ts-utils-jest popularity was classified as not popular.
We found that @fgv/ts-utils-jest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.