Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vitalishapovalov/jest-decorated
Decorators library for writing jest-based tests
https://github.com/vitalishapovalov/jest-decorated
decorators enzyme jest jest-tests react testing testing-tools typescript
Last synced: 1 day ago
JSON representation
Decorators library for writing jest-based tests
- Host: GitHub
- URL: https://github.com/vitalishapovalov/jest-decorated
- Owner: vitalishapovalov
- License: mit
- Created: 2019-11-16T22:20:02.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T05:26:20.000Z (almost 2 years ago)
- Last Synced: 2025-01-09T07:29:02.574Z (24 days ago)
- Topics: decorators, enzyme, jest, jest-tests, react, testing, testing-tools, typescript
- Language: TypeScript
- Homepage: https://vitalishapovalov.github.io/jest-decorated/
- Size: 2.73 MB
- Stars: 49
- Watchers: 3
- Forks: 4
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Contributing: docs/contributing.md
- License: LICENSE
Awesome Lists containing this project
README
Decorators library for writing jest-based tests
Wrapper around [jest](https://jestjs.io/) JavaScript testing framework.
Provides decorators with core jest globals. Also, provides utilities to minimize boilerplate code and make tests code more consistent.
[Read documentation.](https://vitalishapovalov.github.io/jest-decorated)
Jest test:
```typescript
describe("MyFnSpec", () => {
const consoleLogSpy = jest.spyOn(console, "log");
afterEach(() => {
consoleLogSpy.mockClear();
});
afterAll(() => {
consoleLogSpy.mockRestore();
});
test("shouldCallLogTwice", () => {
myFn("foo");
expect(consoleLogSpy).toHaveBeenCalledTimes(2);
});
test("shouldCallLogOnce", () => {
myFn("bar");
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
});
});
```Same test with `@jest-decorated`:
```typescript
@Describe()
class MyFnSpec {
@Spy(console, "log")
consoleLogSpy;
@Test()
shouldCallLogTwice() {
myFn("foo");
expect(this.consoleLogSpy).toHaveBeenCalledTimes(2);
}
@Test()
shouldCallLogOnce() {
myFn("bar");
expect(this.consoleLogSpy).toHaveBeenCalledTimes(1);
}
}
```## Install
Does not bring `jest` as a dependency, you should install the wanted version by yourself.
Install `jest`
```bash
npm i -D jest
```Install `@jest-decorated`
```bash
npm i -D @jest-decorated/core
```Install extensions (if needed)
```bash
npm i -D @jest-decorated/react
```## Setup
### With `setupFilesAfterEnv` jest option
You can register decorators once and use them everywhere, without importing! To achieve that, add `globals` files to `setupFilesAfterEnv` jest config.
For example, if we want to register `core` and `react` decorators globally:
```json
{
"setupFilesAfterEnv": [
"@jest-decorated/core/globals",
"@jest-decorated/react/globals"
]
}
```Or, if you already have single entry point for tests setup:
```json
{
"setupFilesAfterEnv": [
"/testSetup.ts"
]
}
```
```typescript
// testSetup.tsimport "@jest-decorated/core/globals";
import "@jest-decorated/react/globals";
```### With importing `globals` file
Another option is to import `globals` files in each test separately:
```typescript
// myFn.spec.tsimport "@jest-decorated/core/globals";
import "@jest-decorated/react/globals";@Describe()
@RunWith(ReactTestRunner)
class MyFnSpec {
// ...
}
```### With direct import
If solutions above doesn't serves your needs, you can use direct import:
```typescript
// myFn.spec.tsimport { Describe, RunWith } from "@jest-decorated/core";
import { ReactTestRunner } from "@jest-decorated/react";@Describe()
@RunWith(ReactTestRunner)
class MyFnSpec {
// ...
}
```## TypeScript
When using with TypeScript, make sure your setup file (in `setupFilesAfterEnv` section) is a `.ts` and not a `.js` to include the necessary types.
You will also need to include your setup file and the test folder in your `tsconfig.json` if you haven't already:
```json
{
"include": [
"./testSetup.ts",
"./__tests__"
]
}
```## Extensions
Support for different libs and frameworks. Currently, only [React](https://vitalishapovalov.github.io/jest-decorated/react) is strongly supported.
## Decorators
Read [docs](https://vitalishapovalov.github.io/jest-decorated/#/react/index).
## Contributing
[Contribution guidelines for this project](docs/contributing.md)
## License
[MIT License](LICENSE)