https://github.com/devtin/jest-parser
parses jest files
https://github.com/devtin/jest-parser
Last synced: 9 months ago
JSON representation
parses jest files
- Host: GitHub
- URL: https://github.com/devtin/jest-parser
- Owner: devtin
- Created: 2021-03-06T16:57:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-19T13:18:42.000Z (over 3 years ago)
- Last Synced: 2025-06-04T12:17:52.219Z (10 months ago)
- Language: JavaScript
- Size: 91.8 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
jest-parser
Parses jest tests into an object
## Manifesto
Since nothing describes better what a software does than its tests, in order to make easy the documentation process, I
want to be able to parse the content of a jest file.
Mind this library is RegExp-based. See [disclaimer](#disclaimer) below.
## Installation
```sh
$ npm i jest-parser --save
# or
$ yarn add jest-parser
```
## Features
- [Parses jest syntax](#parses-jest-syntax)
## Parses jest syntax
Take the following jest file:
```js
describe('Some describe title 1', () => {
// some code 1
/* eslint-disable-next-line */
describe.only('Nested describe 1', () => {
// some setup
// some more setup
it('does this and that', () => {
// code of what it does
expect(true).toBeTruthy()
})
})
})
describe('Some describe title 2', () => {
// some code 2
})
describe('Some describe title 3', () => {
// some code 3
})
```
...and the following script:
```js
const { parse } = require('jest-parser')
const { readFileSync } = require('fs')
console.log(parse('Some jest file', readFileSync('jest-test.js').toString()))
```
would produce the following output:
```json
{
"title": "Some jest file",
"describe": [
{
"title": "Some describe title 1",
// entire file code
"code": "// some code 1\n/* eslint-disable-next-line */\ndescribe.only('Nested describe 1', () => {\n // some setup\n // some more setup\n\n it('does this and that', () => {\n // code of what it does\n expect(true).toBeTruthy()\n })\n})",
"start": 0,
"end": 12,
"describe": [
{
"title": "Nested describe 1",
// piece of code within "Nested describe 1"
"code": "// some setup\n// some more setup\n\nit('does this and that', () => {\n // code of what it does\n expect(true).toBeTruthy()\n})",
// position where "Nested describe 1" starts in parent's code
"start": 2,
// position where "Nested describe 1" ends in parent's code
"end": 10,
"flag": "only", // flags the test scenario might have (only, skip, todo...)
"describe": [],
"test": [],
"it": [
{
"title": "does this and that",
"code": "// code of what it does\nexpect(true).toBeTruthy()",
"start": 3,
"end": 6,
"describe": [],
"test": [],
"it": []
}
]
}
],
"test": [],
"it": []
},
{
"title": "Some describe title 2",
"code": "// some code 2",
"start": 14,
"end": 16,
"describe": [],
"test": [],
"it": []
},
{
"title": "Some describe title 3",
"code": "// some code 3",
"start": 18,
"end": 20,
"describe": [],
"test": [],
"it": []
}
],
"test": [],
"it": []
}
```
## Disclaimer
Mind this library is RegExp-based (not AST-based). That said, it might not work well using funky syntax.
One known scenario it won't work is a oneliner test (see discussion [here](https://github.com/devtin/jest-parser/pull/1)):
```js
// none of this would work
it('one line explicit-return arrow function', () => { return Promise.resolve(true) })
it('one line implicit-return arrow function', () => Promise.resolve(true))
it('multiline implicit-return arrow function', () =>
Promise.resolve(true)
)
```
* * *
### License
[MIT](https://opensource.org/licenses/MIT)
© 2021-present Martin Rafael Gonzalez